jxdnsong
2020-10-23 a7929e6b3ec9ac17233f39e55a2b8ac63ea75f42
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
define([
    'dojo/_base/array',
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/dom-class',
    'dojo/on',
    'dojo/query',
    'dojo/string',
    'dojo/topic',
    'dijit/registry',
    'dijit/_WidgetBase',
    'dijit/_TemplatedMixin',
    'dijit/_WidgetsInTemplateMixin',
    'dijit/form/_FormMixin',
    './_ResizeMixin',
    '../data/config',
    'dojo/i18n!../nls/laboratory',
    'dojo/text!./templates/ColumnConfigForm.html',
    // for template
    'dijit/form/Button',
    'dijit/form/ComboBox',
    'dijit/form/FilteringSelect',
    'dijit/form/NumberTextBox',
    'dijit/form/RadioButton',
    'dijit/form/TextBox'
], function (arrayUtil, declare, lang, domClass, on, query, string, topic, registry, _WidgetBase, _TemplatedMixin,
    _WidgetsInTemplateMixin, _FormMixin, _ResizeMixin, config, i18n, template) {
 
    var defaultColumnValues = {
        // Standard column properties
        field: '',
        label: '',
        className: '',
        sortable: 'true',
 
        // Editor properties
        editor: '',
        editOn: '',
        autoSave: 'false',
        autoSelect: 'false',
        dismissOnEnter: 'true',
 
        // ColumnHider properties
        hidden: 'false',
        unhidable: 'false',
 
        // ColumnReorder properties
        reorderable: 'true',
 
        // ColumnResizer properties
        resizable: 'true',
 
        // Selector properties
        selector: ''
    };
 
    return declare([ _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _FormMixin, _ResizeMixin ], {
        baseClass: 'configForm column',
        templateString: template,
        i18n: i18n,
        docBaseUrl: config.docBaseUrl,
 
        _featureMidToNodeMap: null,
 
        buildRendering: function () {
            this.inherited(arguments);
 
            if (!this.containerNode) {
                this.containerNode = this.domNode;
            }
 
            var map = this._featureMidToNodeMap = {
                'dgrid/Editor': this.editorFields,
                'dgrid/extensions/ColumnHider': this.columnHiderFields,
                'dgrid/extensions/ColumnReorder': this.columnReorderFields,
                'dgrid/extensions/ColumnResizer': this.columnResizerFields,
                'dgrid/Selector': this.selectorFields
            };
 
            for (var k in map) {
                // Add legend labels programmatically
                var moduleName = k.slice(k.lastIndexOf('/') + 1);
                map[k].getElementsByTagName('legend')[0].innerHTML =
                    string.substitute(this.i18n.moduleProperties, [ moduleName ]);
            }
        },
 
        postCreate: function () {
            function shouldDismiss(editor) {
                return editor ? editor.toLowerCase().indexOf('textarea') === -1 : true;
            }
 
            this.inherited(arguments);
            this.own(
                topic.subscribe('/feature/select', lang.hitch(this, '_onFeatureSelect')),
                on(this.doneButton, 'click', lang.hitch(this, function () {
                    this.emit('close');
                })),
                this.watch('value', function (propertyName, oldValue, newValue) {
                    // Let the ColumnGrid know the column config has changed so it an update the store
                    topic.publish('/column/changed', newValue);
                    if (newValue.editor !== oldValue.editor) {
                        // Set a sane default for dismissOnEnter if switching to/from a textarea editor
                        var newDismiss = shouldDismiss(newValue.editor);
                        var oldDismiss = shouldDismiss(oldValue.editor);
                        if (newDismiss !== oldDismiss) {
                            this.set('value', lang.mixin(newValue, { dismissOnEnter: '' + newDismiss }));
                        }
                    }
                })
            );
        },
 
        _setValueAttr: function (value) {
            // Use default values for any unspecified fields
            this.inherited(arguments, [lang.mixin(lang.clone(defaultColumnValues), value)]);
            // Store the id - _FormMixin will discard this value, but we need it to persist the data back to the store
            this._id = value.id;
        },
 
        _getValueAttr: function () {
            var returnValue = this.inherited(arguments);
            var propertyName, k;
 
            for (k in this._getHiddenFieldNames()) {
                delete returnValue[k];
            }
 
            // Omit properties with default values
            for (propertyName in returnValue) {
                if (returnValue[propertyName] === defaultColumnValues[propertyName]) {
                    delete returnValue[propertyName];
                }
            }
 
            // Restore the id
            returnValue.id = this._id;
 
            return returnValue;
        },
 
        _getHiddenFieldNames: function () {
            var hiddenFieldNames = {};
            // Remove values from hidden fields
            query('fieldset.dijitHidden', this.domNode).forEach(function (fieldset) {
                arrayUtil.forEach(registry.findWidgets(fieldset), function (childWidget) {
                    hiddenFieldNames[childWidget.name] = true;
                });
            });
 
            return hiddenFieldNames;
        },
 
        _onFeatureSelect: function (featureMid, isEnabled) {
            var featureNode = this._featureMidToNodeMap[featureMid];
 
            if (featureNode) {
                domClass.toggle(featureNode, 'dijitHidden', !isEnabled);
 
                if (!isEnabled) {
                    // Close the dialog so input fields will reset if the user reenables the feature.
                    this.emit('close');
                    topic.publish('/columnConfig/hidden', this._getHiddenFieldNames());
                }
            }
        },
 
        _clearField: function (event) {
            var fieldName = event.target.getAttribute('data-field-name');
            var formValue = this.get('value');
 
            if (!fieldName) {
                return;
            }
 
            if (fieldName in formValue) {
                formValue[fieldName] = '';
                this.set('value', formValue);
            }
        }
    });
});