common.js
9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
function getURLVar(key) {
var value = [];
var query = String(document.location).split('?');
if (query[1]) {
var part = query[1].split('&');
for (i = 0; i < part.length; i++) {
var data = part[i].split('=');
if (data[0] && data[1]) {
value[data[0]] = data[1];
}
}
if (value[key]) {
return value[key];
} else {
return '';
}
}
}
$(document).ready(function() {
//Form Submit for IE Browser
$('button[type=\'submit\']').on('click', function() {
$("form[id*='form-']").submit();
});
// Highlight any found errors
$('.text-danger').each(function() {
var element = $(this).parent().parent();
if (element.hasClass('form-group')) {
element.addClass('has-error');
}
});
// Set last page opened on the menu
$('#menu a[href]').on('click', function() {
sessionStorage.setItem('menu', $(this).attr('href'));
});
if (!sessionStorage.getItem('menu')) {
$('#menu #dashboard').addClass('active');
} else {
// Sets active and open to selected page in the left column menu.
$('#menu a[href=\'' + sessionStorage.getItem('menu') + '\']').parents('li').addClass('active open');
}
if (localStorage.getItem('column-left') == 'active') {
$('#button-menu i').replaceWith('<i class="fa fa-dedent fa-lg"></i>');
$('#column-left').addClass('active');
// Slide Down Menu
$('#menu li.active').has('ul').children('ul').addClass('collapse in');
$('#menu li').not('.active').has('ul').children('ul').addClass('collapse');
} else {
$('#button-menu i').replaceWith('<i class="fa fa-indent fa-lg"></i>');
$('#menu li li.active').has('ul').children('ul').addClass('collapse in');
$('#menu li li').not('.active').has('ul').children('ul').addClass('collapse');
}
// Menu button
$('#button-menu').on('click', function() {
// Checks if the left column is active or not.
if ($('#column-left').hasClass('active')) {
localStorage.setItem('column-left', '');
$('#button-menu i').replaceWith('<i class="fa fa-indent fa-lg"></i>');
$('#column-left').removeClass('active');
$('#menu > li > ul').removeClass('in collapse');
$('#menu > li > ul').removeAttr('style');
} else {
localStorage.setItem('column-left', 'active');
$('#button-menu i').replaceWith('<i class="fa fa-dedent fa-lg"></i>');
$('#column-left').addClass('active');
// Add the slide down to open menu items
$('#menu li.open').has('ul').children('ul').addClass('collapse in');
$('#menu li').not('.open').has('ul').children('ul').addClass('collapse');
}
});
// Menu
$('#menu').find('li').has('ul').children('a').on('click', function() {
if ($('#column-left').hasClass('active')) {
$(this).parent('li').toggleClass('open').children('ul').collapse('toggle');
$(this).parent('li').siblings().removeClass('open').children('ul.in').collapse('hide');
} else if (!$(this).parent().parent().is('#menu')) {
$(this).parent('li').toggleClass('open').children('ul').collapse('toggle');
$(this).parent('li').siblings().removeClass('open').children('ul.in').collapse('hide');
}
});
// Tooltip remove fixed
$(document).delegate('[data-toggle=\'tooltip\']', 'click', function(e) {
$('body > .tooltip').remove();
});
// Override summernotes image manager
$('.summernote').each(function() {
var element = this;
$(element).summernote({
disableDragAndDrop: true,
height: 300,
toolbar: [
['style', ['style']],
['font', ['bold', 'underline', 'clear']],
['fontname', ['fontname']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['table', ['table']],
['insert', ['link', 'image', 'video']],
['view', ['fullscreen', 'codeview', 'help']]
],
buttons: {
image: function() {
var ui = $.summernote.ui;
// create button
var button = ui.button({
contents: '<i class="fa fa-image" />',
tooltip: $.summernote.lang[$.summernote.options.lang].image.image,
click: function () {
$('#modal-image').remove();
$.ajax({
url: 'index.php?route=common/filemanager&token=' + getURLVar('token'),
dataType: 'html',
beforeSend: function() {
$('#button-image i').replaceWith('<i class="fa fa-circle-o-notch fa-spin"></i>');
$('#button-image').prop('disabled', true);
},
complete: function() {
$('#button-image i').replaceWith('<i class="fa fa-upload"></i>');
$('#button-image').prop('disabled', false);
},
success: function(html) {
$('body').append('<div id="modal-image" class="modal">' + html + '</div>');
$('#modal-image').modal('show');
$('#modal-image a.thumbnail').on('click', function(e) {
e.preventDefault();
$(element).summernote('insertImage', $(this).attr('href'));
$('#modal-image').modal('hide');
});
}
});
}
});
return button.render();
}
}
});
});
// Image Manager
$(document).delegate('a[data-toggle=\'image\']', 'click', function(e) {
e.preventDefault();
$('.popover').popover('hide', function() {
$('.popover').remove();
});
var element = this;
$(element).popover({
html: true,
placement: 'right',
trigger: 'manual',
content: function() {
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>';
}
});
$(element).popover('show');
$('#button-image').on('click', function() {
$('#modal-image').remove();
$.ajax({
url: 'index.php?route=common/filemanager&token=' + getURLVar('token') + '&target=' + $(element).parent().find('input').attr('id') + '&thumb=' + $(element).attr('id'),
dataType: 'html',
beforeSend: function() {
$('#button-image i').replaceWith('<i class="fa fa-circle-o-notch fa-spin"></i>');
$('#button-image').prop('disabled', true);
},
complete: function() {
$('#button-image i').replaceWith('<i class="fa fa-pencil"></i>');
$('#button-image').prop('disabled', false);
},
success: function(html) {
$('body').append('<div id="modal-image" class="modal">' + html + '</div>');
$('#modal-image').modal('show');
}
});
$(element).popover('hide', function() {
$('.popover').remove();
});
});
$('#button-clear').on('click', function() {
$(element).find('img').attr('src', $(element).find('img').attr('data-placeholder'));
$(element).parent().find('input').attr('value', '');
$(element).popover('hide', function() {
$('.popover').remove();
});
});
});
// tooltips on hover
$('[data-toggle=\'tooltip\']').tooltip({container: 'body', html: true});
// Makes tooltips work on ajax generated content
$(document).ajaxStop(function() {
$('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
});
// https://github.com/opencart/opencart/issues/2595
$.event.special.remove = {
remove: function(o) {
if (o.handler) {
o.handler.apply(this, arguments);
}
}
}
$('[data-toggle=\'tooltip\']').on('remove', function() {
$(this).tooltip('destroy');
});
});
// Autocomplete */
(function($) {
$.fn.autocomplete = function(option) {
return this.each(function() {
this.timer = null;
this.items = new Array();
$.extend(this, option);
$(this).attr('autocomplete', 'off');
// Focus
$(this).on('focus', function() {
this.request();
});
// Blur
$(this).on('blur', function() {
setTimeout(function(object) {
object.hide();
}, 200, this);
});
// Keydown
$(this).on('keydown', function(event) {
switch(event.keyCode) {
case 27: // escape
this.hide();
break;
default:
this.request();
break;
}
});
// Click
this.click = function(event) {
event.preventDefault();
value = $(event.target).parent().attr('data-value');
if (value && this.items[value]) {
this.select(this.items[value]);
}
}
// Show
this.show = function() {
var pos = $(this).position();
$(this).siblings('ul.dropdown-menu').css({
top: pos.top + $(this).outerHeight(),
left: pos.left
});
$(this).siblings('ul.dropdown-menu').show();
}
// Hide
this.hide = function() {
$(this).siblings('ul.dropdown-menu').hide();
}
// Request
this.request = function() {
clearTimeout(this.timer);
this.timer = setTimeout(function(object) {
object.source($(object).val(), $.proxy(object.response, object));
}, 200, this);
}
// Response
this.response = function(json) {
html = '';
if (json.length) {
for (i = 0; i < json.length; i++) {
this.items[json[i]['value']] = json[i];
}
for (i = 0; i < json.length; i++) {
if (!json[i]['category']) {
html += '<li data-value="' + json[i]['value'] + '"><a href="#">' + json[i]['label'] + '</a></li>';
}
}
// Get all the ones with a categories
var category = new Array();
for (i = 0; i < json.length; i++) {
if (json[i]['category']) {
if (!category[json[i]['category']]) {
category[json[i]['category']] = new Array();
category[json[i]['category']]['name'] = json[i]['category'];
category[json[i]['category']]['item'] = new Array();
}
category[json[i]['category']]['item'].push(json[i]);
}
}
for (i in category) {
html += '<li class="dropdown-header">' + category[i]['name'] + '</li>';
for (j = 0; j < category[i]['item'].length; j++) {
html += '<li data-value="' + category[i]['item'][j]['value'] + '"><a href="#"> ' + category[i]['item'][j]['label'] + '</a></li>';
}
}
}
if (html) {
this.show();
} else {
this.hide();
}
$(this).siblings('ul.dropdown-menu').html(html);
}
$(this).after('<ul class="dropdown-menu"></ul>');
$(this).siblings('ul.dropdown-menu').delegate('a', 'click', $.proxy(this.click, this));
});
}
})(window.jQuery);