source: extensions/AdminTools/template/public_controller.js @ 26061

Last change on this file since 26061 was 26061, checked in by mistic100, 10 years ago

add configuration page

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