naranja.min.js 4.76 KB
!function (t) {
    "undefined" != typeof module && module.exports ? module.exports = t : window.naranja = t
}(function () {
    function t(t, i) {
        var e = document.createElement(t);
        return i && i.forEach(function (t) {
            e.classList.add(t)
        }), e
    }

    var i = document.querySelector(".naranja-notification-box");
    return i || (i = t("div", ["naranja-notification-box"]), $newNotificationsAdvice = t("div", ["naranja-notification-advice"]), $newNotificationsAdvice.addEventListener("click", function () {
        i.scrollTop = "0"
    }), i.appendChild($newNotificationsAdvice), document.body.appendChild(i)), i.__proto__.unshifElement = function (t) {
        this.insertBefore(t, this.childNodes[0])
    }, i.addEventListener("scroll", function (t) {
        t.currentTarget.scrollTop < 20 && $newNotificationsAdvice.classList.remove("active")
    }), {
        log: function (t) {
            this.createNotification("log", t)
        },
        success: function (t) {
            this.createNotification("success", t)
        },
        warn: function (t) {
            this.createNotification("warn", t)
        },
        error: function (t) {
            this.createNotification("error", t)
        },
        createNotification: function (e, o) {
            this.type = e, this.title = o.title, this.text = o.text, this.icon = void 0 === o.icon || o.icon, this.buttons = o.buttons;
            var n = this.$createContainer(), a = n.querySelector("div");
            if (this.$notification = n, this.$body = a, this.icon) {
                var c = t("div", ["naranja-icon", "narj-icon-" + e]);
                c.innerHTML = this.chooseIcon[e], a.appendChild(c)
            }
            var r = this.createTitle(), s = this.createText(), l = t("div", ["naranja-text-and-title"]);
            if (l.appendChild(r), l.appendChild(s), a.appendChild(l), this.buttons) {
                var d = this.createButtons(n, a);
                a.querySelector(".naranja-text-and-title").appendChild(d)
            }
            var f, h = t("div", ["naranja-close-icon"]);
            h.addEventListener("click", function () {
                this.closeNotification()
            }.bind(this)), h.innerHTML = this.chooseIcon.close, a.appendChild(h), i.unshifElement(n), f = n, setTimeout(function () {
                var t = f.querySelector(".naranja-body-notification").offsetHeight;
                f.style.height = t + "px"
            }, 0), i.scrollTop > 20 && ($newNotificationsAdvice.classList.add("active"), $newNotificationsAdvice.innerHTML = this.chooseIcon.newNotification), "keep" !== o.timeout && setTimeout(function () {
                this.closeNotification()
            }.bind(this), o.timeout || 5e3)
        },
        $createContainer: function () {
            var i = t("div", ["naranja-notification", "naranja-" + this.type]),
                e = t("div", ["naranja-body-notification", "narj-" + this.type]);
            return i.appendChild(e), i
        },
        createTitle: function () {
            var i, e = t("p", ["naranja-title"]), o = (i = this.title, document.createTextNode(i));
            return e.appendChild(o), e
        },
        createText: function () {
            var i = t("p", ["naranja-parragraph"]), e = document.createTextNode(this.text);
            return i.appendChild(e), i
        },
        createButtons: function (i, e) {
            var o = t("div", ["naranja-buttons-container"]), n = this;
            return this.buttons.forEach(function (i) {
                var e = t("button");
                e.appendChild(document.createTextNode(i.text)), e.addEventListener("click", function (t) {
                    n.removeNotification = !0, t.preventClose = function () {
                        n.removeNotification = !1
                    }, t.closeNotification = function () {
                        n.closeNotification()
                    }, i.click(t), n.removeNotification && n.closeNotification()
                }), o.appendChild(e)
            }), o
        },
        closeNotification: function () {
            var t = this;
            this.elementWasRemoved || (t.$body.style.opacity = "0", setTimeout(function () {
                t.$body.style.marginTop = "0px", t.$body.style.marginBottom = "0px", t.$body.style.padding = "0px", t.$notification.style.height = "0px", t.$notification.style.padding = "0px", setTimeout(function () {
                    t.$notification.parentNode.removeChild(t.$notification)
                }, 600), i.scrollTop < 20 && $newNotificationsAdvice.classList.remove("active")
            }, 150)), this.elementWasRemoved = !0
        },
        chooseIcon: {
            log: '',
            success: '',
            warn: '',
            error: '',
            close: '',
            newNotification: ''
        }
    }
});