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 | 1× 2× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 30× 30× 40× 40× 40× 40× 40× 41× 23× 18× 18× 6× 6× 6× 9× 9× 12× 18× 60× 60× 84× 27× 57× 32× 25× 75× 75× 72× 72× 72× 32× 32× 32× 72× 23× 23× 23× 14× 14× 23× 49× 49× 32× 4× 4× 4× 28× 6× 6× 6× 10× 10× 6× 22× 22× 72× 23× 7× 16× 13× 6× 7× 11× 11× 11× 15× 15× 11× 11× 11× 7× 3× 3× 1× 1× 1× 2× 49× 32× 32× 39× 11× 21× 21× | import Ember from 'ember'; import Base from '../mixins/base'; import PromiseResolver from 'ember-promise-tools/mixins/promise-resolver'; const _proxyCallback = function(callbackName) { return function(value, text, $element) { return this.get(`attrs.${callbackName}`)(this._getObjectOrValue(value), text, $element, this); }; }; export default Ember.Component.extend(Base, PromiseResolver, { module: 'dropdown', classNames: ['ui', 'dropdown'], ignorableAttrs: ['selected'], objectMap: null, init() { this._super(...arguments); this.set('objectMap', {}); }, willDestroyElement() { this._super(...arguments); this.set('objectMap', null); }, // Semantic Hooks willInitSemantic(settings) { this._super(...arguments); Eif (settings.onChange) { settings.onChange = this.get('_onChange'); } Iif (settings.onAdd) { settings.onAdd = this.get('_onAdd'); } Iif (settings.onRemove) { settings.onRemove = this.get('_onRemove'); } }, didInitSemantic() { this._super(...arguments); // We want to handle this outside of the standard process this.get('_settableAttrs').removeObject('selected'); // We need to ensure the internal value is set to '', // otherwise when we get the value later it is undefined // and semantic returns the module instead of the actual value this.execute('clear'); this._inspectSelected(); }, didUpdateAttrs() { this._super(...arguments); this._inspectSelected(); }, actions: { mapping(object) { let guid = Ember.guidFor(object); Eif (!this._hasOwnProperty(this.get('objectMap'), guid)) { this.get('objectMap')[guid] = object; } Ember.run.scheduleOnce('afterRender', this, this._inspectSelected); return guid; } }, // Method proxies _onChange(value, text, $element) { // Semantic calls the events on any 'set {action}' // Because of that we want to ignore calls when we are // Specifically setting the value if (this.get('_isSettingSelect')) { return; } let returnValue; if (this.execute('is multiple')) { let values = this.execute('get values'); returnValue = []; for (let i = 0; i < Ember.get(values, 'length'); i++) { let item = this._atIndex(values, i); returnValue.push(this._getObjectOrValue(item)); } } else { returnValue = this._getObjectOrValue(value); } return this.attrs.onChange(returnValue, text, $element, this); }, _onAdd: _proxyCallback('onAdd'), _onRemove: _proxyCallback('onRemove'), // Private methods _atIndex(collection, index) { Iif (typeof collection.objectAt === 'function') { return collection.objectAt(index); } return collection[index]; }, _getObjectOrValue(value) { if (this._hasOwnProperty(this.get('objectMap'), value)) { return this.get('objectMap')[value]; } if (Ember.isEmpty(value)) { return null; } return value; }, _inspectSelected() { let selected = this.get('selected'); return this.resolvePromise(selected, this._checkSelected); }, _checkSelected(selectedValue) { let isMultiple = this.execute('is multiple'); let moduleSelected = this._getCurrentSelected(isMultiple); if (!this._areSelectedEqual(selectedValue, moduleSelected, isMultiple)) { this.set('_isSettingSelect', true); this._setCurrentSelected(selectedValue, moduleSelected, isMultiple); this.set('_isSettingSelect', false); } }, _getCurrentSelected(isMultiple) { if (isMultiple) { let keys = this.execute('get values'); let returnValues = []; for (let i = 0; i < keys.length; i++) { let key = this._atIndex(keys, i); returnValues.push(this._getObjectOrValue(key)); } return returnValues; } let key = this.execute('get value'); return this._getObjectOrValue(key); }, _setCurrentSelected(selectedValue, moduleSelected, isMultiple) { if (Ember.isBlank(selectedValue)) { Eif (!Ember.isBlank(moduleSelected)) { this.execute('clear'); } return; } if (Ember.isArray(selectedValue)) { let keys = []; Iif (!isMultiple) { Ember.Logger.error("Selected is an array of values, but the dropdown doesn't have the class 'multiple'"); return; } for (let i = 0; i < Ember.get(selectedValue, 'length'); i++) { let item = this._atIndex(selectedValue, i); keys.push(this._getObjectKeyByValue(item)); } return this.execute('set exactly', keys); } let key = this._getObjectKeyByValue(selectedValue); return this.execute('set selected', key); }, _areSelectedEqual(selectedValue, moduleValue, isMultiple) { if (isMultiple) { // If selectedValue passed in is an array, we are assuming that its the collection getting updated and that // all module values must equal the attrValues // If both are in a blank state of some kind, they are equal. // i.e. selected could be null and moduleValue could be an empty array if (Ember.isBlank(selectedValue) && Ember.isBlank(moduleValue)) { return true; } if (Ember.isArray(selectedValue)) { if (Ember.get(selectedValue, 'length') !== Ember.get(moduleValue, 'length')) { return false; } // Loop through the collections and see if they are equal for (let i = 0; i < Ember.get(selectedValue, 'length'); i++) { let value = this._atIndex(selectedValue, i); let equal = false; for (let j = 0; j < Ember.get(moduleValue, 'length'); j++) { let module = this._atIndex(moduleValue, j); if (this.areAttrValuesEqual('selected', value, module)) { equal = true; break; } } Iif (!equal) { return false; } } // If we didn't return, the arrays are equal return true; } // otherwise, just try to see one of the values in the module equals the attr value // The use case is the selected value is a single value to start, then the module value is an array else Eif (Ember.isArray(moduleValue)) { for (let i = 0; i < Ember.get(moduleValue, 'length'); i++) { let item = this._atIndex(moduleValue, i); Eif (this.areAttrValuesEqual('selected', selectedValue, item)) { return true; // We found a match, just looking for one } } return false; } } return this.areAttrValuesEqual('selected', selectedValue, moduleValue); }, _getObjectKeyByValue(value) { // Since semantic is always binding to strings, we must return a string // Either through the object mapping or directly stringed value let objectMap = this.get('objectMap'); for (let key in objectMap) { if (objectMap[key] === value) { return key; } } Iif (value == null) { return ''; } return value.toString(); } }); |