1 | var AdminTools = function($) { |
---|
2 | var __this = this; |
---|
3 | |
---|
4 | this.urlWS; |
---|
5 | this.urlSelf; |
---|
6 | this.multiView; |
---|
7 | |
---|
8 | var $ato = $('#ato_header'), |
---|
9 | $ato_closed = $('#ato_header_closed'), |
---|
10 | ato_height = 28; // normal height, real height conputed on init() |
---|
11 | |
---|
12 | // move to whole page down or up |
---|
13 | function moveBody(dir, anim) { |
---|
14 | var operator = dir=='show' ? '+=' : '-='; |
---|
15 | |
---|
16 | if (anim) { |
---|
17 | $('body').animate({'margin-top': operator+ato_height}); |
---|
18 | |
---|
19 | if ($('#the_page, [data-role="page"]').css('position')=='absolute') { |
---|
20 | $('#the_page, [data-role="page"]').animate({'top': operator+ato_height}); |
---|
21 | } |
---|
22 | } |
---|
23 | else { |
---|
24 | $('body').css({'margin-top': operator+ato_height}); |
---|
25 | |
---|
26 | if ($('#the_page, [data-role="page"]').css('position')=='absolute') { |
---|
27 | $('#the_page, [data-role="page"]').css({'top': operator+ato_height}); |
---|
28 | } |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | // fill multiview selects |
---|
33 | // data came from AJAX request or sessionStorage |
---|
34 | function populateMultiView() { |
---|
35 | var $multiview = $ato.find('.multiview'); |
---|
36 | |
---|
37 | if ($multiview.data('init')) return; |
---|
38 | |
---|
39 | var render = function(data) { |
---|
40 | var html = ''; |
---|
41 | $.each(data.users, function(i, user) { |
---|
42 | html+= '<option value="'+ user.id +'">'+ user.username +'</option>'; |
---|
43 | }); |
---|
44 | $multiview.find('select[data-type="view_as"]').html(html) |
---|
45 | .val(__this.multiView.view_as); |
---|
46 | |
---|
47 | html = ''; |
---|
48 | $.each(data.themes, function(i, theme) { |
---|
49 | html+= '<option value="'+ theme +'">'+ theme +'</option>'; |
---|
50 | }); |
---|
51 | $multiview.find('select[data-type="theme"]').html(html) |
---|
52 | .val(__this.multiView.theme); |
---|
53 | |
---|
54 | html = ''; |
---|
55 | $.each(data.languages, function(i, language) { |
---|
56 | html+= '<option value="'+ language.id +'">'+ language.name +'</option>'; |
---|
57 | }); |
---|
58 | $multiview.find('select[data-type="lang"]').html(html) |
---|
59 | .val(__this.multiView.lang); |
---|
60 | |
---|
61 | $multiview.data('init', true); |
---|
62 | |
---|
63 | $multiview.find('.switcher').show(); |
---|
64 | }; |
---|
65 | |
---|
66 | if ('sessionStorage' in window && window.sessionStorage.multiView != undefined) { |
---|
67 | render(JSON.parse(window.sessionStorage.multiView)); |
---|
68 | } |
---|
69 | else { |
---|
70 | $.ajax({ |
---|
71 | method: 'POST', |
---|
72 | url: __this.urlWS + 'multiView.getData', |
---|
73 | dataType: 'json', |
---|
74 | success: function(data) { |
---|
75 | render(data.result); |
---|
76 | if ('sessionStorage' in window) { |
---|
77 | window.sessionStorage.multiView = JSON.stringify(data.result); |
---|
78 | } |
---|
79 | }, |
---|
80 | error: function(xhr, text, error) { |
---|
81 | alert(text + ' ' + error); |
---|
82 | } |
---|
83 | }); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | // delete session cache |
---|
88 | this.deleteCache = function() { |
---|
89 | if ('sessionStorage' in window) { |
---|
90 | window.sessionStorage.removeItem('multiView'); |
---|
91 | } |
---|
92 | }; |
---|
93 | |
---|
94 | // move close button to smartpocket toolbar |
---|
95 | this.initMobile = function() { |
---|
96 | var $headerbar = $('div[data-role="header"] .title'); |
---|
97 | if ($headerbar.length == 1) { |
---|
98 | $ato_closed.addClass('smartpocket'); |
---|
99 | $ato_closed.find('a').attr({ |
---|
100 | 'data-iconpos':'notext', |
---|
101 | 'data-role':'button' |
---|
102 | }); |
---|
103 | $headerbar.prepend($ato_closed); |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | // attach jquery handlers |
---|
108 | this.init = function(open) { |
---|
109 | $('body').prepend($ato); // ensure the bar is at the begining |
---|
110 | |
---|
111 | $ato.show(); |
---|
112 | ato_height = $ato.height(); |
---|
113 | |
---|
114 | if ('localStorage' in window) { |
---|
115 | if (window.localStorage.ato_panel_open == null) { |
---|
116 | window.localStorage.ato_panel_open = open; |
---|
117 | } |
---|
118 | |
---|
119 | if (window.localStorage.ato_panel_open == 1) { |
---|
120 | moveBody('show', false); |
---|
121 | } |
---|
122 | else { |
---|
123 | $ato.hide(); |
---|
124 | $ato_closed.show(); |
---|
125 | } |
---|
126 | } |
---|
127 | else { |
---|
128 | $ato.show(); |
---|
129 | moveBody('show', false); |
---|
130 | } |
---|
131 | |
---|
132 | /* <!-- sub menus --> */ |
---|
133 | $ato.find('.parent').on({ |
---|
134 | 'click': function() { |
---|
135 | if ($(this).hasClass('multiview')) { |
---|
136 | populateMultiView(); |
---|
137 | } |
---|
138 | $(this).find('ul').toggle(); |
---|
139 | }, |
---|
140 | 'mouseleave': function(e) { |
---|
141 | if (e.target.tagName.toLowerCase() != "select") { |
---|
142 | $(this).find('ul').hide(); |
---|
143 | } |
---|
144 | } |
---|
145 | }); |
---|
146 | $ato.find('.parent>a').on('click', function(e) { |
---|
147 | e.preventDefault(); |
---|
148 | }); |
---|
149 | $ato.find('.parent ul').on('mouseleave', function(e) { |
---|
150 | if (e.target.tagName.toLowerCase() != "select") { |
---|
151 | $(this).hide(); |
---|
152 | } |
---|
153 | }); |
---|
154 | |
---|
155 | /* <!-- select boxes --> */ |
---|
156 | $ato.find('.switcher').on({ |
---|
157 | 'change': function() { |
---|
158 | window.location.href = __this.urlSelf + 'ato_'+ $(this).data('type') +'='+ $(this).val(); |
---|
159 | }, |
---|
160 | 'click': function(e) { |
---|
161 | e.stopPropagation(); |
---|
162 | } |
---|
163 | }); |
---|
164 | |
---|
165 | /* <!-- toggle toolbar --> */ |
---|
166 | $ato.find('.close-panel').on('click', function(e) { |
---|
167 | $ato.slideUp(); |
---|
168 | $ato_closed.slideDown(); |
---|
169 | moveBody('hide', true); |
---|
170 | |
---|
171 | if ('localStorage' in window) window.localStorage.ato_panel_open = 0; |
---|
172 | e.preventDefault(); |
---|
173 | }); |
---|
174 | |
---|
175 | $ato_closed.on('click', function(e) { |
---|
176 | $ato.slideDown(); |
---|
177 | $ato_closed.slideUp(); |
---|
178 | moveBody('show', true); |
---|
179 | |
---|
180 | if ('localStorage' in window) window.localStorage.ato_panel_open = 1; |
---|
181 | e.preventDefault(); |
---|
182 | }); |
---|
183 | }; |
---|
184 | |
---|
185 | // init "set as representative" button |
---|
186 | this.initRepresentative = function(image_id, category_id) { |
---|
187 | $ato.find('.set-representative').on('click', function(e) { |
---|
188 | if (!$(this).parent().hasClass('disabled')) { |
---|
189 | $(this).parent().addClass('disabled') |
---|
190 | |
---|
191 | $.ajax({ |
---|
192 | method: 'POST', |
---|
193 | url: __this.urlWS + 'pwg.categories.setRepresentative', |
---|
194 | dataType: 'json', |
---|
195 | data: { |
---|
196 | image_id: image_id, |
---|
197 | category_id: category_id |
---|
198 | }, |
---|
199 | success: function() { |
---|
200 | $ato.find('.saved').fadeIn(200).delay(1600).fadeOut(200); |
---|
201 | }, |
---|
202 | error: function(xhr, text, error) { |
---|
203 | alert(text + ' ' + error); |
---|
204 | } |
---|
205 | }); |
---|
206 | } |
---|
207 | |
---|
208 | e.preventDefault(); |
---|
209 | }); |
---|
210 | }; |
---|
211 | |
---|
212 | // init "add to caddie" button |
---|
213 | this.initCaddie = function(image_id) { |
---|
214 | $ato.find('.add-caddie').on('click', function(e) { |
---|
215 | if (!$(this).parent().hasClass('disabled')) { |
---|
216 | $(this).parent().addClass('disabled') |
---|
217 | |
---|
218 | $.ajax({ |
---|
219 | method: 'POST', |
---|
220 | url: __this.urlWS + 'pwg.caddie.add', |
---|
221 | dataType: 'json', |
---|
222 | data: { |
---|
223 | image_id: image_id |
---|
224 | }, |
---|
225 | success: function() { |
---|
226 | $ato.find('.saved').fadeIn(200).delay(1600).fadeOut(200); |
---|
227 | }, |
---|
228 | error: function(xhr, text, error) { |
---|
229 | alert(text + ' ' + error); |
---|
230 | } |
---|
231 | }); |
---|
232 | } |
---|
233 | |
---|
234 | e.preventDefault(); |
---|
235 | }); |
---|
236 | }; |
---|
237 | |
---|
238 | // init "quick edit" popup |
---|
239 | this.initQuickEdit = function(is_picture, tokeninput_lang) { |
---|
240 | var $ato_edit = $('#ato_quick_edit'); |
---|
241 | |
---|
242 | // try to find background color matching text color |
---|
243 | // there is a 1s delay to wait for jQuery Mobile initialization |
---|
244 | function bgColor() { |
---|
245 | var bg_color = 'white'; |
---|
246 | var selectors = ['#the_page #content', '[data-role="page"]', 'body']; |
---|
247 | |
---|
248 | for (var i=0; i<selectors.length; i++) { |
---|
249 | var color = $(selectors[i]).css('background-color'); |
---|
250 | if (color && color!='transparent') { |
---|
251 | bg_color = color; |
---|
252 | break; |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | $ato_edit.css('background-color', bg_color); |
---|
257 | } |
---|
258 | |
---|
259 | $ato_edit.find('.close-edit').on('click', function(e) { |
---|
260 | $.colorbox.close() |
---|
261 | e.preventDefault(); |
---|
262 | }); |
---|
263 | |
---|
264 | $(".edit-quick").colorbox({ |
---|
265 | inline: true, |
---|
266 | transition: 'none', |
---|
267 | width: 500, |
---|
268 | maxWidth: '100%', |
---|
269 | top: 50, |
---|
270 | title: $ato_edit.attr('title'), |
---|
271 | |
---|
272 | onComplete: function() { |
---|
273 | setTimeout(function() { |
---|
274 | $('#quick_edit_name').focus(); |
---|
275 | }, 0); |
---|
276 | }, |
---|
277 | |
---|
278 | onOpen: function() { |
---|
279 | bgColor(); |
---|
280 | |
---|
281 | if (is_picture) { |
---|
282 | $ato_edit.find('.datepicker').datepicker({ |
---|
283 | dateFormat: 'yy-mm-dd' |
---|
284 | }); |
---|
285 | |
---|
286 | // fetch tags list on first open |
---|
287 | if ($(this).data('tags-init')) return; |
---|
288 | |
---|
289 | $.ajax({ |
---|
290 | method: 'POST', |
---|
291 | url: __this.urlWS + 'pwg.tags.getList', |
---|
292 | dataType: 'json', |
---|
293 | success: function(data) { |
---|
294 | var tags = []; |
---|
295 | // convert to custom format |
---|
296 | for (var i=0, l=data.result.tags.length; i<l; i++) { |
---|
297 | tags.push({ |
---|
298 | id: '~~'+ data.result.tags[i].id +'~~', |
---|
299 | name: data.result.tags[i].name |
---|
300 | }); |
---|
301 | } |
---|
302 | |
---|
303 | $ato_edit.find('.tags').tokenInput( |
---|
304 | tags, |
---|
305 | $.extend({ |
---|
306 | animateDropdown: false, |
---|
307 | preventDuplicates: true, |
---|
308 | allowFreeTagging: true |
---|
309 | }, tokeninput_lang) |
---|
310 | ); |
---|
311 | |
---|
312 | $.colorbox.resize(); |
---|
313 | $(this).data('tags-init', true); |
---|
314 | }, |
---|
315 | error: function(xhr, text, error) { |
---|
316 | alert(text + ' ' + error); |
---|
317 | } |
---|
318 | }); |
---|
319 | } |
---|
320 | } |
---|
321 | }); |
---|
322 | |
---|
323 | // Ctrl+E opens the quick edit |
---|
324 | Mousetrap.bind('mod+e', function(e) { |
---|
325 | e.preventDefault(); |
---|
326 | $(".edit-quick").click(); |
---|
327 | }); |
---|
328 | }; |
---|
329 | |
---|
330 | return this; |
---|
331 | }(jQuery); |
---|