MD.Input = MD.Input || {}; MD.Input.Base = new Class({ Implements: [Options, Events] ,options: { returnHTML: false ,validators: [ //{ // name: 'MyValidator' // ,validation: Required // ,options: {} //} ] ,filters: [ //{ // name: 'MyFilter' // ,filter: MD.Filter.Trim //} ] ,activatedItemClassName: 'Selected' ,disabledItemClassName: 'Disabled' ,disabled: false //onData:dirty function() //onData:dirty:after function(this) //onReady(this) } ,initialize: function(options) { this.setOptions(options); // define this this.html = ''; this._setup(); this._setupEventHandlers(); // setup validators if (this.options.validators.length) { this.addValidators(this.options.validators); } if (this.options.filters.length) { this.addFilters(this.options.filters); } this._resetValidationValues(); if (this.options.disabled) { this.disable(); } if (this.options.returnHTML) { return this.html; } this._defaultReady(); // we use this because almost all inputs are ready right away, so we default to fire this event } ,isReady: function() { return true; } ,_defaultReady: function() { this.fireEvent('ready', this); } ,destroy: function() { } ,_setup: function() { this._validators = {}; this._needsValidation = true; this._validationErrors = {}; this._needsFilter = true; this._filteredValue = ""; this._filters = {}; } ,_resetValidationValues: function() { this._needsValidation = true; this._needsFilter = true; this._validationErrors = {}; } ,_setupEventHandlers: function() { this.addEvent('data:dirty', function() { this._resetValidationValues(); this.fireEvent('data:dirty:after', this); }.bind(this)); } ,activate: function() { throw new Error('did not implement activate'); } ,deactivate: function() { throw new Error('did not implement deactivate'); } ,enable: function() { throw new Error('did not implement enable'); } ,disable: function() { throw new Error('did not implement disable'); } ,show: function(displayString) { this.toElement().show(displayString); return this; } ,hide: function() { this.toElement().hide(); return this; } ,toElement: function() { return this.html; } /* .d8888b. 888 .d8888b. .d8888b. 888 d88P Y88b 888 d88P "88b d88P Y88b 888 888 888 888 Y88b. d88P Y88b. 888 888 .d88b. 888888 .d8888b "Y8888P" "Y888b. .d88b. 888888 .d8888b 888 88888 d8P Y8b 888 88K .d88P88K.d88P "Y88b. d8P Y8b 888 88K 888 888 88888888 888 "Y8888b. 888" Y888P" "888 88888888 888 "Y8888b. Y88b d88P Y8b. Y88b. X88 Y88b .d8888b Y88b d88P Y8b. Y88b. X88 "Y8888P88 "Y8888 "Y888 88888P' "Y8888P" Y88b "Y8888P" "Y8888 "Y888 88888P' */ ,setPlaceholderText: function(value) { this._placeholderText = value; this.options.placeholderText = value; } ,getValue: function(value) { if (this._needsFilter || value !== this._filteredValue) { Object.each(this._filters, function(filter) { value = filter.execute(value); }); this.filteredValue = value; this._needsFilter = false; } return this.filteredValue; } ,getValueAsText: function(value) { return this.getValue(value); } ,setValue: function(value) { this.fireEvent('data:dirty', this); throw new Error("setValue not implemented "+value); //return this; } ,_getBaseClassName: function() { return 'MDInput'; } /* 888 888 888 d8b 888 888 d8b 888 888 888 Y8P 888 888 Y8P 888 888 888 888 888 Y88b d88P 8888b. 888 888 .d88888 8888b. 888888 888 .d88b. 88888b. Y88b d88P "88b 888 888 d88" 888 "88b 888 888 d88""88b 888 "88b Y88o88P .d888888 888 888 888 888 .d888888 888 888 888 888 888 888 Y888P 888 888 888 888 Y88b 888 888 888 Y88b. 888 Y88..88P 888 888 Y8P "Y888888 888 888 "Y88888 "Y888888 "Y888 888 "Y88P" 888 888 */ ,addValidators: function(validators) { if (validators && validators.length) { validators.each(function(validator) { this.addValidator(validator.name, validator.validation, validator.options); }, this); } return this; } ,addValidator: function(name, validation, options) { this._validators[name] = MD.Validation.Factory(validation, options); this.fireEvent('data:dirty', this); return this; } ,removeValidators: function(names) { if (names && names.length) { names.each(function(name) { this.removeValidator(name); }, this); } return this; } ,removeValidator: function(name) { if (this._validators[name]) { delete this._validators[name]; } this.fireEvent('data:dirty', this); return this; } ,validate: function(name) { // allow single validation by name if (name) { if (this._validators[name]) { var isValid = this._validators[name].validate(this); if ( ! isValid) { var toReturn = {}; toReturn[name] = this._validators[name].getMessage(); return toReturn; } } return {}; } else { if (this._needsValidation) { var errors = {}; Object.each(this._validators, function(validator, key) { var isValid = validator.validate(this); if ( ! isValid) { errors[key] = validator.getMessage(); } }, this); this._validationErrors = errors; this._needsValidation = false; } return this._validationErrors; } } ,isValid: function() { return Object.getLength(this.validate()) === 0; } ,getValidationErrors: function() { return this.validate(); } /* 8888888888 d8b 888 888 d8b 888 Y8P 888 888 Y8P 888 888 888 8888888 888 888 888888 .d88b. 888d888 888 88888b. .d88b. 888 888 888 888 d8P Y8b 888P" 888 888 "88b d88P"88b 888 888 888 888 88888888 888 888 888 888 888 888 888 888 888 Y88b. Y8b. 888 888 888 888 Y88b 888 888 888 888 "Y888 "Y8888 888 888 888 888 "Y88888 888 Y8b d88P "Y88P" */ ,addFilters: function(filters) { if (filters && filters.length) { filters.each(function(filter) { this.addFilter(filter.name, filter.filter, filter.options); }, this); } return this; } ,addFilter: function(name, filter, options) { this._filters[name] = MD.Filter.Factory(filter, options); this.fireEvent('data:dirty', this); return this; } ,removeFilters: function(names) { if (names && names.length) { names.each(function(name) { this.removeFilter(name); }, this); } return this; } ,removeFilter: function(name) { if (this._filters[name]) { delete this._filters[name]; } this.fireEvent('data:dirty', this); return this; } });