common.js (6923B)
1 function getURLVar(key) { 2 var value = []; 3 4 var query = String(document.location).split('?'); 5 6 if (query[1]) { 7 var part = query[1].split('&'); 8 9 for (i = 0; i < part.length; i++) { 10 var data = part[i].split('='); 11 12 if (data[0] && data[1]) { 13 value[data[0]] = data[1]; 14 } 15 } 16 17 if (value[key]) { 18 return value[key]; 19 } else { 20 return ''; 21 } 22 } 23 } 24 25 $(document).ready(function() { 26 //Form Submit for IE Browser 27 $('button[type=\'submit\']').on('click', function() { 28 $("form[id*='form-']").submit(); 29 }); 30 31 // Highlight any found errors 32 $('.text-danger').each(function() { 33 var element = $(this).parent().parent(); 34 35 if (element.hasClass('form-group')) { 36 element.addClass('has-error'); 37 } 38 }); 39 40 // tooltips on hover 41 $('[data-toggle=\'tooltip\']').tooltip({container: 'body', html: true}); 42 43 // Makes tooltips work on ajax generated content 44 $(document).ajaxStop(function() { 45 $('[data-toggle=\'tooltip\']').tooltip({container: 'body'}); 46 }); 47 48 // https://github.com/opencart/opencart/issues/2595 49 $.event.special.remove = { 50 remove: function(o) { 51 if (o.handler) { 52 o.handler.apply(this, arguments); 53 } 54 } 55 } 56 57 // tooltip remove 58 $('[data-toggle=\'tooltip\']').on('remove', function() { 59 $(this).tooltip('destroy'); 60 }); 61 62 // Tooltip remove fixed 63 $(document).on('click', '[data-toggle=\'tooltip\']', function(e) { 64 $('body > .tooltip').remove(); 65 }); 66 67 $('#button-menu').on('click', function(e) { 68 e.preventDefault(); 69 70 $('#column-left').toggleClass('active'); 71 }); 72 73 // Set last page opened on the menu 74 $('#menu a[href]').on('click', function() { 75 sessionStorage.setItem('menu', $(this).attr('href')); 76 }); 77 78 if (!sessionStorage.getItem('menu')) { 79 $('#menu #dashboard').addClass('active'); 80 } else { 81 // Sets active and open to selected page in the left column menu. 82 $('#menu a[href=\'' + sessionStorage.getItem('menu') + '\']').parent().addClass('active'); 83 } 84 85 $('#menu a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('li > a').removeClass('collapsed'); 86 87 $('#menu a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('ul').addClass('in'); 88 89 $('#menu a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('li').addClass('active'); 90 91 // Image Manager 92 $(document).on('click', 'a[data-toggle=\'image\']', function(e) { 93 var $element = $(this); 94 var $popover = $element.data('bs.popover'); // element has bs popover? 95 96 e.preventDefault(); 97 98 // destroy all image popovers 99 $('a[data-toggle="image"]').popover('destroy'); 100 101 // remove flickering (do not re-add popover when clicking for removal) 102 if ($popover) { 103 return; 104 } 105 106 $element.popover({ 107 html: true, 108 placement: 'right', 109 trigger: 'manual', 110 content: function() { 111 return '<button type="button" id="button-image" class="btn btn-primary"><i class="fa fa-pencil"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>'; 112 } 113 }); 114 115 $element.popover('show'); 116 117 $('#button-image').on('click', function() { 118 var $button = $(this); 119 var $icon = $button.find('> i'); 120 121 $('#modal-image').remove(); 122 123 $.ajax({ 124 url: 'index.php?route=common/filemanager&user_token=' + getURLVar('user_token') + '&target=' + $element.parent().find('input').attr('id') + '&thumb=' + $element.attr('id'), 125 dataType: 'html', 126 beforeSend: function() { 127 $button.prop('disabled', true); 128 if ($icon.length) { 129 $icon.attr('class', 'fa fa-circle-o-notch fa-spin'); 130 } 131 }, 132 complete: function() { 133 $button.prop('disabled', false); 134 135 if ($icon.length) { 136 $icon.attr('class', 'fa fa-pencil'); 137 } 138 }, 139 success: function(html) { 140 $('body').append('<div id="modal-image" class="modal">' + html + '</div>'); 141 142 $('#modal-image').modal('show'); 143 } 144 }); 145 146 $element.popover('destroy'); 147 }); 148 149 $('#button-clear').on('click', function() { 150 $element.find('img').attr('src', $element.find('img').attr('data-placeholder')); 151 152 $element.parent().find('input').val(''); 153 154 $element.popover('destroy'); 155 }); 156 }); 157 }); 158 159 // Autocomplete */ 160 (function($) { 161 $.fn.autocomplete = function(option) { 162 return this.each(function() { 163 var $this = $(this); 164 var $dropdown = $('<ul class="dropdown-menu" />'); 165 166 this.timer = null; 167 this.items = []; 168 169 $.extend(this, option); 170 171 $this.attr('autocomplete', 'off'); 172 173 // Focus 174 $this.on('focus', function() { 175 this.request(); 176 }); 177 178 // Blur 179 $this.on('blur', function() { 180 setTimeout(function(object) { 181 object.hide(); 182 }, 200, this); 183 }); 184 185 // Keydown 186 $this.on('keydown', function(event) { 187 switch(event.keyCode) { 188 case 27: // escape 189 this.hide(); 190 break; 191 default: 192 this.request(); 193 break; 194 } 195 }); 196 197 // Click 198 this.click = function(event) { 199 event.preventDefault(); 200 201 var value = $(event.target).parent().attr('data-value'); 202 203 if (value && this.items[value]) { 204 this.select(this.items[value]); 205 } 206 } 207 208 // Show 209 this.show = function() { 210 var pos = $this.position(); 211 212 $dropdown.css({ 213 top: pos.top + $this.outerHeight(), 214 left: pos.left 215 }); 216 217 $dropdown.show(); 218 } 219 220 // Hide 221 this.hide = function() { 222 $dropdown.hide(); 223 } 224 225 // Request 226 this.request = function() { 227 clearTimeout(this.timer); 228 229 this.timer = setTimeout(function(object) { 230 object.source($(object).val(), $.proxy(object.response, object)); 231 }, 200, this); 232 } 233 234 // Response 235 this.response = function(json) { 236 var html = ''; 237 var category = {}; 238 var name; 239 var i = 0, j = 0; 240 241 if (json.length) { 242 for (i = 0; i < json.length; i++) { 243 // update element items 244 this.items[json[i]['value']] = json[i]; 245 246 if (!json[i]['category']) { 247 // ungrouped items 248 html += '<li data-value="' + json[i]['value'] + '"><a href="#">' + json[i]['label'] + '</a></li>'; 249 } else { 250 // grouped items 251 name = json[i]['category']; 252 if (!category[name]) { 253 category[name] = []; 254 } 255 256 category[name].push(json[i]); 257 } 258 } 259 260 for (name in category) { 261 html += '<li class="dropdown-header">' + name + '</li>'; 262 263 for (j = 0; j < category[name].length; j++) { 264 html += '<li data-value="' + category[name][j]['value'] + '"><a href="#"> ' + category[name][j]['label'] + '</a></li>'; 265 } 266 } 267 } 268 269 if (html) { 270 this.show(); 271 } else { 272 this.hide(); 273 } 274 275 $dropdown.html(html); 276 } 277 278 $dropdown.on('click', '> li > a', $.proxy(this.click, this)); 279 $this.after($dropdown); 280 }); 281 } 282 })(window.jQuery);