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