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

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

add "Home" (admin) link, hide <select> until they are populated

File size: 8.5 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() {
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    setTimeout(function() {
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    }, 1000);
258
259    $ato_edit.find('.close-edit').on('click', function(e) {
260      $.colorbox.close()
261      e.preventDefault();
262    });
263
264    if (is_picture) {
265      $ato_edit.find('.datepicker').datepicker({
266        dateFormat: 'yy-mm-dd'
267      });
268    }
269
270    $(".edit-quick").colorbox({
271      inline: true,
272      transition: 'none',
273      width: 500,
274      maxWidth: '100%',
275      top: 50,
276      title: $ato_edit.attr('title'),
277
278      onOpen: function() {
279        if (!is_picture) return;
280
281        // fetch tags list on first open
282        if ($(this).data('tags-init')) return;
283
284        $.ajax({
285          method: 'POST',
286          url: __this.urlWS + 'pwg.tags.getList',
287          dataType: 'json',
288          success: function(data) {
289            var tags = [];
290            // convert to custom format
291            for (var i=0, l=data.result.tags.length; i<l; i++) {
292              tags.push({
293                id: '~~'+ data.result.tags[i].id +'~~',
294                name: data.result.tags[i].name
295              });
296            }
297
298            $ato_edit.find('.tags').tokenInput(
299              tags,
300              $.extend({
301                animateDropdown: false,
302                preventDuplicates: true,
303                allowFreeTagging: true
304              }, tokeninput_lang)
305            );
306
307            $.colorbox.resize();
308            $(this).data('tags-init', true);
309          },
310          error: function(xhr, text, error) {
311            alert(text + ' ' + error);
312          }
313        });
314      }
315    });
316  };
317
318  return this;
319}(jQuery);
Note: See TracBrowser for help on using the repository browser.