0 Votes

Changes for page Todolist Macro

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

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

Summary

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -1,69 +1,84 @@
1 1  requirejs.config({
2 2   baseUrl: '${xwiki.getDocument("TodoLists.TodoListMacro").getURL("download")}/',
3 3   paths: {
4 - 'jquery-1.10.2': 'https://code.jquery.com/jquery-1.10.2.min'
4 + // Use existing jQuery if available, otherwise load 1.10.2
5 + 'jquery': window.jQuery ? 'empty:' : 'https://code.jquery.com/jquery-1.10.2.min'
5 5   },
7 + map: {
8 + // Map any module requiring 'jquery' to use the global jQuery
9 + '*': { 'jquery': window.jQuery ? 'jquery-global' : 'jquery' }
10 + },
6 6   shim: {
7 7   'ember-data': {
8 - deps: ['ember']
13 + deps: ['ember', 'jquery']
9 9   },
10 10   'ember': {
11 - deps: ['jquery-1.10.2']
16 + deps: ['jquery']
12 12   }
13 13   }
14 14  });
15 15  
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
21 +// Define a module that returns the global jQuery
22 +define('jquery-global', [], function() {
23 + return window.jQuery;
24 +});
25 +
26 +// Define empty module for when jQuery is already loaded
27 +define('empty:', [], function() {
28 + return window.jQuery;
29 +});
30 +
31 +require(['jquery', 'handlebars-v1.2.1', 'ember-data', 'ember'], function($) {
32 + // Check jQuery version compatibility with Ember
33 + var jQueryVersion = $.fn.jquery.split('.');
34 + var majorVersion = parseInt(jQueryVersion[0]);
35 + var minorVersion = parseInt(jQueryVersion[1]);
36 +
37 + console.log("Using jQuery version:", $.fn.jquery);
38 +
39 + // Verify jQuery version is compatible with Ember
40 + var isCompatible = (majorVersion === 1 && minorVersion >= 7 && minorVersion <= 10) ||
41 + (majorVersion === 2 && minorVersion === 0);
42 +
43 + if (!isCompatible) {
44 + console.error("Warning: Ember Views require jQuery 1.7, 1.8, 1.9, 1.10, or 2.0. Current version:", $.fn.jquery);
45 + // Try to load a compatible version if needed
46 + require(['https://code.jquery.com/jquery-1.10.2.min.js'], function(compatibleJQuery) {
47 + // Store the original jQuery
48 + var originalJQuery = window.jQuery;
49 + // Replace with compatible jQuery temporarily
50 + window.jQuery = compatibleJQuery;
51 + // Continue with the rest of the code
52 + initializeEmberApp(compatibleJQuery);
53 + // Restore original jQuery when done
54 + window.jQuery = originalJQuery;
55 + });
56 + } else {
57 + // jQuery version is compatible, proceed normally
58 + initializeEmberApp($);
59 + }
60 +
61 + function initializeEmberApp($) {
62 + // Create app
41 41   window.Todos = Ember.Application.create({
42 - rootElement: '#todoappdiv',
43 - jQuery: jQuery
64 + rootElement: '#todoappdiv'
44 44   });
45 45  
46 46   // Define the todolist store that will write to the TodoListsService
47 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 -
69 + namespace: 'api', // Moved inside the adapter definition
70 +
56 56   createRecord: function(store, type, record) {
57 57   console.log("createRecord");
58 - var serialized = this.serialize(record, { includeId: true });
59 - var query = { create: "1", content: JSON.stringify(serialized) };
60 -
73 + var data = this.serialize(record, { includeId: true });
74 + var query = { create: "1", content: JSON.stringify(data) };
75 +
61 61   return new Ember.RSVP.Promise(function(resolve, reject) {
62 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
63 - XWiki.currentSpace + "." + XWiki.currentPage +
77 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
78 + XWiki.currentSpace + "." + XWiki.currentPage +
64 64   "&xpage=plain&outputSyntax=plain";
65 65   console.log("Creating record, URL:", url);
66 -
81 +
67 67   jQuery.getJSON(url, query).then(function(data) {
68 68   console.log("Create success:", data);
69 69   Ember.run(function() {
... ... @@ -77,7 +77,7 @@
77 77   });
78 78   });
79 79   },
80 -
95 +
81 81   deleteRecord: function(store, type, record) {
82 82   console.log("deleteRecord");
83 83   var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
... ... @@ -86,22 +86,22 @@
86 86   }
87 87   return Ember.RSVP.resolve();
88 88   },
89 -
104 +
90 90   find: function(store, type, id) {
91 91   console.log("find");
92 92   return Ember.RSVP.resolve();
93 93   },
94 -
109 +
95 95   findAll: function(store, type, sinceToken) {
96 96   console.log("findAll");
97 97   var query = { since: sinceToken };
98 -
113 +
99 99   return new Ember.RSVP.Promise(function(resolve, reject) {
100 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
101 - XWiki.currentSpace + "." + XWiki.currentPage +
115 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
116 + XWiki.currentSpace + "." + XWiki.currentPage +
102 102   "&xpage=plain&outputSyntax=plain";
103 103   console.log("Finding all records, URL:", url);
104 -
119 +
105 105   jQuery.getJSON(url, query).then(function(data) {
106 106   console.log("FindAll success:", data);
107 107   Ember.run(function() {
... ... @@ -115,7 +115,7 @@
115 115   });
116 116   });
117 117   },
118 -
133 +
119 119   updateRecord: function(store, type, record) {
120 120   console.log("updateRecord");
121 121   var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
... ... @@ -124,17 +124,17 @@
124 124   }
125 125   return Ember.RSVP.resolve();
126 126   },
127 -
142 +
128 128   saveAll: function(alldata) {
129 129   console.log("saveAll");
130 130   var query = { save: "1", content: JSON.stringify(alldata) };
131 -
146 +
132 132   return new Ember.RSVP.Promise(function(resolve, reject) {
133 133   var currentPage = XWiki.currentSpace + "." + XWiki.currentPage;
134 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
149 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
135 135   currentPage + "&xpage=plain&outputSyntax=plain";
136 136   console.log("Saving all records, URL:", url, "Page:", currentPage);
137 -
152 +
138 138   jQuery.getJSON(url, query).then(function(data) {
139 139   console.log("SaveAll success:", data);
140 140   Ember.run(function() {
... ... @@ -271,12 +271,12 @@
271 271   console.error("Failed to save todo:", error);
272 272   });
273 273   },
274 -
289 +
275 275   clearCompleted: function() {
276 276   var completed = this.filter(function(todo) {
277 277   return todo.get('isCompleted');
278 278   });
279 -
294 +
280 280   completed.forEach(function(todo) {
281 281   todo.deleteRecord();
282 282   todo.save();
... ... @@ -294,7 +294,7 @@
294 294   var remaining = this.get('remaining');
295 295   return remaining === 1 ? 'item' : 'items';
296 296   }.property('remaining'),
297 -
312 +
298 298   hasCompleted: function() {
299 299   return this.get('completed') > 0;
300 300   }.property('completed'),