Ext.namespace( 'instore.extlib.ui.form' ); instore.extlib.ui.form.FormPanel = Ext.extend(Ext.form.FormPanel, { warnInvalid: function() { Ext.Msg.show({title: instore.extlib.ui.Messages.errorTypes.WARNING, msg: instore.extlib.ui.Messages.warnings.submit, buttons: Ext.Msg.OK, icon: Ext.MessageBox.WARNING}); }, validateForm: function() { if(this.form.isValid()) return true; else { this.warnInvalid(); return false; } }, clearValues: function() { Ext.each(this.form.items.items, function(item) { item.setValue(null); }); }, getValues: function() { var values = {}; Ext.each(this.form.items.items, function(item) { var value = item.getValue(); if(value != null && value != "") values[item.id] = value; }); return values; }, getFieldValue: function(fieldId) { return (this.findById(fieldId)) ? (this.findById(fieldId).getValue() || null) : null; }, getFieldRawValue: function(fieldId) { return this.findById(fieldId).getRawValue() || null; }, setupListeners: function() { this.on("validate", this.validateForm); }, setValidationText: function(formItems) { if(formItems && typeof formItems == "object") { Ext.each(formItems, function(formItem) { if(formItem) { if(formItem.xtype == "fieldset") this.setValidationText(formItem.items || null); else if(formItem instanceof Ext.form.FieldSet) this.setValidationText(formItem.items.items || null); else { var field = formItem; if(["field", "textfield", "textarea", "numberfield", "datefield", "timefield", "combo"].indexOf(field.xtype) != -1) { Ext.apply(field, { blankText: instore.extlib.ui.Messages.validation.blankText, msgTarget: "side", minLengthText: instore.extlib.ui.Messages.validation.minLengthText, maxLengthText: instore.extlib.ui.Messages.validation.maxLengthText }); if(field.xtype == "datefield") { field.invalidText = instore.extlib.ui.Messages.validation.invalidDateText; field.minText = instore.extlib.ui.Messages.validation.minDateText; field.maxText = instore.extlib.ui.Messages.validation.maxDateText; } if(field.vtype && instore.extlib.ui.Messages.validation[field.vtype]) field.vtypeText = instore.extlib.ui.Messages.validation[field.vtype]; } } } }.createDelegate(this)); } }, checkFormItems: function(formConfig) { if(!formConfig.items || formConfig.items.length == 0 || ( formConfig.items.length == 1 && formConfig.items[0].xtype == "fieldset" && formConfig.items[0].items.length == 0 ) ) formConfig.items = [{}]; }, assignSpecialKeys: function(field, fn, scope) { instore.extlib.ui.util.Util.assignSpecialKeys(field, fn, scope); }, initComponent: function() { this.addEvents("validate"); this.listenersSetup = false; this.on("afterlayout", function() { if(!this.listenersSetup) { this.setupListeners(); this.listenersSetup = true; } }.createDelegate(this), this); if(!this.items || this.items.length == 0) this.items = [{}]; else { this.setValidationText(this.items); this.checkFormItems(this); } instore.extlib.ui.form.FormPanel.superclass.initComponent.call(this); } }); instore.extlib.ui.form.SaveFormPanel = Ext.extend(instore.extlib.ui.form.FormPanel, { dirty: false, warnInvalid: function() { Ext.Msg.show({title: instore.extlib.ui.Messages.errorTypes.WARNING, msg: instore.extlib.ui.Messages.warnings.invalidFields, buttons: Ext.Msg.OK, icon: Ext.MessageBox.WARNING}); }, setDirty: function(dirty) { var cancelBtn = this.getTopToolbar().items.map.cancelBtn; if(dirty) { this.getTopToolbar().items.map.saveBtn.enable(); if(cancelBtn) cancelBtn.enable(); } else { this.getTopToolbar().items.map.saveBtn.disable(); if(cancelBtn) cancelBtn.disable(); } this.dirty = dirty; }, save: function(saveFunc, saveArgs) { if(this.fireEvent("validate") === true) saveFunc.createDelegate(this, saveArgs).call(); else this.fireEvent("save", false); }, warnSaveError: function() { Ext.Msg.show({title: instore.extlib.ui.Messages.errorTypes.ERROR, msg: instore.extlib.ui.Messages.errors["ERROR_SAVE_FORM"], buttons: Ext.Msg.OK, icon: Ext.MessageBox.ERROR}); }, checkSaveDirty: function(saveFunc, saveArgs) { if(this.dirty) { Ext.Msg.confirm( instore.enat.Messages.request.askSaveTitle, instore.enat.Messages.request.askSaveMessage, function(btn) { if(btn == 'yes') { instore.extlib.ui.util.Util.setUniqueListener(this, "save", function(success) { this.fireEvent("checksavedirty", success) }); this.save(saveFunc, saveArgs); } else this.fireEvent("checksavedirty", false); }.createDelegate(this) ); } else this.fireEvent("checksavedirty", true); }, onModify: function(success) { this.setDirty(success); }, cancel: function() { }, initComponent: function() { this.addEvents("save", "checksavedirty"); this.on("render", function() { Ext.each(this.form.items.items, function(field) { field.on("change", this.setDirty.createDelegate(this, [true]), this); }.createDelegate(this)) }, this); var saveBtn = { id: "saveBtn", text: instore.extlib.ui.Messages.buttons.save, disabled: true, handler: this.save, scope: this }; // var cancelBtn = { // id: "cancelBtn", // text: instore.extlib.ui.Messages.buttons.cancel, // disabled: true, // handler: this.cancel, // scope: this // }; if(this.tbar) this.tbar.push({xtype: 'tbseparator'}, saveBtn/*, cancelBtn*/); else this.tbar = [ saveBtn/*, cancelBtn*/ ]; instore.extlib.ui.form.SaveFormPanel.superclass.initComponent.call(this); } }); instore.extlib.ui.form.ComboBox = Ext.extend(Ext.form.ComboBox, { xtype: "combo", doSort: true, sortDir: "ASC", maxListWidth: 450, resizable: true, renderComboDisplay: true, renderCombo: function(mapComboObj) { var minListWidthInChars = Math.round((this.minListWidth - 5) / 7); if(this.store.data) { var options = mapComboObj || instore.extlib.ui.Messages.combo[this.comboName] || null; this.store.data.each(function(option) { if(this.renderComboDisplay && options && options[option.data[this.valueField]]) option.data[this.displayField] = options[option.data[this.valueField]] || ""; var value = option.data[this.displayField]; if(value != null) { if(minListWidthInChars < option.data[this.displayField].length) minListWidthInChars = option.data[this.displayField].length; } }.createDelegate(this)); if(this.renderComboDisplay && this.view) this.view.refresh(); this.minListWidth = (minListWidthInChars * 7) + 5; this.minListWidth = (this.minListWidth > this.maxListWidth) ? this.maxListWidth : this.minListWidth; this.setWidth(this.minListWidth + 17); } }, addQTips: function() { Ext.each(this.view.getNodes(), function(node) { if(node) node.qtip = node.textContent; }) }, initList: function() { instore.extlib.ui.form.ComboBox.superclass.initList.call(this); if(this.store.proxy) this.store.on("load", this.addQTips, this); else this.addQTips(); }, initComponent: function() { instore.extlib.ui.form.ComboBox.superclass.initComponent.call(this); if(this.store.proxy) { this.sortInfo = this.displayField; this.store.on("load", function() { this.renderCombo() }, this); this.store.on("clear", function() { this.lastQuery = null }, this ); } else { this.renderCombo(); if(this.doSort) this.store.sort(this.displayField, this.sortDir); } this.constructor.xtype = this.xtype; } }); Ext.reg("combo", instore.extlib.ui.form.ComboBox); instore.extlib.ui.form.ZeroesPrefixedComboBox = function(config) { Ext.apply(config, {zeroes: config.zeroes || 0}); instore.extlib.ui.form.ZeroesPrefixedComboBox.superclass.constructor.call(this, config); }; Ext.extend(instore.extlib.ui.form.ZeroesPrefixedComboBox, instore.extlib.ui.form.ComboBox, { zeroes: 0, getValue: function() { var val = instore.extlib.ui.form.ZeroesPrefixedComboBox.superclass.getValue.call(this); for(var i = 0; i < this.zeroes; i++) val = "0" + val; return val; } }); instore.extlib.ui.form.ZeroesPrefixedComboBoxTwo = function(config) { Ext.apply(config, {zeroes: config.zeroes || 0}); instore.extlib.ui.form.ZeroesPrefixedComboBoxTwo.superclass.constructor.call(this, config); }; Ext.extend(instore.extlib.ui.form.ZeroesPrefixedComboBoxTwo, instore.extlib.ui.form.ComboBox, { zeroes: 0, getValue: function() { var val = instore.extlib.ui.form.ZeroesPrefixedComboBoxTwo.superclass.getValue.call(this); for(var i = 0; i < this.zeroes; i++) { if ( val.length < 2 ) val = "0" + val; } return val; } }); instore.extlib.ui.form.MonthComboBox = function(config) { return new instore.extlib.ui.form.ZeroesPrefixedComboBoxTwo(Ext.apply(config, { xtype: "month", comboName: "month", doSort: false, store: [ ["1", "January"], ["2", "February"], ["3", "March"], ["4", "April"], ["5", "May"], ["6", "June"], ["7", "July"], ["8", "August"], ["9", "September"], ["10", "October"], ["11", "November"], ["12", "December"], ["13", "Easter Gift"], ["14", "Christmas Gift"], ["15", "Holiday Gift"] ], value: config.value || "1" })); }; Ext.reg("monthcombo", instore.extlib.ui.form.MonthComboBox); instore.extlib.ui.form.FourMonthComboBox = function(config) { return new instore.extlib.ui.form.ZeroesPrefixedComboBox(Ext.apply(config, { xtype: "month", comboName: "fourmonth", doSort: false, store: [ ["1", "First"], ["2", "Second"], ["3", "Third"] ], value: config.value || "1", zeroes: 0 })); }; Ext.reg("fourmonthcombo", instore.extlib.ui.form.FourMonthComboBox); instore.extlib.ui.form.SixMonthComboBox = function(config) { return new instore.extlib.ui.form.ZeroesPrefixedComboBox(Ext.apply(config, { xtype: "month", comboName: "sixmonth", doSort: false, store: [ ["1", "First"], ["2", "Second"] ], value: config.value || "1", zeroes: 0 })); }; Ext.reg("sixmonthcombo", instore.extlib.ui.form.SixMonthComboBox); instore.extlib.ui.form.YearComboBox = function(config) { Ext.apply(config, { minYear: config.minYear || 1900, maxYear: config.maxYear || 2100, comboName: "year", sortDir: config.sortDir || "DESC", value: config.value || (new Date()).getFullYear() }); instore.extlib.ui.form.YearComboBox.superclass.constructor.call(this, config); }; Ext.extend(instore.extlib.ui.form.YearComboBox, instore.extlib.ui.form.ComboBox, { initComponent: function() { this.store = []; var year = this.minYear; for(; year < this.maxYear; year++) this.store.push([year, year]); instore.extlib.ui.form.YearComboBox.superclass.initComponent.call(this); } }); Ext.reg("yearcombo", instore.extlib.ui.form.YearComboBox);