Guest User

Untitled

a guest
Jan 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Provides a input validation system based on event triggering of
  2. // three different type ('info', 'warning', 'error').
  3. // Validation logic is provided extending this base class.
  4. var Lunaval = new Class({
  5.    
  6.     Implements: [Options, Events],
  7.    
  8.     options: {
  9.         getValue: null, // A callback function that returns the value to check
  10.         getValueOn: ['keyup', 'focus', 'blur'], // Element events in which trigger validation
  11.         infoDelay: false, // If integer, remove 'info' message after specified delay
  12.         infoDisappear: ['blur'] // Element events in which remove 'info' message
  13.     },
  14.    
  15.     // Messages
  16.     error: null,
  17.     warning: null,
  18.     info: null,
  19.    
  20.     initialize: function(element, options) {
  21.        
  22.         // Setup element
  23.         this.element = document.id(element);
  24.         if (typeOf(this.element.retrieve('_lunavals')) != 'array') this.element.store('_lunavals', []);
  25.         this.element.retrieve('_lunavals').push(this);
  26.        
  27.         // Setup options
  28.         this.setOptions(options);
  29.         if (typeOf(this.options.getValue) != 'function') this.options.getValue = this.element.get.pass('value', this.element);
  30.        
  31.         // Setup events
  32.         this.options.getValueOn.each(function(event) {
  33.             this.element.addEvent(event, this.validate.bind(this));
  34.         }.bind(this));
  35.         this.options.infoDisappear.each(function(event) {
  36.             this.element.addEvent(this.options.infoDisappear, function() {
  37.                 this.throw(this.error, this.warning, null);
  38.             }.bind(this));
  39.         }.bind(this));
  40.        
  41.         return this;
  42.        
  43.     },
  44.    
  45.     // Triggers validation
  46.     validate: function() {
  47.         var value = this.options.getValue();
  48.         this.throw(null, null, null);
  49.         return this;
  50.     },
  51.    
  52.     // Throws events if messages changed
  53.     throw: function(error, warning, info) {
  54.        
  55.         // Throw error and warning
  56.         if (this.error   !=   error) this.fireEvent(  'error', [  this.error = (error   || null), this.element]);
  57.         if (this.warning != warning) this.fireEvent('warning', [this.warning = (warning || null), this.element]);
  58.        
  59.         // Throw info, manage delay
  60.         if (this.info != info) {
  61.             this.fireEvent('info', [this.info = (info || null), this.element]);
  62.             if (this.options.infoDelay) {
  63.                 if (this.infoTimeout) clearTimeout(this.infoTimeout);
  64.                 this.infoTimeout = (function() {
  65.                     this.fireEvent('info', [this.info = null, this.element]);
  66.                 }).delay(this.options.infoDelay, this);
  67.             }
  68.         }
  69.        
  70.         return this;
  71.     },
  72.    
  73.     // Query for errors
  74.     isError  : function() { return this.error  ; },
  75.     isWarning: function() { return this.warning; },
  76.     isInfo   : function() { return this.info   ; }
  77.    
  78. });
  79.  
  80. // Extends Lunaval to provide a first-char upper-case validator
  81. Lunaval.UCFirst = new Class({
  82.    
  83.     Extends: Lunaval,
  84.    
  85.     options: {
  86.         messages: {
  87.             error: 'Specified value starts with a lowercase letter',
  88.             ok: 'Specified value is well capitalized'
  89.         }
  90.     },
  91.    
  92.     validate: function() {
  93.         var first = this.options.getValue().substr(0, 1);
  94.         var error = (first.toUpperCase() != first);
  95.         var info = (this.error && !error) ? this.options.messages.ok : null;
  96.         this.throw(error ? this.options.messages.error : null, null, error ? null : (info || this.info));
  97.         return this;
  98.     }
  99.    
  100. });
  101.  
  102. // Extends Lunaval to provide a characters count validator with
  103. // optional upper and lower limits
  104. Lunaval.CharCount = new Class({
  105.    
  106.     Extends: Lunaval,
  107.    
  108.     options: {
  109.         max: false, // If numeric, set a lower characters count limit
  110.         min: false, // If numeric, set an upper characters count limit
  111.         messages: {
  112.             over: '{2} characters over limit', // {0} is current, {1} is max, {2} is overflow
  113.             ok: 'Characters count: {0}', // {0} is current count, {1} is max, {2} is min, {3} is left, {4} is chars over min
  114.             under: '{2} characters to go' // {0} is current, {1} is max, {2} is underflow
  115.         }
  116.     },
  117.    
  118.     validate: function() {
  119.         var o = this.options;
  120.         var count = this.options.getValue().length;
  121.         var error = null;
  122.         if (o.max && count > o.max) error = o.messages.over.substitute([count, o.max, count - o.max]);
  123.         if (o.min && count < o.min) error = o.messages.under.substitute([count, o.min, o.min - count]);
  124.         this.throw(error, null, error ? null : o.messages.ok.substitute([count, o.max, o.min, o.max - count, o.count - o.min]));
  125.         return this;
  126.     }
  127.    
  128. });
  129.  
  130. // Provides visual notifications for Lunaval.
  131. // Notifications are placed below target element.
  132. var LunavalNotifications = new Class({
  133.    
  134.     Implements: [Options],
  135.    
  136.     options: {
  137.         offset: { x: 0, y: 10 }
  138.     },
  139.    
  140.     initialize: function(element, options) {
  141.         this.setOptions(options);
  142.        
  143.         // Setup Lunaval events (get Lunaval validators from element storage)
  144.         this.element = document.id(element);
  145.         this.element.store('_lunavalNotifications', this);
  146.         if (typeOf(this.element.retrieve('_lunavals')) == 'array') {
  147.             this.element.retrieve('_lunavals').each(function(lunaval, i) {
  148.                 lunaval.addEvents({
  149.                     'error'  : function(m) { this.show(  'error', i, m); }.bind(this),
  150.                     'warning': function(m) { this.show('warning', i, m); }.bind(this),
  151.                     'info'   : function(m) { this.show(   'info', i, m); }.bind(this)
  152.                 });
  153.             }.bind(this));
  154.         };
  155.        
  156.         // Setup wrapper element
  157.         this.wrapper = new Element('div.lunaval-notifications');
  158.         this.wrapper.setStyle('position', 'absolute').inject(document.body);
  159.         this.positionate();
  160.        
  161.         // Repositionate wrapper every 500ms
  162.         this.positionate.periodical(500, this);
  163.        
  164.         return this;
  165.     },
  166.    
  167.     // Repositionate messages
  168.     positionate: function() {
  169.         var coo = this.element.getCoordinates();
  170.         this.wrapper.setStyles({
  171.             left: coo.left + this.options.offset.x,
  172.             top: coo.bottom + this.options.offset.y
  173.         })
  174.         return this;
  175.     },
  176.    
  177.     // Show a message, group is the Lunaval index
  178.     show: function(type, group, message) {
  179.         var className = 'lunaval-notifications-' + group + '-' + type;
  180.         if (message) {
  181.             var element = this.wrapper.getElements('.' + className).pick();
  182.             if (!element) this.wrapper.adopt(element = new Element('div.' + className).fade('hide').fade('in'));
  183.             element.set('text', message);
  184.         } else {
  185.             this.wrapper.getElements('.' + className).destroy();
  186.         }
  187.         this.positionate();
  188.         return this;
  189.     },
  190.    
  191.     disable: function() {
  192.         this.wrapper.setStyle('display', 'none');
  193.         return this;
  194.     },
  195.    
  196.     enable: function() {
  197.         this.wrapper.setStyle('display', '');
  198.         this.positionate();
  199.         return this;
  200.     }
  201.    
  202. });
  203.  
  204. // Provides 'lunavalNotifications' element property to retrieve
  205. // LunavalNotifications instance attached to element
  206. Element.Properties.lunavalNotifications = {
  207.    
  208.     get: function() {
  209.         return this.retrieve('_lunavalNotifications');
  210.     }
  211.    
  212. };
Add Comment
Please, Sign In to add comment