var App_ECommerce_Order_Admin_List_ActionFormToolbar = qs.createObject(); App_ECommerce_Order_Admin_List_ActionFormToolbar.defaultOptions = { /** @type {Qs_ViewController_List} */ list: undefined, cellClasses: undefined, attribs: { id: undefined, 'class': undefined } }; App_ECommerce_Order_Admin_List_ActionFormToolbar.prototype = { initialize: function (options) { this.options = $.extend({}, App_ECommerce_Order_Admin_List_ActionFormToolbar.defaultOptions, options); if (!this.options.list || !this.options.list instanceof Qs_ViewController_List) { qs.debug.warn('App_ECommerce_Order_Admin_List_ActionFormToolbar: Incorrect option list ', this.options.list); return this; } this.element = {}; /** @type {Qs_ViewController_List} */ this.list = this.options.list; /** @type {jQuery} */ this.element.list = this.list.element.list; this.initElement(); return this; }, initElement: function () { var cellSelector = this.classList2Selector(this.options.cellClasses), switcher = this.element.list.find('th' + cellSelector + ' :checkbox'), checkboxes = this.element.list.find('td' + cellSelector + ' :checkbox'); this.element.toolbar = $('#' + this.options.attribs.id); if (1 !== this.element.toolbar.size()) { qs.debug.warn('App_ECommerce_Order_Admin_List_ActionFormToolbar: Can not find action toolbar'); return this; } if (1 !== switcher.size()) { qs.debug.warn('App_ECommerce_Order_Admin_List_ActionFormToolbar: Can not find switcher checkbox'); return this; } this.element.actionsMap = {}; for (var element in this.options.elementActionsMap) { this.element.actionsMap[element] = $('.tool_' + element); this.element.actionsMap[element].bind(this.options.elementActionsMap[element].event + '.appListToolbar', _.bind(this.onAction, this)); } this.element.cellActionsMap = {}; for (var cellElement in this.options.elementActionsMap) { this.element.cellActionsMap[cellElement] = $('.cell_' + cellElement); this.element.cellActionsMap[cellElement].bind(this.options.elementActionsMap[cellElement].event + '.appListCell', _.bind(this.onCellAction, this)); } this.element.switcher = switcher; this.element.checkboxes = checkboxes; this.element.switcher.bind('change.qsListToolbar', _.bind(this.onSwitcherChange, this)); this.element.checkboxes.bind('change.qsListToolbar', _.bind(this.onCheckboxChange, this)); return this; }, classList2Selector: function (classList) { var selector = ''; for (var i = 0, length = classList.length; i < length; ++i) { selector += '.' + classList[i]; } return selector; }, onAction: function (e) { if (!this.options.elementActionsMap.hasOwnProperty(e.currentTarget.name)) { qs.debug.warn('App_ECommerce_Order_Admin_List_ActionFormToolbar: Can not find element options'); return this; } if (!e.currentTarget.value.length) { return false; } this.fireAction(this.options.elementActionsMap[e.currentTarget.name], e.currentTarget, true); return true; }, onCellAction: function(e) { if (typeof e.currentTarget.name == 'undefined') { return false; } var name = e.currentTarget.className.split('_')[1], selectedPrimary = [{id: e.currentTarget.name.split(name)[1]}]; if (!this.options.elementActionsMap.hasOwnProperty(name)) { qs.debug.warn('App_ECommerce_Order_Admin_List_ActionFormToolbar: Can not find element options'); return this; } this.fireAction(this.options.elementActionsMap[name], e.currentTarget, false, selectedPrimary); return true; }, fireAction: function (elementOptions, target, validateSelectedItems, selectedPrimary) { if (!selectedPrimary) { selectedPrimary = this.getSelectedPrimary(); } var action = elementOptions.action; if (validateSelectedItems) { if (!selectedPrimary.length) { alert('Please select item(s)'); $(this.element.actionsMap[target.name]).val(''); return this; } } if (action && this.confirmAction(elementOptions)) { var request = { value: target.value, action: action, selectedPrimary: selectedPrimary }; qs.ajax(this.list.options.url + '?action=' + request.action, request).done(function () { location.reload(); }); } else { $(this.element.actionsMap[target.name]).val(''); } return this; }, confirmAction: function (elementOptions) { var confirmationOptions, confirmation, result = true; confirmationOptions = elementOptions.confirmationOptions; if (confirmationOptions && _.isObject(confirmationOptions)) { if ((confirmation = confirmationOptions['confirmation'])) { result = confirm(confirmation); } if ((confirmation = confirmationOptions['confirmationCallback'])) { result = qs.call(confirmation); } } return result; }, onSwitcherChange: function () { if (this.element.switcher.is(':checked')) { this.element.checkboxes.not(':disabled').prop('checked', true); } else { this.element.checkboxes.prop('checked', false); } this.updateCounter(this.element.checkboxes.filter(':checked').size()); return true; }, updateCounter: function(count) { if ($(this.options.counter.updatingTagSelector).size()) { var updateHtml = count + ' '; updateHtml += (count == 1 ? this.options.counter.options.singular : this.options.counter.options.plural); $(this.options.counter.updatingTagSelector).html(updateHtml); } return true; }, onCheckboxChange: function (e) { var target = $(e.currentTarget); if (!target.is(':checked') && this.element.switcher.is(':checked')) { this.element.switcher.prop('checked', false); } this.updateCounter(this.element.checkboxes.filter(':checked').size()); return true; }, /** * Returns array of primary keys for selected items * @return {Array} Array of objects */ getSelectedPrimary: function () { var that = this, itemKeys, primaryKeys = []; this.element.checkboxes.filter(':checked').each(function () { itemKeys = that.list.getPrimaryKey(that.list.getItemNode(this)); primaryKeys.push(itemKeys); }); return primaryKeys; } };