[25817] | 1 | var AdminTools = function($) { |
---|
| 2 | var __this = this; |
---|
| 3 | |
---|
| 4 | this.urlWS; |
---|
[26061] | 5 | this.urlSelf; |
---|
[25817] | 6 | this.multiView; |
---|
| 7 | |
---|
| 8 | var $ato = $('#ato_header'), |
---|
[25979] | 9 | $ato_closed = $('#ato_header_closed'), |
---|
| 10 | ato_height = 28; // normal height, real height conputed on init() |
---|
[25817] | 11 | |
---|
| 12 | // move to whole page down or up |
---|
| 13 | function moveBody(dir, anim) { |
---|
[25979] | 14 | var operator = dir=='show' ? '+=' : '-='; |
---|
[25817] | 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 |
---|
[26603] | 34 | function populateMultiView() { |
---|
| 35 | var $multiview = $ato.find('.multiview'); |
---|
[25817] | 36 | |
---|
[26603] | 37 | if ($multiview.data('init')) return; |
---|
| 38 | |
---|
[25817] | 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 | }); |
---|
[26603] | 44 | $multiview.find('select[data-type="view_as"]').html(html) |
---|
[25817] | 45 | .val(__this.multiView.view_as); |
---|
| 46 | |
---|
| 47 | html = ''; |
---|
| 48 | $.each(data.themes, function(i, theme) { |
---|
| 49 | html+= '<option value="'+ theme +'">'+ theme +'</option>'; |
---|
| 50 | }); |
---|
[26603] | 51 | $multiview.find('select[data-type="theme"]').html(html) |
---|
[25817] | 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 | }); |
---|
[26603] | 58 | $multiview.find('select[data-type="lang"]').html(html) |
---|
[25817] | 59 | .val(__this.multiView.lang); |
---|
| 60 | |
---|
[26603] | 61 | $multiview.data('init', true); |
---|
| 62 | |
---|
| 63 | $multiview.find('.switcher').show(); |
---|
[25817] | 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 | |
---|
[25818] | 87 | // delete session cache |
---|
| 88 | this.deleteCache = function() { |
---|
| 89 | if ('sessionStorage' in window) { |
---|
| 90 | window.sessionStorage.removeItem('multiView'); |
---|
| 91 | } |
---|
| 92 | }; |
---|
| 93 | |
---|
[25817] | 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 |
---|
[26061] | 108 | this.init = function(open) { |
---|
[25817] | 109 | $('body').prepend($ato); // ensure the bar is at the begining |
---|
[25979] | 110 | |
---|
| 111 | $ato.show(); |
---|
| 112 | ato_height = $ato.height(); |
---|
[25817] | 113 | |
---|
| 114 | if ('localStorage' in window) { |
---|
| 115 | if (window.localStorage.ato_panel_open == null) { |
---|
[26061] | 116 | window.localStorage.ato_panel_open = open; |
---|
[25817] | 117 | } |
---|
| 118 | |
---|
[26061] | 119 | if (window.localStorage.ato_panel_open == 1) { |
---|
[25817] | 120 | moveBody('show', false); |
---|
| 121 | } |
---|
| 122 | else { |
---|
[25979] | 123 | $ato.hide(); |
---|
[25817] | 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')) { |
---|
[26603] | 136 | populateMultiView(); |
---|
[25817] | 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() { |
---|
[26061] | 158 | window.location.href = __this.urlSelf + 'ato_'+ $(this).data('type') +'='+ $(this).val(); |
---|
[25817] | 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 | |
---|
[26061] | 171 | if ('localStorage' in window) window.localStorage.ato_panel_open = 0; |
---|
[25817] | 172 | e.preventDefault(); |
---|
| 173 | }); |
---|
| 174 | |
---|
| 175 | $ato_closed.on('click', function(e) { |
---|
| 176 | $ato.slideDown(); |
---|
| 177 | $ato_closed.slideUp(); |
---|
| 178 | moveBody('show', true); |
---|
| 179 | |
---|
[26061] | 180 | if ('localStorage' in window) window.localStorage.ato_panel_open = 1; |
---|
[25817] | 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() { |
---|
[25839] | 200 | $ato.find('.saved').fadeIn(200).delay(1600).fadeOut(200); |
---|
[25817] | 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() { |
---|
[25839] | 226 | $ato.find('.saved').fadeIn(200).delay(1600).fadeOut(200); |
---|
[25817] | 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 |
---|
[25839] | 239 | this.initQuickEdit = function(is_picture, tokeninput_lang) { |
---|
[25817] | 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 | |
---|
[25839] | 264 | if (is_picture) { |
---|
[25817] | 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, |
---|
[25979] | 274 | maxWidth: '100%', |
---|
[25817] | 275 | top: 50, |
---|
| 276 | title: $ato_edit.attr('title'), |
---|
| 277 | |
---|
| 278 | onOpen: function() { |
---|
[25839] | 279 | if (!is_picture) return; |
---|
| 280 | |
---|
[25817] | 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); |
---|