liuyg
2021-07-02 25ce610f6ecca7325e7a743dc032c4a76559c63d
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
define([
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/dom-class',
    'dojo/topic',
    'dijit/_WidgetBase',
    'dijit/_TemplatedMixin',
    'dijit/_WidgetsInTemplateMixin',
    './_ResizeMixin',
    'dojo/i18n!../nls/laboratory',
    'dojo/text!./templates/ColumnGrid.html',
    'dgrid/Grid',
    'dgrid/Editor',
    'dgrid/extensions/DijitRegistry',
    'dgrid/extensions/DnD',
    'dgrid/extensions/SingleQuery',
    'dstore/Memory',
    'dstore/Trackable',
    // Widgets in template:
    'dijit/form/Form',
    'dijit/form/Button',
    'dijit/form/TextBox'
], function (declare, lang, domClass, topic, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _ResizeMixin, i18n,
    template, Grid, Editor, DijitRegistry, DnD, SingleQuery, Memory, Trackable) {
 
    function renderDragSourceCell(item, value, node) {
        domClass.add(node, 'dojoDndHandle');
        node.innerHTML = '<i class="icon-navicon" title="' + i18n.dragToMove + '"></i>';
    }
 
    return declare([ _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _ResizeMixin ], {
        baseClass: 'columnGridContainer',
        templateString: template,
        i18n: i18n,
 
        buildRendering: function () {
            this.inherited(arguments);
 
            this.store = new (declare([ Memory, Trackable ]))();
 
            this.grid = new (declare([ Grid, SingleQuery, Editor, DnD, DijitRegistry ], {
                columns: {
                    dragSource: {
                        label: '',
                        renderCell: renderDragSourceCell,
                        sortable: false
                    },
                    label: {
                        field: 'label',
                        label: i18n.label,
                        autoSave: true,
                        sortable: false
                    },
                    config: {
                        label: '',
                        formatter: function () {
                            return '<i class="icon-times" title="' + i18n['delete'] + '"></i>' +
                                '<i class="icon-gear" title="' + i18n.edit + '"></i> ';
                        },
                        sortable: false
                    }
                },
                showHeader: false,
                dndParams: {
                    withHandles: true
                }
            }))({
                collection: this.store,
                className: 'columnGrid dgrid-autoheight'
            }, this.gridNode);
 
            this._startupWidgets.push(this.grid);
        },
 
        postCreate: function () {
            this.inherited(arguments);
            this.own(
                this.store.on('add,delete,update', lang.hitch(this, '_onStoreChange')),
                this.grid.on('.icon-times:click', lang.hitch(this, 'removeColumn')),
                this.grid.on('.icon-gear:click', lang.hitch(this, '_editColumn')),
                topic.subscribe('/column/changed', lang.hitch(this, '_onColumnChange')),
                topic.subscribe('/columnConfig/hidden', lang.hitch(this, '_onFieldsHidden'))
            );
        },
 
        _getColumnsAttr: function () {
            return this.store.fetchSync();
        },
 
        addColumn: function (label) {
            // summary:
            //        Adds a column to the store with the given label;
            //        the column's field name will be based on the label provided
            this.store.put({
                field: label.replace(/[^\w-]/g, '_'),
                label: label
            });
        },
 
        removeColumn: function (target) {
            // summary:
            //        Removes the column from the store corresponding to the given target
            //        (whether an event, element, ID, or item)
 
            this.store.remove(this.grid.row(target).id);
        },
 
        _onFormSubmit: function (event) {
            // summary:
            //        Adds a column to the store from the UI values
 
            event.preventDefault();
            var form = this.columnGridForm;
            var value = form.get('value');
 
            if (!value.label) {
                return;
            }
 
            this.addColumn(value.label);
 
            form.reset();
            this.fieldLabelTextBox.focus();
        },
 
        _editColumn: function (event) {
            // summary:
            //        Shows the column configuration for a column
 
            var row = this.grid.row(event);
 
            // Let the ColumnEditor know that is should set the form data and display the form
            this.emit('editcolumn', { data: row.data });
        },
 
        _onColumnChange: function (value) {
            this.store.put(value);
        },
 
        _onStoreChange: function () {
            // Let the Laboratory know that it should update the demo display (grid or generated code)
            topic.publish('/configuration/changed');
            // Let the Tree config module know that is should update its list of column names
            topic.publish('/store/columns/update', this.store);
        },
 
        _onFieldsHidden: function (hiddenFieldNames) {
            this.store.forEach(function (columnConfig) {
                var k, updated;
                for (k in columnConfig) {
                    if (hiddenFieldNames[k]) {
                        delete columnConfig[k];
                        updated = true;
                    }
                }
                updated && this.store.put(columnConfig);
            }, this);
        }
    });
});