nnnjjj123
2020-11-17 1b2c1edb61190eeb19f465ff031eaa3b2a1b8dbc
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
///////////////////////////////////////////////////////////////////////////
// Copyright © 2014 - 2018 Esri. All Rights Reserved.
//
// Licensed under the Apache License Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////
 
define([
  'dojo/aspect',
  'dojo/Deferred',
  'dojo/_base/lang',
  'dojo/_base/html',
  'dojo/_base/array',
  'dojo/on',
  'dojo/_base/declare',
  './ValueProvider',
  'dijit/_TemplatedMixin',
  'dijit/_WidgetsInTemplateMixin',
  'dojo/text!./ListValueProvider.html',
  'dojo/store/Memory',
  'jimu/utils',
  'dijit/form/FilteringSelect'
],
  function(aspect, Deferred, lang, html, array, on, declare, ValueProvider, _TemplatedMixin, _WidgetsInTemplateMixin,
    template, Memory, jimuUtils) {
 
    return declare([ValueProvider, _TemplatedMixin, _WidgetsInTemplateMixin], {
      templateString: template,
      codedValues: null,//[{value,label}] for coded values and sub types
      staticValues: null,//[{value,label}]
      showNullValues: false,//show null values
      layerDataChanged: false, //layer data update status
      ifDropDown: false,
 
      postCreate: function(){
        this.inherited(arguments);
        html.addClass(this.domNode, 'jimu-filter-list-value-provider');
 
        this._uniqueValueCache = {};
        this.noDataTips = '<div class="error-tip-section" style="display: block;">' +
                          '<span class="jimu-icon jimu-icon-error"></span>' +
                          '<span class="jimu-state-error-text">' + this.nls.noFilterValueTip + '</span></div>';
 
        //[{id,value,label}]
        var store = new Memory({idProperty:'id', data: []});
        this.valuesSelect.set('store', store);
 
        //is numberical field
        this.isNumberField = jimuUtils.isNumberField(this.fieldInfo.type);
 
        if(!this.staticValues && typeof this.valuesSelect._onDropDownMouseDown === 'function'){
          if(!this.codedValues || (this.codedValues && this.filterCodedValue)){
            this.own(
              aspect.before(this.valuesSelect,
                            "_onDropDownMouseDown",
                            lang.hitch(this, this._onBeforeDropDownMouseDown))
            );
            this.own(on(document.body, 'click', lang.hitch(this, this._onBodyClick)));
 
            if(this.layerInfo){ //it always exsits because it's required from valueProviderFactory constructor
              //it will tragger after the  add/remove/update events happen
              this.layerInfo.getLayerObject().then(lang.hitch(this, function(layerObject){
                layerObject.on("edits-complete", lang.hitch(this, function() {
                  this.layerDataChanged = true;
                }));
              }));
            }
          }
        }
      },
 
      _onFilteringSelectInput: function(){
        this.emit('change');
      },
 
      _getCodedValueLabelsBySubTypeId:function(){
        // var newExpr = this.getDropdownFilterExpr();
        // return jimuUtils.getCodedValueLabelsByExprs(this.layerDefinition, this.fieldName, newExpr, this.codedValues);
        var newParj = this.getDropdownFilterPartsObj();
        return this.getCodedValueListByPartsObj(this.layerDefinition, this.fieldName, newParj, this.codedValues);
      },
 
      _onBeforeDropDownMouseDown: function(){
        this.ifDropDown = true;
        this._tryUpdatingUniqueValues(undefined, true);
        return arguments;
      },
 
      _onBodyClick: function(evt){
        var target = evt.target || evt.srcElement;
        if(target === this.domNode || html.isDescendant(target, this.domNode)){
          return;
        }
        if(this.msgDiv){
          html.setStyle(this.msgDiv, "display", "none");
        }
      },
 
      getDijits: function(){
        return [this.valuesSelect];
      },
 
      isValidValue: function(){
        return this.getStatus() > 0;
      },
 
      //-1 means invalid value type
      //0 means empty value, this ValueProvider should be ignored
      //1 means valid value
      getStatus: function(){
        var item = this.valuesSelect.get('item');
        if(item){
          if(item.value !== undefined){ //if(item.label !== ''){
            if(this.isNumberField && !jimuUtils.isValidNumber(item.value)){
              var newVal = parseFloat(item.value);
              if(jimuUtils.isValidNumber(newVal)){
                item.value = newVal;
                return this._getStatusForDijit(this.valuesSelect);
              }
              return -1;
            }
            return this._getStatusForDijit(this.valuesSelect);
          }else{
            return 0;
          }
        }else{
          return 0;
        }
      },
 
      //return -1 means input a wrong value
      //return 0 means empty value
      //return 1 means valid value
      _getStatusForDijit: function(dijit){
        if(dijit.validate()){
          if(dijit.get("DisplayedValue")){
            return 1;
          }else{
            return 0;
          }
        }else{
          return -1;
        }
      },
 
      //maybe return a deferred
      setValueObject: function(valueObj){
        if(this.staticValues){
          return this._setValueForStaticValues(valueObj.value, this.staticValues);
        } else if(this.codedValues){
          if(this.filterCodedValue){
 
            return this._tryUpdatingUniqueValues(valueObj.value, false);
          }else{
            return this._setValueForStaticValues(valueObj.value, this.codedValues);
          }
        } else{
          return this._tryUpdatingUniqueValues(valueObj.value, false);
        }
      },
 
      getValueObject: function(){
        if(this.isValidValue()){
          var item = this.valuesSelect.get('item');
          var value = item.value;
          return {
            "isValid": true,
            "type": this.partObj.valueObj.type,
            "value": value
          };
        }
        return null;
      },
 
      tryGetValueObject: function(){
        if(this.isValidValue()){
          return this.getValueObject();
        }else if(this.isEmptyValue()){
          return {
            "isValid": true,
            "type": this.partObj.valueObj.type,
            "value": this.shortType === 'string' ? "" : null
          };
        }
        return null;
      },
 
      setRequired: function(required){
        this.valuesSelect.set("required", required);
      },
 
      /*disable: function(){
        this.inherited(arguments);
        this.valuesSelect.closeDropDown(true);
        this.valuesSelect.set('disabled', true);
      },
 
      enable: function(){
        this.inherited(arguments);
        this.valuesSelect.set('disabled', false);
      },*/
 
      _setValueForStaticValues: function(selectedValue, valueLabels){
        //selectedValue maybe undefined or null
        var data = null;
        var selectedId = -1;
        var selectedItem = null;
        if(valueLabels){
          data = array.map(valueLabels, lang.hitch(this, function(item, index){
            var dataItem = {
              id: index,
              value: item.value,
              label: item.label
            };
            if(dataItem.value + '' === selectedValue + ''){
              selectedId = index;
            }
            return dataItem;
          }));
          this.valuesSelect.store.setData(data);
          if(selectedId >= 0){
            selectedItem = this.valuesSelect.store.get(selectedId);
            if(selectedItem){
              this.valuesSelect.set('item', selectedItem);
            }
          }
          this._checkIfNoData();
        }
      },
 
      _uniqueValueLoadingDef: null,
      _uniqueValueLoadingExpr: '',
      _uniqueValueCache: null,//{expr1:values1,expr2:values2}
 
      _tryUpdatingUniqueValues: function(selectedValue, showDropDownAfterValueUpdate){
        var def = new Deferred();
        if(!this.valuesSelect._opened){
          var newExpr = this.getDropdownFilterExpr();
          if(newExpr !== this._uniqueValueLoadingExpr || this.layerDataChanged){
            //expr changed
            this.valuesSelect.readOnly = true;
            if(this._uniqueValueLoadingDef){
              this._uniqueValueLoadingDef.reject();
              this._uniqueValueLoadingDef = null;
            }
            this._uniqueValueLoadingExpr = newExpr;
            this._uniqueValueLoadingDef = this._getUniqueValues(newExpr);
            this._uniqueValueLoadingDef.then(lang.hitch(this, function(valueLabels){
              if(!this.domNode){
                return;
              }
              this._uniqueValueLoadingDef = null;
              this.valuesSelect.readOnly = false;
              this._setValueForUniqueValues(selectedValue, valueLabels);
              this._hideLoadingIcon();
              if(showDropDownAfterValueUpdate){
                this.valuesSelect.toggleDropDown();
              }
              this._checkIfNoData();
              def.resolve();
            }), lang.hitch(this, function(err){
              console.error(err);
              if(!this.domNode){
                return;
              }
              this._uniqueValueLoadingDef = null;
              this.valuesSelect.readOnly = false;
              this._hideLoadingIcon();
              this._checkIfNoData();
              def.reject(err);
            }));
          }else{
            this._checkIfNoData();
            def.resolve();
          }
        }else{
          this._checkIfNoData();
          def.resolve();
        }
        return def;
      },
 
      //return a deferred
      _setValueForUniqueValues: function(selectedValue, valueLabels){
        valueLabels.sort(function(item1, item2){
          if(item1.value < item2.value){
            return -1;
          }else if(item1.value === item2.value){
            return 0;
          }else{
            return 1;
          }
        });
        //selectedValue maybe undefined or null
        if(!this.showNullValues){
          valueLabels = array.filter(valueLabels, lang.hitch(this, function(item){
            return item.value !== '<Null>' && item.value !== null;
          }));
        }
        if(selectedValue === undefined){
          var currentValueObj = this.getValueObject();
          if(currentValueObj){
            selectedValue = currentValueObj.value;
          }
        }
        var selectedId = -1;
        var selectedItem = null;
        var data = array.map(valueLabels, lang.hitch(this, function(item, index) {
          var dataItem = {
            id: index,
            value: item.value,
            label: item.label
          };
 
          if (item.value + '' === selectedValue + '') {
            selectedId = index;
          }
 
          return dataItem;
        }));
 
        this.valuesSelect.store.setData(data);
 
        if (selectedId >= 0) {
          selectedItem = this.valuesSelect.store.get(selectedId);
        }
 
        //selectedItem maybe null
        //we need to set item to null to clear the previous invlaid value
        this.valuesSelect.set('item', selectedItem);
      },
 
      _checkIfNoData: function(){
        if(this.runtime && this.ifDropDown){
          this.ifDropDown = false;
          var dataList = this.valuesSelect.store.data;
          if (dataList.length === 0) {
            if(!this.msgDiv){
              this.msgDiv = document.createElement('div');
              html.addClass(this.msgDiv, "jimu-filter-list-value-provider-tip-container");
              this.msgDiv.innerHTML = this.noDataTips;
              this.valuesSelect.domNode.parentNode.appendChild(this.msgDiv);
            }else{
              html.setStyle(this.msgDiv, "display", "block");
            }
          }
        }
      },
 
      _showLoadingIcon: function(){
        html.addClass(this.valuesSelect.domNode, 'loading');
      },
 
      _hideLoadingIcon: function(){
        html.removeClass(this.valuesSelect.domNode, 'loading');
      },
 
      _getUniqueValues: function(where){
        var def = new Deferred();
        if(this._uniqueValueCache[where] && !this.layerDataChanged){
          def.resolve(this._uniqueValueCache[where]);
        }else{
          this._showLoadingIcon();
          jimuUtils.getUniqueValues(this.url, this.fieldName, where, this.layerDefinition, this.fieldPopupInfo)
          .then(lang.hitch(this, function(valueLabels){
            if(!this.domNode){
              return;
            }
            this._uniqueValueCache[where] = valueLabels;
            def.resolve(valueLabels);
            this._hideLoadingIcon();
          }), lang.hitch(this, function(err){
            if(!this.domNode){
              return;
            }
            def.reject(err);
            this._hideLoadingIcon();
          }));
        }
        this.layerDataChanged = false;//reset default value
        return def;
      }
 
    });
  });