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

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

load tags,themes,users,languages list in AJAX + move javascript code to external file

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