1 | /*global $, dotclear, jsToolBar */ |
---|
2 | 'use strict'; |
---|
3 | |
---|
4 | dotclear.dbCommentsCount = function() { |
---|
5 | const params = { |
---|
6 | f: 'getCommentsCount', |
---|
7 | xd_check: dotclear.nonce, |
---|
8 | }; |
---|
9 | $.get('services.php', params, function(data) { |
---|
10 | if ($('rsp[status=failed]', data).length > 0) { |
---|
11 | // For debugging purpose only: |
---|
12 | // console.log($('rsp',data).attr('message')); |
---|
13 | window.console.log('Dotclear REST server error'); |
---|
14 | } else { |
---|
15 | const nb = $('rsp>count', data).attr('ret'); |
---|
16 | if (nb != dotclear.dbCommentsCount_Counter) { |
---|
17 | // First pass or counter changed |
---|
18 | const icon = $('#dashboard-main #icons p a[href="comments.php"]'); |
---|
19 | if (icon.length) { |
---|
20 | // Update count if exists |
---|
21 | const nb_label = icon.children('span.db-icon-title'); |
---|
22 | if (nb_label.length) { |
---|
23 | nb_label.text(nb); |
---|
24 | } |
---|
25 | } |
---|
26 | // Store current counter |
---|
27 | dotclear.dbCommentsCount_Counter = nb; |
---|
28 | } |
---|
29 | } |
---|
30 | }); |
---|
31 | }; |
---|
32 | dotclear.dbPostsCount = function() { |
---|
33 | const params = { |
---|
34 | f: 'getPostsCount', |
---|
35 | xd_check: dotclear.nonce, |
---|
36 | }; |
---|
37 | $.get('services.php', params, function(data) { |
---|
38 | if ($('rsp[status=failed]', data).length > 0) { |
---|
39 | // For debugging purpose only: |
---|
40 | // console.log($('rsp',data).attr('message')); |
---|
41 | window.console.log('Dotclear REST server error'); |
---|
42 | } else { |
---|
43 | const nb = $('rsp>count', data).attr('ret'); |
---|
44 | if (nb != dotclear.dbPostsCount_Counter) { |
---|
45 | // First pass or counter changed |
---|
46 | const icon = $('#dashboard-main #icons p a[href="posts.php"]'); |
---|
47 | if (icon.length) { |
---|
48 | // Update count if exists |
---|
49 | const nb_label = icon.children('span.db-icon-title'); |
---|
50 | if (nb_label.length) { |
---|
51 | nb_label.text(nb); |
---|
52 | } |
---|
53 | } |
---|
54 | // Store current counter |
---|
55 | dotclear.dbPostsCount_Counter = nb; |
---|
56 | } |
---|
57 | } |
---|
58 | }); |
---|
59 | }; |
---|
60 | $(function() { |
---|
61 | function quickPost(f, status) { |
---|
62 | if ($.isFunction('jsToolBar') && (contentTb.getMode() == 'wysiwyg')) { |
---|
63 | contentTb.syncContents('iframe'); |
---|
64 | } |
---|
65 | |
---|
66 | const params = { |
---|
67 | f: 'quickPost', |
---|
68 | xd_check: dotclear.nonce, |
---|
69 | post_title: $('#post_title', f).val(), |
---|
70 | post_content: $('#post_content', f).val(), |
---|
71 | cat_id: $('#cat_id', f).val(), |
---|
72 | post_status: status, |
---|
73 | post_format: $('#post_format', f).val(), |
---|
74 | post_lang: $('#post_lang', f).val(), |
---|
75 | new_cat_title: $('#new_cat_title', f).val(), |
---|
76 | new_cat_parent: $('#new_cat_parent', f).val() |
---|
77 | }; |
---|
78 | |
---|
79 | $('p.qinfo', f).remove(); |
---|
80 | |
---|
81 | $.post('services.php', params, function(data) { |
---|
82 | let msg; |
---|
83 | if ($('rsp[status=failed]', data).length > 0) { |
---|
84 | msg = '<p class="qinfo"><strong>' + dotclear.msg.error + |
---|
85 | '</strong> ' + $('rsp', data).text() + '</p>'; |
---|
86 | } else { |
---|
87 | msg = '<p class="qinfo">' + dotclear.msg.entry_created + |
---|
88 | ' - <a href="post.php?id=' + $('rsp>post', data).attr('id') + '">' + |
---|
89 | dotclear.msg.edit_entry + '</a>'; |
---|
90 | if ($('rsp>post', data).attr('post_status') == 1) { |
---|
91 | msg += ' - <a href="' + $('rsp>post', data).attr('post_url') + '">' + |
---|
92 | dotclear.msg.view_entry + '</a>'; |
---|
93 | } |
---|
94 | msg += '</p>'; |
---|
95 | $('#post_title', f).val(''); |
---|
96 | $('#post_content', f).val(''); |
---|
97 | $('#post_content', f).change(); |
---|
98 | if ($.isFunction('jsToolBar') && (contentTb.getMode() == 'wysiwyg')) { |
---|
99 | contentTb.syncContents('textarea'); |
---|
100 | } |
---|
101 | $('#cat_id', f).val('0'); |
---|
102 | $('#new_cat_title', f).val(''); |
---|
103 | $('#new_cat_parent', f).val('0'); |
---|
104 | } |
---|
105 | |
---|
106 | $('fieldset', f).prepend(msg); |
---|
107 | }); |
---|
108 | } |
---|
109 | |
---|
110 | const f = $('#quick-entry'); |
---|
111 | if (f.length > 0) { |
---|
112 | if ($.isFunction(jsToolBar)) { |
---|
113 | var contentTb = new jsToolBar($('#post_content', f)[0]); |
---|
114 | contentTb.switchMode($('#post_format', f).val()); |
---|
115 | } |
---|
116 | |
---|
117 | $('input[name=save]', f).click(function() { |
---|
118 | quickPost(f, -2); |
---|
119 | return false; |
---|
120 | }); |
---|
121 | |
---|
122 | if ($('input[name=save-publish]', f).length > 0) { |
---|
123 | var btn = $('<input type="submit" value="' + $('input[name=save-publish]', f).val() + '" />'); |
---|
124 | $('input[name=save-publish]', f).remove(); |
---|
125 | $('input[name=save]', f).after(btn).after(' '); |
---|
126 | btn.click(function() { |
---|
127 | quickPost(f, 1); |
---|
128 | return false; |
---|
129 | }); |
---|
130 | } |
---|
131 | |
---|
132 | $('#new_cat').toggleWithLegend($('#new_cat').parent().children().not('#new_cat'), { |
---|
133 | // no cookie on new category as we don't use this every day |
---|
134 | legend_click: true |
---|
135 | }); |
---|
136 | } |
---|
137 | |
---|
138 | // allow to hide quick entry div, and remember choice |
---|
139 | $('#quick h3').toggleWithLegend($('#quick').children().not('h3'), { |
---|
140 | legend_click: true, |
---|
141 | user_pref: 'dcx_quick_entry' |
---|
142 | }); |
---|
143 | |
---|
144 | // check if core update available |
---|
145 | let params = { |
---|
146 | f: 'checkCoreUpdate', |
---|
147 | xd_check: dotclear.nonce |
---|
148 | }; |
---|
149 | $.post('services.php', params, function(data) { |
---|
150 | if ($('rsp[status=failed]', data).length > 0) { |
---|
151 | // Silently fail as a forced checked my be done with admin update page |
---|
152 | } else { |
---|
153 | if ($('rsp>update', data).attr('check') == 1) { |
---|
154 | // Something has to be displayed |
---|
155 | const xml = $('rsp>update', data).attr('ret'); |
---|
156 | $('#content h2').after(xml); |
---|
157 | // manage outgoing links |
---|
158 | dotclear.outgoingLinks('#ajax-update a'); |
---|
159 | } |
---|
160 | } |
---|
161 | }); |
---|
162 | |
---|
163 | // check if some news are available |
---|
164 | params = { |
---|
165 | f: 'checkNewsUpdate', |
---|
166 | xd_check: dotclear.nonce |
---|
167 | }; |
---|
168 | $.post('services.php', params, function(data) { |
---|
169 | if ($('rsp[status=failed]', data).length > 0) { |
---|
170 | // Silently fail |
---|
171 | } else { |
---|
172 | if ($('rsp>news', data).attr('check') == 1) { |
---|
173 | // Something has to be displayed |
---|
174 | const xml = $('rsp>news', data).attr('ret'); |
---|
175 | if ($('#dashboard-boxes').length == 0) { |
---|
176 | // Create the #dashboard-boxes container |
---|
177 | $('#dashboard-main').append('<div id="dashboard-boxes"></div>'); |
---|
178 | } |
---|
179 | if ($('#dashboard-boxes div.db-items').length == 0) { |
---|
180 | // Create the #dashboard-boxes div.db-items container |
---|
181 | $('#dashboard-boxes').prepend('<div class="db-items"></div>'); |
---|
182 | } |
---|
183 | $('#dashboard-boxes div.db-items').prepend(xml); |
---|
184 | // manage outgoing links |
---|
185 | dotclear.outgoingLinks('#ajax-news a'); |
---|
186 | } |
---|
187 | } |
---|
188 | }); |
---|
189 | |
---|
190 | // run counters' update on some dashboard icons |
---|
191 | // Comments (including everything) |
---|
192 | if ($('#dashboard-main #icons p a[href="comments.php"]').length) { |
---|
193 | // Icon exists on dashboard |
---|
194 | // First pass |
---|
195 | dotclear.dbCommentsCount(); |
---|
196 | // Then fired every 60 seconds (1 minute) |
---|
197 | dotclear.dbCommentsCount_Timer = setInterval(dotclear.dbCommentsCount, 60 * 1000); |
---|
198 | } |
---|
199 | // Posts |
---|
200 | if ($('#dashboard-main #icons p a[href="posts.php"]').length) { |
---|
201 | // Icon exists on dashboard |
---|
202 | // First pass |
---|
203 | dotclear.dbPostsCount(); |
---|
204 | // Then fired every 600 seconds (10 minutes) |
---|
205 | dotclear.dbPostsCount_Timer = setInterval(dotclear.dbCommentsPost, 600 * 1000); |
---|
206 | } |
---|
207 | |
---|
208 | if (!dotclear.noDragDrop) { |
---|
209 | // Dashboard boxes and their children are sortable |
---|
210 | const set_positions = function(sel, id) { |
---|
211 | const list = $(sel).sortable("toArray").join(); |
---|
212 | // Save positions (via services) for id |
---|
213 | const params = { |
---|
214 | f: 'setDashboardPositions', |
---|
215 | xd_check: dotclear.nonce, |
---|
216 | id: id, |
---|
217 | list: list |
---|
218 | }; |
---|
219 | $.post('services.php', params, function() {}); |
---|
220 | }; |
---|
221 | const init_positions = function(sel, id) { |
---|
222 | $(sel).sortable({ |
---|
223 | cursor: 'move', |
---|
224 | opacity: 0.5, |
---|
225 | delay: 200, |
---|
226 | distance: 10, |
---|
227 | tolerance: "pointer", |
---|
228 | update: function() { |
---|
229 | set_positions(sel, id); |
---|
230 | }, |
---|
231 | start: function() { |
---|
232 | $(sel).addClass('sortable-area'); |
---|
233 | }, |
---|
234 | stop: function() { |
---|
235 | $(sel).removeClass('sortable-area'); |
---|
236 | } |
---|
237 | }); |
---|
238 | }; |
---|
239 | const reset_positions = function(sel) { |
---|
240 | $(sel).sortable('destroy'); |
---|
241 | }; |
---|
242 | // List of sortable areas |
---|
243 | const areas = [ |
---|
244 | ['#dashboard-main', 'main_order'], |
---|
245 | ['#dashboard-boxes', 'boxes_order'], |
---|
246 | ['#db-items', 'boxes_items_order'], |
---|
247 | ['#db-contents', 'boxes_contents_order'] |
---|
248 | ]; |
---|
249 | // Set or reset sortable depending on #dragndrop checbkox value |
---|
250 | $('#dragndrop').click(function() { |
---|
251 | if ($(this).is(':checked')) { |
---|
252 | // Activate sorting feature |
---|
253 | areas.forEach(element => init_positions(element[0], element[1])); |
---|
254 | $(this).prop('title', dotclear.dragndrop_on); |
---|
255 | $('#dragndrop-label').text(dotclear.dragndrop_on); |
---|
256 | } else { |
---|
257 | // Deactivate sorting feature |
---|
258 | areas.forEach(element => reset_positions(element[0])); |
---|
259 | $(this).prop('title', dotclear.dragndrop_off); |
---|
260 | $('#dragndrop-label').text(dotclear.dragndrop_off); |
---|
261 | } |
---|
262 | }); |
---|
263 | } |
---|
264 | }); |
---|