username@email.com
2023-02-28 67a0042c5f29e4bb0e0b82f6190f2bc51480b45c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(function ($, zou) {
    "use strict";
 
    $.lrCustmerFormDesigner = {
        init: function ($self, op) {
            var dfop = {
                data: [{// 选项卡数据
                    id: zou.newGuid(),
                    title: '主表信息',
                    columns: []
                }]
            }
            $.extend(dfop, op || {});
            dfop.id = $self.attr('id');
            $self[0].zCustmerFormDesigner = { dfop: dfop };
            $self.addClass('z-custmerform-designer-layout');
            var _html = '';
            _html += '<div class="custmerform-designer-layout-left"  id="lr_custmerform_compont_list_' + dfop.id + '"></div>';
 
            _html += '<div class="z-custmerform-designer-layout-center">';
            _html += '<div class="z-custmerform-designer-layout-header">';
            _html += '<div class="z-custmerform-designer-tabs" id="lr_custmerform_designer_tabs_' + dfop.id + '">';
            _html += '<ul class="nav nav-tabs z-form-tab">';
            _html += '</ul>';
            _html += '</div>';
            _html += '</div>';
 
            _html += '<div class="z-custmerform-designer-layout-area" id="lr_custmerform_designer_layout_area_' + dfop.id + '" ></div>';
            _html += '<div class="z-custmerform-designer-layout-footer">';
            //_html += '<div class="z-custmerform-designer-layout-footer-item" id="lr_custmerform_preview_btn_' + dfop.id + '"><i class="fa fa-eye"></i><span>预览表单</span></div>';
            _html += '</div>';
            _html += '<div class="z-custmerform-designer-layout-center-bg"><img src="/Scripts/Form/images/tableform.png" /></div>';
 
            _html += '</div>';
 
            _html += '<div class="z-custmerform-designer-layout-right" id="lr_custmerform_compont_property_' + dfop.id + '"></div>';
 
            $self.html(_html);
            $.lrCustmerFormDesigner.bind($self);
            $.lrCustmerFormDesigner.compontinit($self);
            $.lrCustmerFormDesigner.compontbind($self);
 
            $.lrCustmerFormDesigner.renderTabs($self);
            $.lrCustmerFormDesigner.renderComponts($self);
        },
        // 绑定表单设计器的全局事件
        bind: function ($self) {
 
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            // 优化滚动条
            $('#lr_custmerform_compont_list_' + dfop.id).mCustomScrollbar({
                theme: "minimal-dark"
            });
            $('#lr_custmerform_designer_tabs_' + dfop.id).mCustomScrollbar({
                axis: "x",
                theme: "minimal-dark"
            });
            $('#lr_custmerform_designer_layout_area_' + dfop.id).mCustomScrollbar({
                theme: "minimal-dark"
            });
            $('#lr_custmerform_compont_property_' + dfop.id).mCustomScrollbar({
                theme: "minimal-dark"
            });
            $('#lr_custmerform_designer_layout_area_' + dfop.id + ' .mCSB_container')[0].dfop = dfop;
 
            // 预览表单
            $self.find('#lr_custmerform_preview_btn_' + dfop.id).on('click', function () {
                $.lrCustmerFormDesigner.saveComponts($self);
                var url = "/FormDesigner/PreviewForm?key=" + window.keyValue;
                layer.open({
                    type: 2,
                    title: '预览表单',
                    shadeClose: true,
                    shade: 0.4,
                    area: ['80%', '80%'],
                    content: url
                });
            });
        },
        // 组件初始化
        compontinit: function ($self) {// 组件初始化
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            var $compontList = $self.find('#lr_custmerform_compont_list_' + dfop.id + ' .mCSB_container');
            $.each($.formComponents, function (i, component) {
                var $component = component.init();
                $compontList.append($component);
            });
            $compontList.find('.z-custmerform-component').draggable({
                connectToSortable: '#lr_custmerform_designer_layout_area_' + dfop.id + ' .mCSB_container',
                helper: "clone",
                revert: "invalid"
            });
 
            $('#lr_custmerform_designer_layout_area_' + dfop.id + ' .mCSB_container').sortable({
                opacity: 0.4,
                delay: 300,
                cursor: 'move',
                placeholder: "ui-state-highlight",
                stop: function (event, ui) {
                    var $compont = $(ui.item[0]);
                    var componttype = $compont.attr('data-type');
                    if (!!componttype) {//如果是第一次移入,需要对单元项进行初始化处理
                        $compont.addClass('z-compont-item').css({ 'width': '100%' });
                        $compont.removeClass('z-custmerform-component');
                        $compont.removeAttr('data-type');
                        $.formComponents[componttype].render($compont);
                        $compont[0].dfop.id = zou.newGuid();
                        $compont.trigger("click");
                    }
                    else {
                        $compont.trigger("click");
                    }
                },
                start: function (event, ui) {
                    $self.find(".z-custmerform-designer-layout-center-bg").hide();
                    var $highlight = $self.find(".ui-state-highlight");
                    $highlight.html('拖放控件到这里');
                    var $compont = $(ui.item[0]);
                    var componttype = $compont.attr('data-type');
                    if (!componttype) {
                        $highlight.css({ width: ((100 / $compont[0].dfop.proportion) + "%") });
                    }
                },
                out: function (event, ui) {
                    if (ui.helper != null) {
                        var $componts = $('.z-custmerform-designer-layout-area .mCSB_container .z-compont-item');
                        if ($componts.length <= 1) {
                            if ($componts.length === 1) {
                                if ($componts.find('.z-compont-value').length === 0) {
                                    $(".z-custmerform-designer-layout-center-bg").show();
                                }
                            }
                            else {
                                $(".z-custmerform-designer-layout-center-bg").show();
                            }
                        }
                    }
                }
            });
        },
        // 组件事件注册
        compontbind: function ($self) {
            $self.delegate('.z-compont-item', 'click', function () {
                var $this = $(this);
                if (!$this.hasClass('active')) {
                    $('.z-custmerform-designer-layout-area .mCSB_container .z-compont-item').removeClass('active');
                    $this.addClass('active');
                    if ($('.z-custmerform-designer-layout').css('padding-right') === '0px') {
                        $('.z-custmerform-designer-layout').animate({ 'padding-right': '240px', speed: 2000 });
                        $('.z-custmerform-designer-layout-right').animate({ right: 0, speed: 2000 });
                    }
                    setTimeout(function () {
                        $.formComponents[$this[0].dfop.type].property($this);
                    }, 150);
                }
            });
            $self.delegate('.z-compont-remove i', 'click', function () {
                var $compont = $(this).parents('.z-compont-item');
                $compont.remove();
                if ($('.z-custmerform-designer-layout-area .mCSB_container .z-compont-item').length === 0) {
                    $('.z-custmerform-designer-layout-right').animate({ right: '-240px', speed: 2000 });
                    $('.z-custmerform-designer-layout').animate({ 'padding-right': '0px', speed: 2000 });
                    $(".z-custmerform-designer-layout-center-bg").show();
                }
                else {
                    $('.z-custmerform-designer-layout-area .mCSB_container .z-compont-item').eq(0).trigger('click');
                }
            });
        },
        // 渲染选项卡
        renderTabs: function ($self) {// 渲染选项卡
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            var $tabs = $('#lr_custmerform_designer_tabs_' + dfop.id + ' ul');
            var tabsLength = dfop.data.length;
            var index = 0;
            $tabs.html("");
            for (var i = 0; i < tabsLength; i++) {
                var tabItem = dfop.data[i];
                $tabs.append('<li data-value="' + tabItem.id + '"><a>' + tabItem.text + '</a></li>');
                if (dfop._currentTabId === tabItem.id) {
                    index = i;
                }
            }
            // 获取当前选项卡的组件数据并渲染
            if (dfop._currentTabId !== dfop.data[index].id) {
                dfop._currentTabId = dfop.data[index].id;
                dfop._currentComponts = dfop.data[index].columns;
                dfop._isRenderComponts = true;
                $.lrCustmerFormDesigner.renderComponts($self);
            }
 
            if (tabsLength <= 1) {
                $self.find('.z-custmerform-designer-layout-center').removeClass('hasTab');
            }
            else {
                $self.find('.z-custmerform-designer-layout-center').addClass('hasTab');
                $tabs.find('li').eq(index).addClass('active');
            }
        },
        // 渲染数据
        renderData: function ($self) {
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            var $tabs = $('#lr_custmerform_designer_tabs_' + dfop.id + ' ul');
            var tabsLength = 1;
            $tabs.html("");
            for (var i = 0; i < tabsLength; i++) {
                var tabItem = dfop.data[i];
                $tabs.append('<li data-value="' + tabItem.id + '"><a>' + tabItem.text + '</a></li>');
                if (i === 0) {
                    dfop._currentTabId = tabItem.id;
                    dfop._currentComponts = dfop.data[0].columns;
                    dfop._isRenderComponts = true;
                    $.lrCustmerFormDesigner.renderComponts($self);
                }
            }
            if (tabsLength <= 1) {
                $self.find('.z-custmerform-designer-layout-center').removeClass('hasTab');
            }
            else {
                $self.find('.z-custmerform-designer-layout-center').addClass('hasTab');
                $tabs.find('li').eq(0).addClass('active');
            }
        },
        // 保存当前选项卡的组件数据
        saveComponts: function ($self) {
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            var columns = [];
            var compontsLayout = $('#lr_custmerform_designer_layout_area_' + dfop.id + ' .mCSB_container');
            compontsLayout.find('.z-compont-item').each(function () {
                var column = $(this)[0].dfop;
                columns.push(column);
            });
            for (var i = 0, l = dfop.data.length; i < l; i++) {
                if (dfop.data[i].id === dfop._currentTabId) {
                    dfop.data[i].columns = columns;
                    break;
                }
            }
        },
        // 渲染组件
        renderComponts: function ($self) {
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            if (dfop._isRenderComponts) {
                var compontsLayout = $('#lr_custmerform_designer_layout_area_' + dfop.id + ' .mCSB_container');
                compontsLayout.html('');
                //console.log(JSON.stringify(dfop._currentComponts));
                if (dfop._currentComponts.length > 0) {
                    $self.find(".z-custmerform-designer-layout-center-bg").hide();
                    for (var i = 0, l = dfop._currentComponts.length; i < l; i++) {
                        var compontItem = dfop._currentComponts[i];
                        var $column = $('<div class="z-compont-item" ></div>');
                        $column[0].dfop = compontItem;
                        $column.css({ 'width': 100 / parseInt(compontItem.proportion) + '%' });
                        $.formComponents[compontItem.type].render($column);
                        compontsLayout.append($column);
 
                        if (i === 0) {
                            $column.trigger("click");
                        }
                    }
                }
                else {
                    $('.z-custmerform-designer-layout-right').animate({ right: '-240px', speed: 2000 });
                    $('.z-custmerform-designer-layout').animate({ 'padding-right': '0px', speed: 2000 });
                    $(".z-custmerform-designer-layout-center-bg").show();
                }
                dfop._isRenderComponts = false;
            }
        },
        // 判定所有组件数据是否输入完整(主要是数据库绑定信息)
        validData: function ($self) {
            var dfop = $self[0].zCustmerFormDesigner.dfop;
            var _data = {};
            var res = true;
            for (var i = 0, l = dfop.data.length; i < l; i++) {
                for (var j = 0, jl = dfop.data[i].columns.length; j < jl; j++) {
                    if (dfop.data[i].columns[j].type !== 'label') {
                        var table = dfop.data[i].columns[j].table;
                        var field = dfop.data[i].columns[j].field;
                        var title = dfop.data[i].columns[j].title;
                        if (table !== '' && field !== '' && (table !== undefined && field !== undefined)) {
                            //console.log(JSON.stringify(_data));
                            if (!!_data[table + '_' + field]) {
                                layer.alert('【' + title + '】绑定数据表字段与【' + _data[table + '_' + field] + '】重复!');
                                res = false;
                            }
                            else {
                                _data[table + '_' + field] = title;
                            }
                        }
                        else {
                            if (dfop.data[i].columns[j].type === 'girdtable') {
                                if (table === '' || table === undefined) {
                                    layer.alert('【' + title + '】请绑定数据表!');
                                    res = false;
                                }
                            } else {
                                if (table === '' || table === undefined) {
                                    layer.alert('【' + title + '】请绑定数据表!');
                                    res = false;
                                }
                                else if (field === '' || field === undefined) {
                                    layer.alert('【' + title + '】请绑定数据字段!');
                                    res = false;
                                }
                            }
                        }
                    }
                }
            }
            return res;
        }
    };
 
    //对外暴露接口
    $.fn.lrCustmerFormDesigner = function (type, op) {
        var $this = $(this);
        if (!$this.attr('id')) {
            return false;
        }
        var dfop;
        switch (type) {
            // 初始化设计器
            case "init":
                $.lrCustmerFormDesigner.init($this, op);
                break;
                // 判定所有组件数据是否输入完整(主要是数据库绑定信息)
            case 'valid':
                $.lrCustmerFormDesigner.saveComponts($this);
                return $.lrCustmerFormDesigner.validData($this);
            case "get":
                $.lrCustmerFormDesigner.saveComponts($this);
                dfop = $this[0].zCustmerFormDesigner.dfop;
                var $json = dfop.data[0].columns;
                $.each($json, function (i, item) {
                    if (item != null && item != undefined && item.type === "girdtable") {//指标
                        dfop.data[0].columns.splice(i, 1);
                        json.children = item;
                    }
                });
                json.maindata = dfop.data;
                return json;
            case "set":
                dfop = $this[0].zCustmerFormDesigner.dfop;
                dfop.data = op.data;
                $.lrCustmerFormDesigner.renderData($this);
                break;
        }
    };
 
})(jQuery, top.zou);