lyg
2025-03-04 72bbec1590f85974d369ce7aeaa05be8905672a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
mbui.define([], function (exports) {
    var doc = document, query = 'querySelectorAll', claname = 'getElementsByClassName', S = function (s) {
        return doc[query](s);
    };
 
    var template = {
        warning: function (text) {
            //警告提示
            return `<div class="mbui-toptips mbui-toptips-warning fixed" id="${classs}${index}">
                        <i class="mbui-toptips-warning-icon"></i><span>${text}</span>
                    </div>`;
        },
        error: function (text) {
            //失败提示
            return `<div class="mbui-toptips mbui-toptips-error fixed" id="${classs}${index}">
                         <i class="mbui-toptips-error-icon"></i><span>${text}</span>
                    </div>`;
        },
        toast: function (text) {
            //轻提示
            return `<div class="mbui-toast" id="${classs}${index}">
                        <div class="mbui-toast-backdrop"></div>
                        <div class="mbui-toast-dialog">
                            <div class="mbui-toast-content">
                                <span>${text}</span>
                            </div>
                        </div>
                    </div>`;
        },
        loading: function (text) {
            //轻提示
            return `<div class="mbui-toast mbui-toast-loading" id="${classs}${index}">
                        <div class="mbui-toast-backdrop"></div>
                        <div class="mbui-toast-dialog">
                            <div class="mbui-toast-content">
                                <span>
                                    <div class="mbui-toast-loading_icon">
                                        <div class="loading-icon-leaf loading-icon-leaf_0"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_1"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_2"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_3"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_4"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_5"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_6"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_7"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_8"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_9"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_10"></div>
                                        <div class="loading-icon-leaf loading-icon-leaf_11"></div>
                                    </div>
                                    ${text}
                                </span>
                            </div>
                        </div>
                    </div>`;
        },
        success: function (text) {
            //轻提示
            return `<div class="mbui-toast mbui-toast-success" id="${classs}${index}">
                        <div class="mbui-toast-backdrop"></div>
                        <div class="mbui-toast-dialog">
                            <div class="mbui-toast-content">
                                <span><i class="mbui-toast-success-icon"></i>${text}</span>
                            </div>
                        </div>
                    </div>`;
        }
    }
 
    //默认配置
    var config = {
        type: 1,
        shade: true,
        shadeClose: false,
        fixed: true
    };
 
    var ready = {
        extend: function (obj) {
            var newobj = JSON.parse(JSON.stringify(config));
            for (var i in obj) {
                newobj[i] = obj[i];
            }
            return newobj;
        },
        timer: {}, end: {}
    };
 
    //点触事件
    ready.touch = function (elem, fn) {
        elem.addEventListener('click', function (e) {
            fn.call(this, e);
        }, false);
    };
 
    var index = 0, classs = ['mbui-layer'], Layer = function (options) {
        var that = this;
        that.config = ready.extend(options);
        that.view();
    };
 
    Layer.prototype.view = function () {
        var that = this, config = that.config, layerbox = doc.createElement('div');
 
        that.id = layerbox.id = classs[0] + index;
        layerbox.setAttribute('class', classs[0] + ' ' + classs[0] + (config.type || 0));
        layerbox.setAttribute('index', index);
 
        //标题区域
        var title = (function () {
            var titype = typeof config.title === 'object';
            return config.title
                ? '<div class="modal-header" style="' + (titype ? config.title[1] : '') + '">' + (titype ? config.title[0] : config.title) + '</div>' : '';
        }());
 
        //按钮区域
        var button = (function () {
            typeof config.btn === 'string' && (config.btn = [config.btn]);
            var btns = (config.btn || []).length, btndom;
            if (btns === 0 || !config.btn) {
                return '';
            }
            btndom = '<button type="button" types="1" class="btn btn-block active">' + config.btn[0] + '</button>'
            if (btns === 2) {
                btndom = '<a href="javascript:void(0);" types="0" class="btn">' + config.btn[1] + '</a><button type="button" types="1" class="btn active">' + config.btn[0] + '</button>';
            }
            return '<div class="modal-footer clearfix">' + btndom + '</div>';
        }());
 
        if (!config.fixed) {
            config.top = config.hasOwnProperty('top') ? config.top : 100;
            config.style = config.style || '';
            config.style += ' top:' + (doc.body.scrollTop + config.top) + 'px';
        }
 
        if (config.type === 0) {
            config.shade = false;
            let content = template[config.template];
            layerbox.innerHTML = content(config.content);
        }
 
        if (config.type === 2) {
            config.content = '<div class="form-hairlines"><input type="text" id="prompt' + index + '" class="mbui-input" placeholder="请输入内容"></div>';
        }
        
        if (config.type === 3) {
            config.className = 'modal-view top';
            config.content = '<div style="padding:0 16px 16px;"><textarea rows="5" id="textarea' + index + '" class="mbui-textarea" placeholder="请输入内容"></textarea></div>';
        }
 
        if (config.type === 4) {
            config.className = 'modal-actionsheet bottom';
        }
 
        if (config.type === 5) {
            config.className = 'modal-view top';
        }
 
        if (config.type > 0) {
            layerbox.innerHTML = (config.shade ? '<div ' + (typeof config.shade === 'string' ? 'style="' + config.shade + '"' : '') + ' class="mbui-layershade"></div>' : '')
                + '<div class="mbui-layermain" ' + (!config.fixed ? 'style="position:static;"' : '') + '>'
                + '<div class="modal-dialog">'
                + '<div class="modal-content ' + (config.className ? config.className : '') + '" ' + (config.style ? 'style="' + config.style + '"' : '') + '>'
                + title
                + '<div class="modal-body">' + config.content + '</div>'
                + button
                + '</div>'
                + '</div>'
                + '</div>';
        }
 
        document.body.appendChild(layerbox);
        var elem = that.elem = S('#' + that.id)[0];
        config.success && config.success(elem,index);
        that.index = index++;
        that.action(config, elem);
    };
 
    Layer.prototype.action = function (config, elem) {
        var that = this;
        //自动关闭
        if (config.time && config.time > 0) {
            ready.timer[that.index] = setTimeout(function () {
                layer.close(that.index);
            }, config.time * 1000);
        }
 
        //确认取消
        var btn = function () {
            var type = this.getAttribute('types');
            if (type == 0) {
                config.no && config.no();
                layer.close(that.index);
            } else {
                config.yes ? config.yes(that.index) : layer.close(that.index);
            }
        };
 
        if (config.btn) {
            var btns = elem[claname]('modal-footer')[0].children, btnlen = btns.length;
            for (var ii = 0; ii < btnlen; ii++) {
                ready.touch(btns[ii], btn);
            }
        }
 
        //点遮罩关闭
        if (config.shade && config.shadeClose) {
            var shade = elem[claname]('mbui-layershade')[0];
            ready.touch(shade, function () {
                layer.close(that.index, config.end);
            });
        }
        config.end && (ready.end[that.index] = config.end);
    };
 
    var layer = {
        v: '1.0',
        index: index,
        //核心方法
        open: function (options) {
            var o = new Layer(options || {});
            return o.index;
        },
        alert: function (txt) {
            layer.open({
                type: 1,
                title: '提示',
                content: txt,
                btn: ['确定']
            });
        },
        confirm: function (txt, fun) {
            layer.open({
                type: 1,
                title: '温馨提示',
                content: txt,
                btn: ['确定', '取消'],
                yes: function (index) {
                    fun ? fun(index) : layer.close(index);
                }
            });
        },
        prompt: function (title,fun,txt) {
            layer.open({
                type: 2,
                title: title,
                content: txt,
                btn: ['确定', '取消'],
                yes: function (index) {
                    let val = $('#prompt' + index).val();
                    fun ? fun(val,index) : layer.close(index);
                }
            });
        },
        textarea: function (title,fun,txt) {
            layer.open({
                type: 3,
                title: title,
                content: txt,
                btn: ['确定', '取消'],
                yes: function (index) {
                    let val = $('#textarea' + index).val();
                    fun ? fun(val,index) : layer.close(index);
                }
            });
        },
        msg: function (txt = '', time = 3, template = 'toast') {
            let msg = layer.open({
                type: 0,
                content: txt,
                template: template,
                time: time
            });
            return msg;
        },
        success: function (txt) {
            layer.msg(txt, 3, 'success');
        },
        warning: function (txt) {
            layer.msg(txt, 3, 'warning');
        },
        error: function (txt) {
            layer.msg(txt, 3, 'error');
        },
        loading: function (txt) {
            let loading = layer.msg(txt, 0, 'loading');
            return loading;
        },
        photo: function (url) {
            var $image = $('<div class="layer-image-photo"><img src="' + url + '"></div>');
            $('body').append($image);
            $image.fadeIn();
            $image.click(function () {
                $(this).fadeOut(function () {
                    $(this).remove();
                });
            });
        },
        close: function (index) {
            var ibox = S('#' + classs[0] + index)[0];
            if (!ibox) return;
            ibox.innerHTML = '';
            doc.body.removeChild(ibox);
            clearTimeout(ready.timer[index]);
            delete ready.timer[index];
            typeof ready.end[index] === 'function' && ready.end[index]();
            delete ready.end[index];
        },
        //关闭所有layer层
        closeAll: function () {
            var boxs = doc[claname](classs[0]);
            for (var i = 0, len = boxs.length; i < len; i++) {
                layer.close((boxs[0].getAttribute('index') | 0));
            }
        }
    };
    // 输出接口
    exports('layer', layer);
});