diff life_calendar/happy.js @ 81:256b8df1c686

add life_calendar
author paulo
date Fri, 17 Jun 2016 22:24:17 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/life_calendar/happy.js	Fri Jun 17 22:24:17 2016 -0700
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*global $*/
     1.5 +(function happyJS($) {
     1.6 +    function trim(el) {
     1.7 +        return (''.trim) ? el.val().trim() : $.trim(el.val());
     1.8 +    }
     1.9 +    $.fn.isHappy = function isHappy(config) {
    1.10 +        var fields = [], item;
    1.11 +        var pauseMessages = false;
    1.12 +
    1.13 +        function isFunction(obj) {
    1.14 +            return !!(obj && obj.constructor && obj.call && obj.apply);
    1.15 +        }
    1.16 +        function defaultError(error) { //Default error template
    1.17 +            var msgErrorClass = config.classes && config.classes.message || 'unhappyMessage';
    1.18 +            return $('<span id="' + error.id + '" class="' + msgErrorClass + '" role="alert">' + error.message + '</span>');
    1.19 +        }
    1.20 +        function getError(error) { //Generate error html from either config or default
    1.21 +            if (isFunction(config.errorTemplate)) {
    1.22 +                return config.errorTemplate(error);
    1.23 +            }
    1.24 +            return defaultError(error);
    1.25 +        }
    1.26 +        function handleSubmit() {
    1.27 +            var  i, l;
    1.28 +            var errors = false;
    1.29 +            for (i = 0, l = fields.length; i < l; i += 1) {
    1.30 +                if (!fields[i].testValid(true)) {
    1.31 +                    errors = true;
    1.32 +                }
    1.33 +            }
    1.34 +            if (errors) {
    1.35 +                if (isFunction(config.unHappy)) config.unHappy();
    1.36 +                return false;
    1.37 +            } else if (config.testMode) {
    1.38 +                if (isFunction(config.happy)) return config.happy();
    1.39 +                if (window.console) console.warn('would have submitted');
    1.40 +                return false;
    1.41 +            }
    1.42 +            if (isFunction(config.happy)) return config.happy();
    1.43 +        }
    1.44 +        function handleMouseUp() {
    1.45 +            pauseMessages = false;
    1.46 +        }
    1.47 +        function handleMouseDown() {
    1.48 +            pauseMessages = true;
    1.49 +            $(window).bind('mouseup', handleMouseUp);
    1.50 +        }
    1.51 +        function processField(opts, selector) {
    1.52 +            var field = $(selector);
    1.53 +            var error = {
    1.54 +                message: opts.message || '',
    1.55 +                id: selector.slice(1) + '_unhappy'
    1.56 +            };
    1.57 +            var errorEl = $(error.id).length > 0 ? $(error.id) : getError(error);
    1.58 +            var handleBlur = function handleBlur() {
    1.59 +                if (!pauseMessages) {
    1.60 +                    field.testValid();
    1.61 +                } else {
    1.62 +                    $(window).bind('mouseup', field.testValid.bind(this));
    1.63 +                }
    1.64 +            };
    1.65 +
    1.66 +            fields.push(field);
    1.67 +            field.testValid = function testValid(submit) {
    1.68 +                var val, gotFunc, temp;
    1.69 +                var el = $(this);
    1.70 +                var errorTarget = (opts.errorTarget && $(opts.errorTarget)) || el;
    1.71 +                var error = false;
    1.72 +                var required = !!el.get(0).attributes.getNamedItem('required') || opts.required;
    1.73 +                var password = (field.attr('type') === 'password');
    1.74 +                var arg = isFunction(opts.arg) ? opts.arg() : opts.arg;
    1.75 +                var fieldErrorClass = config.classes && config.classes.field || 'unhappy';
    1.76 +
    1.77 +                // handle control groups (checkboxes, radio)
    1.78 +                if (el.length > 1) {
    1.79 +                  val = [];
    1.80 +                  el.each(function(i,obj) {
    1.81 +                    val.push($(obj).val());
    1.82 +                  });
    1.83 +                  val = val.join(',');
    1.84 +                } else {
    1.85 +                  // clean it or trim it
    1.86 +                  if (isFunction(opts.clean)) {
    1.87 +                      val = opts.clean(el.val());
    1.88 +                  } else if (!password && typeof opts.trim === 'undefined' || opts.trim) {
    1.89 +                      val = trim(el);
    1.90 +                  } else {
    1.91 +                      val = el.val();
    1.92 +                  }
    1.93 +
    1.94 +                  // write it back to the field
    1.95 +                  el.val(val);
    1.96 +                }
    1.97 +
    1.98 +                // get the value
    1.99 +                gotFunc = ((val.length > 0 || required === 'sometimes') && isFunction(opts.test));
   1.100 +
   1.101 +                // check if we've got an error on our hands
   1.102 +                if (submit === true && required === true && val.length === 0) {
   1.103 +                    error = true;
   1.104 +                } else if (gotFunc) {
   1.105 +                    error = !opts.test(val, arg);
   1.106 +                }
   1.107 +
   1.108 +                if (error) {
   1.109 +                    errorTarget.addClass(fieldErrorClass).after(errorEl);
   1.110 +                    return false;
   1.111 +                } else {
   1.112 +                    temp = errorEl.get(0);
   1.113 +                    // this is for zepto
   1.114 +                    if (temp.parentNode) {
   1.115 +                        temp.parentNode.removeChild(temp);
   1.116 +                    }
   1.117 +                    errorTarget.removeClass(fieldErrorClass);
   1.118 +                    return true;
   1.119 +                }
   1.120 +            };
   1.121 +            field.bind(opts.when || config.when || 'blur', handleBlur);
   1.122 +        }
   1.123 +
   1.124 +        for (item in config.fields) {
   1.125 +            processField(config.fields[item], item);
   1.126 +        }
   1.127 +
   1.128 +        $(config.submitButton || this).bind('mousedown', handleMouseDown);
   1.129 +
   1.130 +        if (config.submitButton) {
   1.131 +            $(config.submitButton).click(handleSubmit);
   1.132 +        } else {
   1.133 +            this.bind('submit', handleSubmit);
   1.134 +        }
   1.135 +        return this;
   1.136 +    };
   1.137 +})(this.jQuery || this.Zepto);