0 Votes

Changes for page Todolist Macro

Last modified by Ryan C on 2025/04/30 07:54

From version 8.1
edited by Ryan C
on 2025/04/24 10:23
Change comment: There is no comment for this version
To version 28.1
edited by Ryan C
on 2025/04/24 11:28
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,7 +1,9 @@
1 1  Macro to display todo-lists using emberjs. This code is based on the emberjs demo. You can see an example below
2 2  
3 3  {{todolist width="50%" center="1"}}
4 -a thing|1
5 -a second thing|0
4 +a 3nd thing|0
5 +adding this thing|0
6 +adding this thing|0
7 +refreshed they are still there not even having to save page|0
6 6  
7 7  {{/todolist}}
XWiki.JavaScriptExtension[0]
Code
... ... @@ -1,257 +1,333 @@
1 1  requirejs.config({
2 - //By default load any module IDs from js/lib
3 - baseUrl: '${xwiki.getDocument("TodoLists.TodoListMacro").getURL("download")}/',
4 - //except, if the module ID starts with "app",
5 - //load it from the js/app directory. paths
6 - //config is relative to the baseUrl, and
7 - //never includes a ".js" extension since
8 - //the paths config could be for a directory.
9 - paths: {
2 + baseUrl: '${xwiki.getDocument("TodoLists.TodoListMacro").getURL("download")}/',
3 + paths: {
4 + 'jquery-1.10.2': 'https://code.jquery.com/jquery-1.10.2.min'
5 + },
6 + shim: {
7 + 'ember-data': {
8 + deps: ['ember']
10 10   },
11 - shim: {
12 - 'ember-data': {
13 - deps: ['ember', 'jquery']
14 - },
15 - 'ember': {
16 - deps: ['jquery']
17 - }
10 + 'ember': {
11 + deps: ['jquery-1.10.2']
18 18   }
13 + }
19 19  });
20 -require(['jquery', 'handlebars-v1.2.1', 'ember-data', 'ember' ], function ($) {
21 21  
22 -// create app
23 -window.Todos = Ember.Application.create({
24 - rootElement: '#todoappdiv'
25 -});
16 +// Load jQuery 1.10.2 specifically for Ember, using noConflict mode
17 +require(['jquery-1.10.2'], function(jQuery) {
18 + // Store the original jQuery and $ if they exist
19 + var originalJQuery = window.jQuery;
20 + var original$ = window.$;
21 +
22 + // Create a new jQuery instance in noConflict mode
23 + var emberJQuery = jQuery.noConflict(true);
24 +
25 + // Now load Ember with our specific jQuery
26 + require(['handlebars-v1.2.1', 'ember-data', 'ember'], function() {
27 + // Make emberJQuery available to Ember
28 + window.jQuery = emberJQuery;
29 + window.$ = emberJQuery;
30 +
31 + // Initialize Ember app
32 + initializeEmberApp(emberJQuery);
33 +
34 + // Restore original jQuery and $ when done
35 + window.jQuery = originalJQuery;
36 + window.$ = original$;
37 + });
38 +
39 + function initializeEmberApp(jQuery) {
40 + // Create app with specific jQuery instance
41 + window.Todos = Ember.Application.create({
42 + rootElement: '#todoappdiv',
43 + jQuery: jQuery
44 + });
26 26  
27 -// define the todolist store that will write to the TodoListsService
28 -Todos.ApplicationAdapter = DS.Adapter.extend({
29 - createRecord: function(store, type, record) {
30 - console.log("createRecord");
31 - var query = { create: "1", content : JSON.stringify(record) };
32 - return new Ember.RSVP.Promise(function(resolve, reject) {
33 - jQuery.getJSON("${xwiki.getURL("TodoLists.TodoListsService")}?page=" + XWiki.currentSpace + "." + XWiki.currentPage + "&xpage=plain&outputSyntax=plain", query).then(function(data) {
34 - Ember.run(null, resolve, data);
35 - }, function(jqXHR) {
36 - jqXHR.then = null; // tame jQuery's ill mannered promises
37 - Ember.run(null, reject, jqXHR);
38 - });
46 + // Define the todolist store that will write to the TodoListsService
47 + Todos.ApplicationAdapter = DS.Adapter.extend({
48 + serialize: function(record, options) {
49 + var serialized = record.record.toJSON();
50 + if (options && options.includeId) {
51 + serialized.id = record.id;
52 + }
53 + return serialized;
54 + },
55 +
56 + createRecord: function(store, type, record) {
57 + console.log("createRecord");
58 + var serialized = this.serialize(record, { includeId: true });
59 + var query = { create: "1", content: JSON.stringify(serialized) };
60 +
61 + return new Ember.RSVP.Promise(function(resolve, reject) {
62 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
63 + XWiki.currentSpace + "." + XWiki.currentPage +
64 + "&xpage=plain&outputSyntax=plain";
65 + console.log("Creating record, URL:", url);
66 +
67 + jQuery.getJSON(url, query).then(function(data) {
68 + console.log("Create success:", data);
69 + Ember.run(function() {
70 + resolve(data);
71 + });
72 + }, function(jqXHR) {
73 + console.error("Create error:", jqXHR);
74 + Ember.run(function() {
75 + reject(jqXHR);
76 + });
77 + });
78 + });
79 + },
80 +
81 + deleteRecord: function(store, type, record) {
82 + console.log("deleteRecord");
83 + var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
84 + if (recordArrays && recordArrays.list && recordArrays.list[0]) {
85 + return this.saveAll(recordArrays.list[0].content);
86 + }
87 + return Ember.RSVP.resolve();
88 + },
89 +
90 + find: function(store, type, id) {
91 + console.log("find");
92 + return Ember.RSVP.resolve();
93 + },
94 +
95 + findAll: function(store, type, sinceToken) {
96 + console.log("findAll");
97 + var query = { since: sinceToken };
98 +
99 + return new Ember.RSVP.Promise(function(resolve, reject) {
100 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
101 + XWiki.currentSpace + "." + XWiki.currentPage +
102 + "&xpage=plain&outputSyntax=plain";
103 + console.log("Finding all records, URL:", url);
104 +
105 + jQuery.getJSON(url, query).then(function(data) {
106 + console.log("FindAll success:", data);
107 + Ember.run(function() {
108 + resolve(data);
109 + });
110 + }, function(jqXHR) {
111 + console.error("FindAll error:", jqXHR);
112 + Ember.run(function() {
113 + reject(jqXHR);
114 + });
115 + });
116 + });
117 + },
118 +
119 + updateRecord: function(store, type, record) {
120 + console.log("updateRecord");
121 + var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
122 + if (recordArrays && recordArrays.list && recordArrays.list[0]) {
123 + return this.saveAll(recordArrays.list[0].content);
124 + }
125 + return Ember.RSVP.resolve();
126 + },
127 +
128 + saveAll: function(alldata) {
129 + console.log("saveAll");
130 + var query = { save: "1", content: JSON.stringify(alldata) };
131 +
132 + return new Ember.RSVP.Promise(function(resolve, reject) {
133 + var currentPage = XWiki.currentSpace + "." + XWiki.currentPage;
134 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
135 + currentPage + "&xpage=plain&outputSyntax=plain";
136 + console.log("Saving all records, URL:", url, "Page:", currentPage);
137 +
138 + jQuery.getJSON(url, query).then(function(data) {
139 + console.log("SaveAll success:", data);
140 + Ember.run(function() {
141 + resolve(data);
142 + });
143 + }, function(jqXHR) {
144 + console.error("SaveAll error:", jqXHR);
145 + Ember.run(function() {
146 + reject(jqXHR);
147 + });
148 + });
149 + });
150 + }
39 39   });
40 - },
41 - deleteRecord: function(store, type, record) {
42 - console.log("deleteRecord");
43 - return this.saveAll(store.recordArrayManager.recordArraysForRecord(record).list[0].content);
44 - },
45 - find: function(store, type, id) {
46 - console.log("find");
47 - return;
48 - },
49 - findAll: function(store, type, sinceToken) {
50 - console.log("findAll");
51 - var query = { since: sinceToken };
52 - return new Ember.RSVP.Promise(function(resolve, reject) {
53 - jQuery.getJSON("${xwiki.getURL("TodoLists.TodoListsService")}?page=" + XWiki.currentSpace + "." + XWiki.currentPage + "&xpage=plain&outputSyntax=plain", query).then(function(data) {
54 - Ember.run(null, resolve, data);
55 - }, function(jqXHR) {
56 - jqXHR.then = null; // tame jQuery's ill mannered promises
57 - Ember.run(null, reject, jqXHR);
152 +
153 + // Routing
154 + Todos.Router.map(function() {
155 + this.resource('todos', { path: '' }, function() {
156 + this.route('active');
157 + this.route('completed');
58 58   });
59 59   });
60 - },
61 - updateRecord: function(store, type, record) {
62 - return this.saveAll(store.recordArrayManager.recordArraysForRecord(record).list[0].content);
63 - },
64 - saveAll: function(alldata) {
65 - console.log("updateRecord");
66 - var query = { save: "1", content : JSON.stringify(alldata) };
67 - return new Ember.RSVP.Promise(function(resolve, reject) {
68 - jQuery.getJSON("${xwiki.getURL("TodoLists.TodoListsService")}?page=" + XWiki.currentSpace + "." + XWiki.currentPage + "&xpage=plain&outputSyntax=plain", query).then(function(data) {
69 - Ember.run(null, resolve, data);
70 - }, function(jqXHR) {
71 - jqXHR.then = null; // tame jQuery's ill mannered promises
72 - Ember.run(null, reject, jqXHR);
73 - });
160 +
161 + Todos.TodosRoute = Ember.Route.extend({
162 + model: function() {
163 + return this.store.find('todo');
164 + }
74 74   });
75 - }
76 -});
77 77  
78 -// use my own store
79 -Todos.store = DS.Store.create({
80 - adapter: 'ApplicationAdapter'
81 -});
167 + Todos.TodosIndexRoute = Ember.Route.extend({
168 + model: function() {
169 + return this.modelFor('todos');
170 + }
171 + });
82 82  
83 -// Todos.ApplicationAdapter = DS.FixtureAdapter.extend();
173 + Todos.TodosActiveRoute = Ember.Route.extend({
174 + model: function() {
175 + return this.store.filter('todo', function(todo) {
176 + return !todo.get('isCompleted');
177 + });
178 + },
179 + renderTemplate: function(controller) {
180 + this.render('todos/index', { controller: controller });
181 + }
182 + });
84 84  
85 -// routing
86 -Todos.Router.map(function() {
87 - this.resource('todos', { path: '' }, function() {
88 - this.route('active');
89 - this.route('completed');
90 - });
91 -});
92 -
93 -Todos.TodosRoute = Ember.Route.extend({
94 - model: function() {
95 - return this.store.find('todo');
96 - }
97 -});
98 -
99 -Todos.TodosIndexRoute = Ember.Route.extend({
100 - model: function() {
101 - return this.modelFor('todos');
102 - }
103 -});
104 -
105 -// ... additional lines truncated for brevity ...
106 -Todos.TodosActiveRoute = Ember.Route.extend({
107 - model: function(){
108 - return this.store.filter('todo', function(todo) {
109 - return !todo.get('isCompleted');
184 + Todos.TodosCompletedRoute = Ember.Route.extend({
185 + model: function() {
186 + return this.store.filter('todo', function(todo) {
187 + return todo.get('isCompleted');
188 + });
189 + },
190 + renderTemplate: function(controller) {
191 + this.render('todos/index', { controller: controller });
192 + }
110 110   });
111 - },
112 - renderTemplate: function(controller) {
113 - this.render('todos/index', {controller: controller});
114 - }
115 -});
116 116  
117 -Todos.TodosCompletedRoute = Ember.Route.extend({
118 - model: function(){
119 - return this.store.filter('todo', function (todo) {
120 - return todo.get('isCompleted');
195 + // Data model
196 + Todos.Todo = DS.Model.extend({
197 + title: DS.attr('string'),
198 + priority: DS.attr('string'),
199 + assignee: DS.attr('string'),
200 + isCompleted: DS.attr('boolean')
121 121   });
122 - },
123 - renderTemplate: function(controller){
124 - this.render('todos/index', {controller: controller});
125 - }
126 -});
127 127  
128 -// data model
129 -Todos.Todo = DS.Model.extend({
130 - title: DS.attr('string'),
131 - priority: DS.attr('string'),
132 - assignee: DS.attr('string'),
133 - isCompleted: DS.attr('boolean')
134 -});
203 + // Controller
204 + Todos.TodoController = Ember.ObjectController.extend({
205 + actions: {
206 + editTodo: function() {
207 + this.set('isEditing', true);
208 + },
209 + acceptChanges: function() {
210 + this.set('isEditing', false);
135 135  
136 -// ... additional lines truncated for brevity ...
137 -Todos.Todo.FIXTURES = [
138 - {
139 - id: 1,
140 - title: 'Learn Ember.js X',
141 - isCompleted: true
142 - },
143 - {
144 - id: 2,
145 - title: '...',
146 - isCompleted: false
147 - },
148 - {
149 - id: 3,
150 - title: 'Profit!',
151 - isCompleted: false
152 - }
153 -];
212 + if (Ember.isEmpty(this.get('model.title'))) {
213 + this.send('removeTodo');
214 + } else {
215 + this.get('model').save();
216 + }
217 + },
218 + removeTodo: function() {
219 + var todo = this.get('model');
220 + todo.deleteRecord();
221 + todo.save();
222 + }
223 + },
154 154  
155 -// controler
156 -Todos.TodoController = Ember.ObjectController.extend({
157 - actions: {
158 - editTodo: function () {
159 - this.set('isEditing', true);
160 - },
161 - acceptChanges: function() {
162 - this.set('isEditing', false);
225 + isEditing: false,
163 163  
164 - if (Ember.isEmpty(this.get('model.title'))) {
165 - this.send('removeTodo');
166 - } else {
167 - this.get('model').save();
168 - }
169 - },
170 - removeTodo: function() {
171 - var todo = this.get('model');
172 - todo.deleteRecord();
173 - todo.save();
174 - }
175 - },
227 + isCompleted: function(key, value) {
228 + var model = this.get('model');
176 176  
177 - isEditing: false,
230 + if (value === undefined) {
231 + // Property being used as a getter
232 + return model.get('isCompleted');
233 + } else {
234 + // Property being used as setter
235 + model.set('isCompleted', value);
236 + model.save();
237 + return value;
238 + }
239 + }.property('model.isCompleted')
240 + });
178 178  
179 - isCompleted: function(key, value){
180 - var model = this.get('model');
242 + Todos.TodosController = Ember.ArrayController.extend({
243 + actions: {
244 + createTodo: function() {
245 + // Get the todo title set by the "New Todo" text field
246 + var title = this.get('newTitle');
247 + if (!title || !title.trim()) { return; }
181 181  
182 - if (value === undefined) {
183 - // property being used as a getter
184 - return model.get('isCompleted');
185 - } else {
186 - // property being used as setter
187 - model.set('isCompleted', value);
188 - model.save();
189 - return value;
190 - }
191 - }.property('model.isCompleted')
192 -});
249 + console.log("Creating new todo with title:", title);
193 193  
194 -Todos.TodosController = Ember.ArrayController.extend({
195 - actions: {
196 - createTodo: function () {
197 - // Get the todo title set by the "New Todo" text field
198 - var title = this.get('newTitle');
199 - if (!title.trim()) { return; }
251 + // Create the new Todo model
252 + var todo = this.store.createRecord('todo', {
253 + title: title,
254 + priority: "P3",
255 + assignee: "all",
256 + isCompleted: false
257 + });
200 200  
201 - // Create the new Todo model
202 - var todo = this.store.createRecord('todo', {
203 - title: title,
204 - priority: "P3",
205 - assignee: "all",
206 - isCompleted: false
207 - });
259 + // Clear the "New Todo" text field
260 + this.set('newTitle', '');
208 208  
209 - // Clear the "New Todo" text field
210 - this.set('newTitle', '');
262 + // Save the new model
263 + todo.save().then(function(savedTodo) {
264 + console.log("Todo saved successfully:", savedTodo);
265 + // Force a refresh of the list after saving
266 + Ember.run.later(function() {
267 + console.log("Refreshing todo list");
268 + Todos.store.find('todo');
269 + }, 100);
270 + }, function(error) {
271 + console.error("Failed to save todo:", error);
272 + });
273 + },
274 +
275 + clearCompleted: function() {
276 + var completed = this.filter(function(todo) {
277 + return todo.get('isCompleted');
278 + });
279 +
280 + completed.forEach(function(todo) {
281 + todo.deleteRecord();
282 + todo.save();
283 + });
284 + }
285 + },
211 211  
212 - // Save the new model
213 - todo.save();
214 - },
215 - clearCompleted: function () {
216 - var completed = this.filterProperty('isCompleted', true);
217 - completed.invoke('deleteRecord');
218 - completed.invoke('save');
219 - }
220 - },
287 + remaining: function() {
288 + return this.filter(function(todo) {
289 + return !todo.get('isCompleted');
290 + }).get('length');
291 + }.property('@each.isCompleted'),
221 221  
222 - remaining: function () {
223 - return this.filterProperty('isCompleted', false).get('length');
224 - }.property('@each.isCompleted'),
293 + inflection: function() {
294 + var remaining = this.get('remaining');
295 + return remaining === 1 ? 'item' : 'items';
296 + }.property('remaining'),
297 +
298 + hasCompleted: function() {
299 + return this.get('completed') > 0;
300 + }.property('completed'),
225 225  
226 - inflection: function () {
227 - var remaining = this.get('remaining');
228 - return remaining === 1 ? 'item' : 'items';
229 - }.property('remaining'),
230 - hasCompleted: function () {
231 - return this.get('completed') > 0;
232 - }.property('completed'),
302 + completed: function() {
303 + return this.filter(function(todo) {
304 + return todo.get('isCompleted');
305 + }).get('length');
306 + }.property('@each.isCompleted'),
233 233  
234 - completed: function () {
235 - return this.filterProperty('isCompleted', true).get('length');
236 - }.property('@each.isCompleted'),
308 + allAreDone: function(key, value) {
309 + if (value === undefined) {
310 + return this.get('length') > 0 && this.every(function(todo) {
311 + return todo.get('isCompleted');
312 + });
313 + } else {
314 + this.forEach(function(todo) {
315 + todo.set('isCompleted', value);
316 + todo.save();
317 + });
318 + return value;
319 + }
320 + }.property('@each.isCompleted')
321 + });
237 237  
238 - allAreDone: function(key, value) {
239 - if (value === undefined) {
240 - return !!this.get('length') && this.everyProperty('isCompleted', true);
241 - } else {
242 - this.setEach('isCompleted', value);
243 - this.invoke('save');
244 - return value;
245 - }
246 -}.property('@each.isCompleted')
247 -});
323 + // Editing
324 + Todos.EditTodoView = Ember.TextField.extend({
325 + didInsertElement: function() {
326 + this.$().focus();
327 + }
328 + });
248 248  
249 -// editing
250 -Todos.EditTodoView = Ember.TextField.extend({
251 - didInsertElement: function() {
252 - this.$().focus();
330 + Ember.Handlebars.helper('edit-todo', Todos.EditTodoView);
253 253   }
254 254  });
255 255  
256 -Ember.Handlebars.helper('edit-todo', Todos.EditTodoView);
257 -});
XWiki.WikiMacroClass[0]
Cached
... ... @@ -1,0 +1,1 @@
1 +No
Context elements
... ... @@ -1,0 +1,1 @@
1 +User
Asynchronous rendering
... ... @@ -1,0 +1,1 @@
1 +No