Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Provides a input validation system based on event triggering of
- // three different type ('info', 'warning', 'error').
- // Validation logic is provided extending this base class.
- var Lunaval = new Class({
- Implements: [Options, Events],
- options: {
- getValue: null, // A callback function that returns the value to check
- getValueOn: ['keyup', 'focus', 'blur'], // Element events in which trigger validation
- infoDelay: false, // If integer, remove 'info' message after specified delay
- infoDisappear: ['blur'] // Element events in which remove 'info' message
- },
- // Messages
- error: null,
- warning: null,
- info: null,
- initialize: function(element, options) {
- // Setup element
- this.element = document.id(element);
- if (typeOf(this.element.retrieve('_lunavals')) != 'array') this.element.store('_lunavals', []);
- this.element.retrieve('_lunavals').push(this);
- // Setup options
- this.setOptions(options);
- if (typeOf(this.options.getValue) != 'function') this.options.getValue = this.element.get.pass('value', this.element);
- // Setup events
- this.options.getValueOn.each(function(event) {
- this.element.addEvent(event, this.validate.bind(this));
- }.bind(this));
- this.options.infoDisappear.each(function(event) {
- this.element.addEvent(this.options.infoDisappear, function() {
- this.throw(this.error, this.warning, null);
- }.bind(this));
- }.bind(this));
- return this;
- },
- // Triggers validation
- validate: function() {
- var value = this.options.getValue();
- this.throw(null, null, null);
- return this;
- },
- // Throws events if messages changed
- throw: function(error, warning, info) {
- // Throw error and warning
- if (this.error != error) this.fireEvent( 'error', [ this.error = (error || null), this.element]);
- if (this.warning != warning) this.fireEvent('warning', [this.warning = (warning || null), this.element]);
- // Throw info, manage delay
- if (this.info != info) {
- this.fireEvent('info', [this.info = (info || null), this.element]);
- if (this.options.infoDelay) {
- if (this.infoTimeout) clearTimeout(this.infoTimeout);
- this.infoTimeout = (function() {
- this.fireEvent('info', [this.info = null, this.element]);
- }).delay(this.options.infoDelay, this);
- }
- }
- return this;
- },
- // Query for errors
- isError : function() { return this.error ; },
- isWarning: function() { return this.warning; },
- isInfo : function() { return this.info ; }
- });
- // Extends Lunaval to provide a first-char upper-case validator
- Lunaval.UCFirst = new Class({
- Extends: Lunaval,
- options: {
- messages: {
- error: 'Specified value starts with a lowercase letter',
- ok: 'Specified value is well capitalized'
- }
- },
- validate: function() {
- var first = this.options.getValue().substr(0, 1);
- var error = (first.toUpperCase() != first);
- var info = (this.error && !error) ? this.options.messages.ok : null;
- this.throw(error ? this.options.messages.error : null, null, error ? null : (info || this.info));
- return this;
- }
- });
- // Extends Lunaval to provide a characters count validator with
- // optional upper and lower limits
- Lunaval.CharCount = new Class({
- Extends: Lunaval,
- options: {
- max: false, // If numeric, set a lower characters count limit
- min: false, // If numeric, set an upper characters count limit
- messages: {
- over: '{2} characters over limit', // {0} is current, {1} is max, {2} is overflow
- ok: 'Characters count: {0}', // {0} is current count, {1} is max, {2} is min, {3} is left, {4} is chars over min
- under: '{2} characters to go' // {0} is current, {1} is max, {2} is underflow
- }
- },
- validate: function() {
- var o = this.options;
- var count = this.options.getValue().length;
- var error = null;
- if (o.max && count > o.max) error = o.messages.over.substitute([count, o.max, count - o.max]);
- if (o.min && count < o.min) error = o.messages.under.substitute([count, o.min, o.min - count]);
- this.throw(error, null, error ? null : o.messages.ok.substitute([count, o.max, o.min, o.max - count, o.count - o.min]));
- return this;
- }
- });
- // Provides visual notifications for Lunaval.
- // Notifications are placed below target element.
- var LunavalNotifications = new Class({
- Implements: [Options],
- options: {
- offset: { x: 0, y: 10 }
- },
- initialize: function(element, options) {
- this.setOptions(options);
- // Setup Lunaval events (get Lunaval validators from element storage)
- this.element = document.id(element);
- this.element.store('_lunavalNotifications', this);
- if (typeOf(this.element.retrieve('_lunavals')) == 'array') {
- this.element.retrieve('_lunavals').each(function(lunaval, i) {
- lunaval.addEvents({
- 'error' : function(m) { this.show( 'error', i, m); }.bind(this),
- 'warning': function(m) { this.show('warning', i, m); }.bind(this),
- 'info' : function(m) { this.show( 'info', i, m); }.bind(this)
- });
- }.bind(this));
- };
- // Setup wrapper element
- this.wrapper = new Element('div.lunaval-notifications');
- this.wrapper.setStyle('position', 'absolute').inject(document.body);
- this.positionate();
- // Repositionate wrapper every 500ms
- this.positionate.periodical(500, this);
- return this;
- },
- // Repositionate messages
- positionate: function() {
- var coo = this.element.getCoordinates();
- this.wrapper.setStyles({
- left: coo.left + this.options.offset.x,
- top: coo.bottom + this.options.offset.y
- })
- return this;
- },
- // Show a message, group is the Lunaval index
- show: function(type, group, message) {
- var className = 'lunaval-notifications-' + group + '-' + type;
- if (message) {
- var element = this.wrapper.getElements('.' + className).pick();
- if (!element) this.wrapper.adopt(element = new Element('div.' + className).fade('hide').fade('in'));
- element.set('text', message);
- } else {
- this.wrapper.getElements('.' + className).destroy();
- }
- this.positionate();
- return this;
- },
- disable: function() {
- this.wrapper.setStyle('display', 'none');
- return this;
- },
- enable: function() {
- this.wrapper.setStyle('display', '');
- this.positionate();
- return this;
- }
- });
- // Provides 'lunavalNotifications' element property to retrieve
- // LunavalNotifications instance attached to element
- Element.Properties.lunavalNotifications = {
- get: function() {
- return this.retrieve('_lunavalNotifications');
- }
- };
Add Comment
Please, Sign In to add comment