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

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

remove useless SQL queries, don't change user on admin pages, fix theme and language overwrites, fix toolbar overlap, colorbox max width

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