Newer
Older
* @module package/quiqqer/coupons/bin/backend/controls/Manager
define('package/quiqqer/coupons/bin/backend/controls/Manager', [
'qui/controls/desktop/Panel',
'qui/controls/loader/Loader',
'qui/controls/windows/Popup',
'qui/controls/windows/Confirm',
'qui/controls/buttons/Button',
'qui/controls/buttons/Separator',
'controls/grid/Grid',
'qui/utils/Form',
'package/quiqqer/coupons/bin/backend/CouponCodes',
'text!package/quiqqer/coupons/bin/backend/controls/Manager.html',
'text!package/quiqqer/coupons/bin/backend/controls/Manager.Create.html',
'text!package/quiqqer/coupons/bin/backend/controls/Manager.Usages.html',
'css!package/quiqqer/coupons/bin/backend/controls/Manager.css'
], function (QUI, QUIPanel, QUILoader, QUIPopup, QUIConfirm, QUIButton, QUISeparator,
Grid, QUIFormUtils, CouponCodes, QUILocale, Mustache, template, templateCreate,
templateUsages) {
"use strict";
var lg = 'quiqqer/coupons';
return new Class({
Extends: QUIPanel,
Type : 'package/quiqqer/coupons/bin/backend/controls/Manager',
Binds: [
'$onCreate',
'$onResize',
'$listRefresh',
'$onRefresh',
'$load',
'$setGridData',
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
'$toggleActiveStatus',
'$managePackages',
'$delete',
'$editBundle',
'refresh',
'$openUserPanel',
'$sendMail'
],
options: {
title: QUILocale.get(lg, 'controls.manager.title')
},
initialize: function (options) {
this.parent(options);
this.Loader = new QUILoader();
this.$User = null;
this.$Grid = null;
this.$GridParent = null;
this.$Panel = null;
this.addEvents({
onCreate : this.$onCreate,
onRefresh: this.$onRefresh,
onResize : this.$onResize
});
},
/**
* Event: onCreate
*/
$onCreate: function () {
var self = this;
this.Loader.inject(this.$Elm);
this.addButton({
name : 'create',
text : QUILocale.get(lg, 'controls.manager.tbl.btn.create'),
textimage: 'fa fa-plus',
events : {
onClick: function () {
name : 'edit',
text : QUILocale.get(lg, 'controls.manager.tbl.btn.edit'),
textimage: 'fa fa-edit',
self.$showDetails(Object.clone(self.$Grid.getSelectedData()[0]));
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
}
}
});
this.addButton({
name : 'delete',
text : QUILocale.get(lg, 'controls.manager.tbl.btn.delete'),
textimage: 'fa fa-trash',
events : {
onClick: this.$delete
}
});
this.$load();
},
/**
* Refresh data
*/
refresh: function () {
if (this.$Grid) {
this.$Grid.refresh();
}
},
/**
* event: onResize
*/
$onResize: function () {
if (this.$GridParent && this.$Grid) {
var size = this.$GridParent.getSize();
this.$Grid.setHeight(size.y);
this.$Grid.resize();
}
},
/**
* Load Grid
*/
$load: function () {
var self = this;
this.setContent(Mustache.render(template));
var Content = this.getContent();
this.$GridParent = Content.getElement(
);
this.$Grid = new Grid(this.$GridParent, {
columnModel : [{
header : QUILocale.get('quiqqer/system', 'id'),
dataIndex: 'id',
dataType : 'number',
width : 50
}, {
header : QUILocale.get(lg, 'controls.manager.tbl.header.code'),
dataIndex: 'code',
dataType : 'string',
width : 150
}, {
header : QUILocale.get(lg, 'controls.manager.tbl.header.title'),
dataIndex: 'title',
dataType : 'string',
width : 200
}, {
header : QUILocale.get(lg, 'controls.manager.tbl.header.status'),
dataIndex: 'status',
dataType : 'node',
}, {
header : QUILocale.get(lg, 'controls.manager.tbl.header.validUntilDate'),
}, {
header : QUILocale.get(lg, 'controls.manager.tbl.header.reusable'),
}, {
header : QUILocale.get(lg, 'controls.manager.tbl.header.createDate'),
dataIndex: 'createDate',
dataType : 'string',
width : 150
}],
pagination : true,
serverSort : true,
selectable : true,
multipleSelection: true
});
this.$Grid.addEvents({
onDblClick: function () {
self.$showDetails(Object.clone(self.$Grid.getSelectedData()[0]));
},
onClick : function (event) {
var selected = self.$Grid.getSelectedData();
self.getButtons('delete').enable();
if (selected.length === 1) {
self.getButtons('edit').enable();
} else {
self.getButtons('edit').disable();
}
if (!event.cell.hasClass('clickable')) {
return;
}
var Row = selected[0];
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
},
onRefresh : this.$listRefresh
});
this.resize();
this.$Grid.refresh();
},
/**
* Event: onRefresh
*/
$onRefresh: function () {
if (this.$Grid) {
this.$Grid.refresh();
}
},
/**
* Refresh bundle list
*
* @param {Object} Grid
*/
$listRefresh: function (Grid) {
if (!this.$Grid) {
return;
}
var self = this;
self.getButtons('delete').disable();
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
var GridParams = {
sortOn : Grid.getAttribute('sortOn'),
sortBy : Grid.getAttribute('sortBy'),
perPage: Grid.getAttribute('perPage'),
page : Grid.getAttribute('page')
};
switch (GridParams.sortOn) {
case 'status':
GridParams.sortOn = 'useDate';
break;
case 'user':
GridParams.sortOn = 'userId';
break;
}
this.Loader.show();
CouponCodes.getList(GridParams).then(function (ResultData) {
self.Loader.hide();
self.$setGridData(ResultData);
});
},
/**
* Set license data to grid
*
* @param {Object} GridData
*/
$setGridData: function (GridData) {
var textUnlimited = QUILocale.get(lg, 'controls.manager.tbl.validUntil.unlimited');
for (var i = 0, len = GridData.data.length; i < len; i++) {
var Row = GridData.data[i];
var StatusElm = new Element('span', {
var usageCount = Row.usages.length;
if (!Row.isValid) {
StatusElm.set('html', QUILocale.get(lg, 'controls.manager.tbl.status.invalid', {
usageCount: usageCount
}));
StatusElm.addClass('quiqqer-coupons-manager-tbl-status-invalid');
StatusElm.set('html', QUILocale.get(lg, 'controls.manager.tbl.status.valid', {
usageCount: usageCount
StatusElm.addClass('quiqqer-coupons-manager-tbl-status-used');
}
Row.status = StatusElm;
if (!Row.validUntilDate) {
Row.validUntilDateText = textUnlimited;
} else {
Row.validUntilDateText = Row.validUntilDate;
* Create new CouponCode or edit an existing one
* @param {Object} [CouponData] - If omitted create new CouponCode
var FuncSubmit = function () {
var Content = Popup.getContent();
var Form = Content.getElement('form');
Popup.Loader.show();
if (CouponData) {
CouponCodes.edit(CouponData.id, QUIFormUtils.getFormData(Form)).then(function (couponCodeId) {
if (!couponCodeId) {
Popup.Loader.hide();
return;
}
self.refresh();
Popup.close();
});
return;
}
CouponCodes.create(QUIFormUtils.getFormData(Form)).then(function (couponCodeId) {
if (!couponCodeId) {
Popup.Loader.hide();
return;
}
self.refresh();
Popup.close();
});
};
// open popup
var lgPrefix = 'controls.manager.create.template.';
var Popup = new QUIPopup({
icon : 'fa fa-plus',
title : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.title_edit', {code: CouponData.code}) :
QUILocale.get(lg, 'controls.manager.details.popup.title_new'),
maxHeight : 335,
var Content = Popup.getContent();
var Form = Content.getElement('form');
var SubmitBtn = Popup.getButton('submit');
Form.addEvent('submit', function (event) {
event.stop();
FuncSubmit();
});
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
var Settings = Content.getElement('.quiqqer-coupons-manager-create-settings');
var Amount = Content.getElement('input[name="amount"]');
var Code = Content.getElement('input[name="code"]');
Code.addEvent('keyup', function () {
if (Code.value !== '') {
Amount.disabled = true;
Amount.value = 1;
} else {
Amount.disabled = false;
}
});
new QUIButton({
textimage: 'fa fa-cogs',
text : QUILocale.get(lg, 'controls.Manager.create.settings_btn.show'),
events : {
onClick: function (Btn) {
if (Settings.getStyle('display') === 'none') {
Settings.setStyle('display', 'block');
Btn.setAttribute(
'text',
QUILocale.get(lg, 'controls.Manager.create.settings_btn.hide')
);
Content.getElement('input[name="title"]').focus();
Popup.setAttribute('maxHeight', 850);
Popup.resize();
} else {
Settings.setStyle('display', 'none');
Btn.setAttribute(
'text',
QUILocale.get(lg, 'controls.Manager.create.settings_btn.show')
);
Popup.setAttribute('maxHeight', 335);
Popup.resize();
}
}
}
}).inject(Content.getElement(
'.quiqqer-coupons-manager-create-settings-btn'
));
if (CouponData) {
CouponData.discountIds = CouponData.discountIds.join(',');
QUIFormUtils.setDataToForm(CouponData, Form);
}
var DiscountSelect = QUI.Controls.getById(
Content.getElement('input[name="discountIds"]').get('data-quiid')
);
DiscountSelect.addEvents({
onChange: function () {
if (DiscountSelect.getValue() === '') {
SubmitBtn.disable();
} else {
SubmitBtn.enable();
}
}
});
});
}
},
closeButton: true,
content : Mustache.render(templateCreate, {
labelTitle : QUILocale.get(lg, lgPrefix + 'labelTitle'),
labelCode : QUILocale.get(lg, lgPrefix + 'labelCode'),
labelUsers : QUILocale.get(lg, lgPrefix + 'labelUsers'),
labelGroups : QUILocale.get(lg, lgPrefix + 'labelGroups'),
labelReusable: QUILocale.get(lg, lgPrefix + 'labelReusable'),
labelDiscount: QUILocale.get(lg, lgPrefix + 'labelDiscount')
})
});
Popup.open();
Popup.addButton(new QUIButton({
name : 'submit',
disabled: true,
text : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_text_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_text_new'),
alt : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_new'),
title : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_new'),
events : {
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
onClick: FuncSubmit
}
}));
},
/**
* Remove all selected licenses
*/
$delete: function () {
var self = this;
var deleteData = [];
var deleteIds = [];
var rows = this.$Grid.getSelectedData();
for (var i = 0, len = rows.length; i < len; i++) {
deleteData.push(
rows[i].title + ' (ID: #' + rows[i].id + ')'
);
deleteIds.push(rows[i].id);
}
// open popup
var Popup = new QUIConfirm({
'maxHeight': 300,
'autoclose': false,
'information': QUILocale.get(
lg,
'controls.manager.delete.popup.info', {
codes: deleteData.join('<br/>')
}
),
'title' : QUILocale.get(lg, 'controls.manager.delete.popup.title'),
'texticon' : 'fa fa-trash',
text : QUILocale.get(lg, 'controls.manager.delete.popup.title'),
'icon' : 'fa fa-trash',
cancel_button: {
text : false,
textimage: 'icon-remove fa fa-remove'
},
ok_button : {
text : false,
textimage: 'icon-ok fa fa-check'
},
events : {
onSubmit: function () {
Popup.Loader.show();
CouponCodes.delete(deleteIds).then(function (success) {
if (!success) {
Popup.Loader.hide();
return;
}
Popup.close();
self.refresh();
});
}
}
});
Popup.open();
},
/**
* Open user panel
*
$showUsages: function (RowData) {
new QUIConfirm({
maxHeight: 550,
maxWidth : 400,
autoclose: false,
title: QUILocale.get(lg, 'controls.Manager.usages.title', {code: RowData.code}),
icon : 'fa fa-user',
cancel_button: false,
ok_button : {
text : QUILocale.get(lg, 'controls.Manager.usages.btn_ok'),
onSubmit: function (Popup) {
Popup.close();
},
onOpen : function (Popup) {
var lgPrefix = 'controls.Manager.usages.template.';
Popup.setContent(Mustache.render(templateUsages, {
headerUser: QUILocale.get(lg, lgPrefix + 'headerUser'),
headerDate: QUILocale.get(lg, lgPrefix + 'headerDate')
}));
var Content = Popup.getContent();
var TableBody = Content.getElement('tbody');
for (var i = 0, len = RowData.usages.length; i < len; i++) {
var usage = RowData.usages[i];
new Element('tr', {
html: '<td>' + usage.date + '</td>' +
'<td>' + usage.userId + ' (' + usage.userName + ')</td>'
}).inject(TableBody);
}
}
}
}).open();