steps.min.js 8.67 KB
! function (undefined) {
    "use strict";
    var _global, utils = {
            extend: (isObjFunc = function (name) {
                var toString = Object.prototype.toString;
                return function () {
                    return toString.call(arguments[0]) === "[object " + name + "]"
                }
            }, isObject = isObjFunc("Object"), isArray = isObjFunc("Array"), isBoolean = isObjFunc("Boolean"), function extend() {
                var index = 0,
                    isDeep = !1,
                    obj, copy, destination, source, i;
                for (isBoolean(arguments[0]) && (index = 1, isDeep = arguments[0]), i = arguments.length - 1; i > index; i--)
                    if (destination = arguments[i - 1], source = arguments[i], isObject(source) || isArray(source))
                        for (var property in source)
                            if (obj = source[property], isDeep && (isObject(obj) || isArray(obj))) {
                                var extended = extend(isDeep, copy = isObject(obj) ? {} : [], obj);
                                destination[property] = extended
                            } else destination[property] = source[property];
                else destination = source;
                return destination
            })
        },
        isObjFunc, isObject, isArray, isBoolean, steps = function (options) {
            return new _steps(options)
        };

    function _steps(options) {
        this._initial(options)
    }
    _steps.prototype = {
        constructor: this,
        _initial: function (options) {
            var _this = this,
                defined = {
                    el: "",
                    data: [],
                    props: {
                        title: "title",
                        description: "description",
                        status: "status",
                        icon: "icon",
                        customHtml: "customHtml"
                    },
                    dataOrder: ["line", "title", "description"],
                    dataWidth: [],
                    space: null,
                    direction: "horizontal",
                    iconType: "number",
                    center: !1,
                    active: 0,
                    dataSetStatus: !1,
                    defaultClass: "step-default-class",
                    finishClass: "step-finish-class",
                    finishLine: !0,
                    finishLineClass: "step-finish-line-bg",
                    customClass: ""
                };
            return this.options = utils.extend(!0, defined, options), this.options.el ? this.options.data && 0 != this.options.data.length ? (this.options.dataLength = this.options.data.length, void this.render()) : (alert("请传入'data'参数"), !1) : (alert("请传入'el'参数"), !1)
        },
        getBoxClass: function () {
            var _this = this,
                options = this.options,
                direction = options.direction.toLowerCase(),
                boxClass = "";
            return "horizontal" == direction ? (boxClass += "steps-horizontal", options.center && (boxClass += " steps-center")) : "vertical" == direction ? boxClass += "steps-vertical" : alert("参数'direction'错误"), boxClass
        },
        getParentNode: function () {
            var _this = this,
                options = this.options,
                resultEl;
            return "object" == typeof options.el ? resultEl = options.el : "string" == typeof options.el && (resultEl = document.querySelector(options.el)), resultEl
        },
        render: function () {
            var _this = this,
                options = _this.options,
                boxHtml = "",
                parentNode = _this.getParentNode();
            options.customClass && (parentNode.className = parentNode.className + options.customClass), options.boxClass = _this.getBoxClass(), boxHtml = boxHtml + '<div class="steps ' + options.boxClass + '">';
            var stepContainer = "";
            if (options.data.forEach(function (currentValue, index, array) {
                    (currentValue[options.props.status] || 0 == currentValue[options.props.status]) && (options.dataSetStatus = !0);
                    var stepClass = currentValue[options.props.status] ? options.finishClass : !options.dataSetStatus && index <= options.active ? options.finishClass : "";
                    stepContainer = stepContainer + '<div class="step ' + options.defaultClass + " " + stepClass + '" style="' + _this.getStepStyle(index).join("") + '">{{stepHtml}}</div>';
                    var stepIconClass = "",
                        stepIconInnerClass = "",
                        stepIconInnerText = "",
                        iconType = options.iconType.toLowerCase();
                    "number" == iconType ? (stepIconInnerClass = "step-icon-number", stepIconInnerText = index + 1) : "bullets" == iconType ? stepIconInnerClass = "step-icon-bullets" : "custom" == iconType && (stepIconClass = "step-icon-custom-box", stepIconInnerClass = "step-icon-custom", stepIconInnerText = currentValue[options.props.icon] ? currentValue[options.props.icon] : index + 1);
                    var stepHtml, lineStyle = "",
                        titleStyle = "",
                        descStyle = "",
                        lineIndex = options.dataOrder.indexOf("line"),
                        titleIndex = options.dataOrder.indexOf("title"),
                        descIndex = options.dataOrder.indexOf("description"),
                        stepLineBox, stepTitle, stepDesc;

                    var iconBg = currentValue[options.props.status] ? options.finishClass : !options.dataSetStatus && index <= options.active ? "step-icon-finish-bg" : "step-icon-default-bg"; 

                    "vertical" == options.direction.toLowerCase() && options.dataWidth.length > 0 && (lineStyle = "flex: " + (options.dataWidth[lineIndex] ? options.dataWidth[lineIndex] : "none") + ";", titleStyle = "flex: " + (options.dataWidth[titleIndex] ? options.dataWidth[titleIndex] : "none") + ";", descStyle = "flex: " + (options.dataWidth[descIndex] ? options.dataWidth[descIndex] : "none") + ";"), stepHtml = '<div class="step-line-box" style="' + lineStyle + "order:" + lineIndex + '"><div class="step-line {{finishLineClass}}"></div><div class="step-icon ' + iconBg + ' ' + stepIconClass + '"><div class="' + stepIconInnerClass + '">' + stepIconInnerText + "</div></div></div>" + ('<div class="step-title" style="' + titleStyle + "order:" + titleIndex + '">' + currentValue[options.props.title] + "</div>") + ('<div class="step-description" style="' + descStyle + "order:" + descIndex + '">' + currentValue[options.props.description] + "</div>"), currentValue[options.props.customHtml] && (stepHtml += currentValue[options.props.customHtml]), stepContainer = stepContainer.replace("{{stepHtml}}", stepHtml)
                }), options.dataSetStatus) stepContainer = stepContainer.replace(/{{finishLineClass}}/g, "");
            else if (options.finishLine)
                for (var i = 0, len = options.dataLength; i < len; i++) stepContainer = i < options.active ? stepContainer.replace(/{{finishLineClass}}/, options.finishLineClass) : stepContainer.replace(/{{finishLineClass}}/, "");
            else stepContainer = stepContainer.replace(/{{finishLineClass}}/g, "");
            parentNode.innerHTML = boxHtml + stepContainer + "</div>"
        },
        getStepStyle: function (index) {
            var _this = this,
                options = this.options,
                style = [],
                space = "number" == typeof options.space ? options.space + "px" : options.space ? options.space : 100 / (options.dataLength - (options.center ? 0 : 1)) + "%";
            return style.push("flex-basis:" + space + ";"), options.center || index != options.dataLength - 1 || (style.length = 0, style.push("flex-basis: auto!important;"), style.push("flex-shrink: 0;"), style.push("flex-grow: 0;"), style.push("max-width:" + 100 / options.dataLength + "%;")), style
        },
        setActive: function (num) {
            var _this = this;
            this.options.dataSetStatus ? alert("参数'data'中已设置'status',参数'active'已停用") : (this.options.active = num, this.render())
        },
        getActive: function () {
            var _this = this;
            if (!this.options.dataSetStatus) return this.options.active;
            alert("参数'data'中已设置'status',参数'active'已停用")
        }
    }, _global = function () {
        return this || (0, eval)("this")
    }(), "undefined" != typeof module && module.exports ? module.exports = steps : "function" == typeof define && define.amd ? define(function () {
        return steps
    }) : !("steps" in _global) && (_global.steps = steps)
}();