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); 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; } 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)); } 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) { console.log('cell Action'); 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; }, 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); 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; } };