mangroveorg/datawinners

View on GitHub
datawinners/media/javascript/Knockout-Validation/Src/utils.js

Summary

Maintainability
D
2 days
Test Coverage
ko.validation.utils = (function () {
    var seedId = new Date().getTime();

    var domData = {}; //hash of data objects that we reference from dom elements
    var domDataKey = '__ko_validation__';

    return {
        isArray: function (o) {
            return o.isArray || Object.prototype.toString.call(o) === '[object Array]';
        },
        isObject: function (o) {
            return o !== null && typeof o === 'object';
        },
        isObservableArray: function(instance) {
            return !!instance &&
                    typeof instance["remove"] === "function" &&
                    typeof instance["removeAll"] === "function" &&
                    typeof instance["destroy"] === "function" &&
                    typeof instance["destroyAll"] === "function" &&
                    typeof instance["indexOf"] === "function" &&
                    typeof instance["replace"] === "function";
        },
        values: function (o) {
            var r = [];
            for (var i in o) {
                if (o.hasOwnProperty(i)) {
                    r.push(o[i]);
                }
            }
            return r;
        },
        getValue: function (o) {
            return (typeof o === 'function' ? o() : o);
        },
        hasAttribute: function (node, attr) {
            return node.getAttribute(attr) !== null;
        },
        getAttribute: function (element, attr) {
            return element.getAttribute(attr);
        },
        setAttribute: function (element, attr, value) {
            return element.setAttribute(attr, value);
        },
        isValidatable: function (o) {
            return !!(o && o.rules && o.isValid && o.isModified);
        },
        insertAfter: function (node, newNode) {
            node.parentNode.insertBefore(newNode, node.nextSibling);
        },
        newId: function () {
            return seedId += 1;
        },
        getConfigOptions: function (element) {
            var options = ko.validation.utils.contextFor(element);

            return options || ko.validation.configuration;
        },
        setDomData: function (node, data) {
            var key = node[domDataKey];

            if (!key) {
                node[domDataKey] = key = ko.validation.utils.newId();
            }

            domData[key] = data;
        },
        getDomData: function (node) {
            var key = node[domDataKey];

            if (!key) {
                return undefined;
            }

            return domData[key];
        },
        contextFor: function (node) {
            switch (node.nodeType) {
                case 1:
                case 8:
                    var context = ko.validation.utils.getDomData(node);
                    if (context) { return context; }
                    if (node.parentNode) { return ko.validation.utils.contextFor(node.parentNode); }
                    break;
            }
            return undefined;
        },
        isEmptyVal: function (val) {
            if (val === undefined) {
                return true;
            }
            if (val === null) {
                return true;
            }
            if (val === "") {
                return true;
            }
        },
        getOriginalElementTitle: function (element) {
            var savedOriginalTitle = ko.validation.utils.getAttribute(element, 'data-orig-title'),
                currentTitle = element.title,
                hasSavedOriginalTitle = ko.validation.utils.hasAttribute(element, 'data-orig-title');

            return hasSavedOriginalTitle ?
                savedOriginalTitle : currentTitle;
        },
        async: function (expr) {
            if (window.setImmediate) { window.setImmediate(expr); }
            else { window.setTimeout(expr, 0); }
        },
        forEach: function (object, callback) {
            if (ko.validation.utils.isArray(object)) {
                return ko.utils.arrayForEach(object, callback);
            }
            for (var prop in object) {
                if (object.hasOwnProperty(prop)) {
                    callback(object[prop], prop);
                }
            }
        }
    };
}());