Project

General

Profile

1
/**
2
 * customAlerts.js
3
 * Author: Philippe Assis
4
 * Doc and repo: https://github.com/PhilippeAssis/custom-alert
5
 *
6
 * Alert e confirm personalizados.
7
 * FF, Chromer, IE(>=9)*
8
 *
9
 * 
10
 * window.customAlert e window.customConfirm devem permanecer com esses nomes,
11
 * a não ser que você saiba o que esta fazendo.
12
 */
13
var customKit = {
14
    createDiv: function (attr, name, parent) {
15
        var div = document.createElement("div");
16
        div.setAttribute(attr, name);
17
        if (parent) {
18
            var parent = document.getElementById(parent)
19
            parent.appendChild(div);
20
            return;
21
        }
22

    
23
        document.body.appendChild(div);
24
    },
25
    mergeObjects: function (obj1, obj2) {
26
        var obj3 = {};
27
        for (var attrname in obj1) {
28
            obj3[attrname] = obj1[attrname];
29
        }
30
        for (var attrname in obj2) {
31
            obj3[attrname] = obj2[attrname];
32
        }
33
        return obj3;
34
    }
35
};
36

    
37
function customAlert(options) {
38

    
39
    this.defaultOptions = {
40
        'ok': 'OK',
41
        'title': 'Alert!'
42
    };
43

    
44
    if (options)
45
        this.defaultOptions = customKit.mergeObjects(this.defaultOptions, options);
46

    
47
    this.options = this.defaultOptions;
48

    
49
    if (document.getElementById("customAlert") == null) {
50
        customKit.createDiv("id", "customAlert-overlay");
51
        customKit.createDiv("id", "customAlert");
52
        customKit.createDiv("class", "header", "customAlert");
53
        customKit.createDiv("class", "body", "customAlert");
54
        customKit.createDiv("class", "footer", "customAlert");
55

    
56
        //Os nomes podem ser alterados, window.alert e window.Alert, ao seu gosto!
57
        window.alert = window.Alert = function (dialog, options) {
58
            if (options)
59
                window.customAlert.options = customKit.mergeObjects(window.customAlert.options, options);
60
            window.customAlert.render(dialog);
61
        };
62
    }
63

    
64
    this.render = function (dialog) {
65
        alertBox = document.getElementById("customAlert");
66
        alertBox.getElementsByClassName("header")[0].innerHTML = this.options.title;
67
        alertBox.getElementsByClassName("body")[0].innerHTML = dialog;
68
        alertBox.getElementsByClassName("footer")[0].innerHTML = "<button onclick=\"window.customAlert.ok()\">" + this.options.ok + "</button>";
69
        document.getElementsByTagName("html")[0].style.overflow = "hidden";
70
        document.getElementById("customAlert-overlay").style.display = "block";
71
        alertBox.style.display = "block";
72
    };
73

    
74
    this.ok = function () {
75
        document.getElementById("customAlert").style.display = "none";
76
        document.getElementById("customAlert-overlay").style.display = "none";
77
        document.getElementsByTagName("html")[0].style.overflow = "auto";
78
        this.options = this.defaultOptions;
79
    }
80
}
81

    
82
function customConfirm(options) {
83

    
84
    this.defaultOptions = {
85
        'yes': 'YES',
86
        'no': 'NO',
87
        'title': 'Confirm it:',
88
        'return': false
89
    };
90

    
91
    if (options)
92
        this.defaultOptions = customKit.mergeObjects(this.defaultOptions, options);
93

    
94
    this.options = this.defaultOptions;
95

    
96
    if (document.getElementById("customConfirm") == null) {
97
        customKit.createDiv("id", "customConfirm-overlay");
98
        customKit.createDiv("id", "customConfirm");
99
        customKit.createDiv("class", "header", "customConfirm");
100
        customKit.createDiv("class", "body", "customConfirm");
101
        customKit.createDiv("class", "footer", "customConfirm");
102

    
103
        //Os nomes podem ser alterados, window.confirm e window.Confirm, ao seu gosto!
104
        window.confirm = window.Confirm = function (dialog, callback, options) {
105
            if (options)
106
                window.customConfirm.options = customKit.mergeObjects(window.customConfirm.options, options);
107
            window.customConfirm.render(dialog, callback);
108
        };
109
    }
110

    
111
    this.callback = function (data) {
112
        if (data != undefined) console.log(data)
113
    };
114

    
115
    this.render = function (dialog, callback) {
116
        this.callback = callback;
117
        confirmBox = document.getElementById("customConfirm");
118
        confirmBox.getElementsByClassName("header")[0].innerHTML = this.options.title;
119
        confirmBox.getElementsByClassName("body")[0].innerHTML = dialog;
120
        confirmBox.getElementsByClassName("footer")[0].innerHTML = "<button class=\"confirm\" onclick=\"window.customConfirm.ok()\">" + (this.options.yes) + "</button><button class=\"cancel\" onclick=\"window.customConfirm.cancel()\">" + (this.options.no) + "</button>";
121
        document.getElementsByTagName("html")[0].style.overflow = "hidden";
122
        document.getElementById("customConfirm-overlay").style.display = "block";
123
        confirmBox.style.display = "block";
124
    };
125

    
126
    this.ok = function () {
127
        this.end();
128
        if (this.options.return) {
129
            this.clear();
130
            this.callback(true);
131
            return;
132
        }
133

    
134
        this.clear();
135
        this.callback();
136
    }
137

    
138
    this.cancel = function () {
139
        this.end();
140
        if (this.options.return) {
141
            this.clear();
142
            this.callback(false);
143
            return;
144
        }
145
        this.clear();
146
    }
147

    
148
    this.end = function () {
149
        document.getElementById("customConfirm").style.display = "none";
150
        document.getElementById("customConfirm-overlay").style.display = "none";
151
        document.getElementsByTagName("html")[0].style.overflow = "auto";
152
    }
153

    
154
    this.clear = function () {
155
        this.options = this.defaultOptions;
156
    }
157
}
158

    
159
/*
160
 * window.customAlert e window.customConfirm devem permanecer com esses nomes, a não se que vc saiba o que esta fazendo.
161
 * Vocẽ pode adicionar configuraçãos na declaração de ambos, ex: new customConfirm({execute:false});
162
 * */
163
window.customAlert = new customAlert({title: ''});
164

    
165
window.customConfirm = new customConfirm({title: ''});
(3-3/13)