|
| 1 | +/** |
| 2 | + * @module mdeditor |
| 3 | + * @submodule components-input |
| 4 | + */ |
| 5 | + |
1 | 6 | import Ember from 'ember';
|
2 | 7 | import MdCodelist from '../md-codelist/component';
|
3 | 8 |
|
4 | 9 | export default MdCodelist.extend({
|
5 |
| - |
| 10 | + classNames: ['md-codelist-multi'], |
6 | 11 | /**
|
7 | 12 | * Specialized select list control for displaying and selecting
|
8 | 13 | * options in mdCodes codelists.
|
9 | 14 | * Extends md-codelist.
|
10 |
| - * Allows selection of multiple options. |
| 15 | + * Allows selection of multiple options. |
11 | 16 | *
|
12 | 17 | * @class md-codelist-multi
|
13 | 18 | * @constructor
|
| 19 | + * @extends md-codelist |
14 | 20 | */
|
15 |
| - |
| 21 | + |
16 | 22 | /**
|
17 |
| - * Initial value, returned value. |
18 |
| - * Accepts either an Array of strings or JSON formatted array |
19 |
| - * of strings. Example: '["foo","bar"]' |
| 23 | + * Initial value, returned value. |
| 24 | + * Accepts an Array of strings. |
| 25 | + * |
| 26 | + * Example: `["foo","bar"]` |
20 | 27 | *
|
21 | 28 | * @property value
|
22 | 29 | * @type Array
|
23 | 30 | * @return Array
|
24 | 31 | * @required
|
25 | 32 | */
|
26 |
| - |
27 |
| - /** |
28 |
| - * Indicates whether to allow the user to enter a new option |
29 |
| - * not contained in the select list. |
30 |
| - * |
31 |
| - * @property create |
32 |
| - * @type Boolean |
33 |
| - * @default false |
34 |
| - */ |
35 |
| - |
36 |
| - /** |
37 |
| - * Indicates if tooltips should be rendered for the options. |
38 |
| - * |
39 |
| - * @property tooltip |
40 |
| - * @type Boolean |
41 |
| - * @default false |
42 |
| - */ |
43 |
| - |
| 33 | + |
44 | 34 | /**
|
45 |
| - * Indicates if icons should be rendered. |
| 35 | + * The multiple property for power-select-with-create |
46 | 36 | *
|
47 |
| - * @property icon |
| 37 | + * @property multiple |
| 38 | + * @private |
48 | 39 | * @type Boolean
|
49 |
| - * @default false |
| 40 | + * @default true |
50 | 41 | */
|
51 |
| - |
| 42 | + multiple: true, |
| 43 | + |
52 | 44 | /**
|
53 |
| - * Whether to render a button to clear the selection. |
| 45 | + * The component to render |
54 | 46 | *
|
55 |
| - * @property allowClear |
56 |
| - * @type Boolean |
57 |
| - * @default false |
| 47 | + * @property theComponent |
| 48 | + * @type Ember.computed |
| 49 | + * @return String |
58 | 50 | */
|
59 |
| - |
| 51 | + theComponent: Ember.computed('create', function () { |
| 52 | + return this.get('create') ? 'power-select-with-create' : |
| 53 | + 'power-select-multiple'; |
| 54 | + }), |
| 55 | + |
60 | 56 | /**
|
61 | 57 | * Whether to close the selection list after a selection has been made.
|
62 | 58 | *
|
63 | 59 | * @property closeOnSelect
|
64 | 60 | * @type Boolean
|
65 |
| - * @default true |
| 61 | + * @default false |
66 | 62 | */
|
67 |
| - |
| 63 | + closeOnSelect: false, |
| 64 | + |
68 | 65 | /**
|
69 | 66 | * The string to display when no option is selected.
|
70 | 67 | *
|
71 | 68 | * @property placeholder
|
72 | 69 | * @type String
|
73 |
| - * @default 'Select one option' |
| 70 | + * @default 'Select one or more options' |
74 | 71 | */
|
75 |
| - |
76 |
| - /** |
77 |
| - * Form label for select list |
78 |
| - * |
79 |
| - * @property label |
80 |
| - * @type String |
81 |
| - * @default null |
82 |
| - */ |
83 |
| - |
| 72 | + placeholder: 'Select one or more options', |
| 73 | + |
84 | 74 | /**
|
85 |
| - * Form field width |
| 75 | + * The currently selected item in the codelist |
86 | 76 | *
|
87 |
| - * @property width |
88 |
| - * @type String |
89 |
| - * @default 100% |
| 77 | + * @property selectedItem |
| 78 | + * @type Ember.computed |
| 79 | + * @return PromiseObject |
90 | 80 | */
|
91 |
| - |
| 81 | + selectedItem: Ember.computed('value', function () { |
| 82 | + let value = this.get('value'); |
| 83 | + let codelist = this.get('codelist'); |
| 84 | + |
| 85 | + if(value) { |
| 86 | + return codelist.filter((item) => { |
| 87 | + return value.includes(item['codeName']); |
| 88 | + }); |
| 89 | + } |
| 90 | + return null; |
| 91 | + }), |
| 92 | + |
92 | 93 | /**
|
93 |
| - * Indicates if input is disabled |
| 94 | + * If a value is provided by the user which is not in the codelist and 'create=true' |
| 95 | + * the new value will be added into the codelist array |
94 | 96 | *
|
95 |
| - * @property disabled |
96 |
| - * @type Boolean |
97 |
| - * @default false |
98 |
| - */ |
99 |
| - |
100 |
| - /* |
101 |
| - * codelist is an array of code objects in mdCodelist format |
102 |
| - * the initial codelist for 'mdCodeName' is provided by the 'codelist' service; |
103 |
| - * if a value is provided by the user which is not in the codelist and 'create=true' |
104 |
| - * the new value will be added into the codelist array; |
105 |
| - * then a Boolean 'selected' element will be added to each codelist object where the |
106 |
| - * selected option will be set to true and all others false. |
| 97 | + * @property codelist |
| 98 | + * @type Ember.computed |
| 99 | + * @return Array |
107 | 100 | */
|
108 |
| - codelist: Ember.computed(function() { |
109 |
| - let codelist = []; |
110 |
| - let codelistName = this.get('mdCodeName'); |
111 |
| - let mdCodelist = this.get('mdCodes') |
112 |
| - .get(codelistName) |
113 |
| - .codelist |
114 |
| - .sortBy('codeName'); |
115 |
| - mdCodelist.forEach(function(item) { |
116 |
| - let newObject = { |
117 |
| - code: item['code'], |
118 |
| - codeName: item['codeName'], |
119 |
| - description: item['description'], |
120 |
| - selected: false |
121 |
| - }; |
122 |
| - codelist.pushObject(newObject); |
123 |
| - }); |
124 |
| - |
125 |
| - let val = this.get('value'); |
126 |
| - let selectedItems = typeof val === 'string' ? JSON.parse(val) : val; |
| 101 | + codelist: Ember.computed('value', function () { |
| 102 | + let codelist = this.get('mapped'); |
| 103 | + let value = this.get('value'); |
127 | 104 | let create = this.get('create');
|
128 |
| - if(selectedItems) { |
| 105 | + |
| 106 | + if(value) { |
129 | 107 | if(create) {
|
130 |
| - selectedItems.forEach(function(selectedItem) { |
131 |
| - let mdIndex = -1; |
132 |
| - codelist.forEach(function(codeObject, cIndex) { |
133 |
| - if(selectedItem === codeObject['codeName']) { |
134 |
| - mdIndex = cIndex; |
135 |
| - } |
136 |
| - }); |
137 |
| - if(mdIndex === -1) { |
138 |
| - let newObject = { |
139 |
| - code: Math.floor(Math.random() * 100000) + 1, |
140 |
| - codeName: selectedItem, |
141 |
| - description: 'Undefined', |
142 |
| - selected: false |
143 |
| - }; |
| 108 | + value.forEach((val) => { |
| 109 | + let found = codelist.findBy('codeName', val); |
| 110 | + if(found === undefined) { |
| 111 | + let newObject = this.createCode(val); |
144 | 112 | codelist.pushObject(newObject);
|
145 | 113 | }
|
146 | 114 | });
|
147 | 115 | }
|
148 |
| - codelist.forEach(function(item) { |
149 |
| - let mdIndex = selectedItems.indexOf(item['codeName']); |
150 |
| - if(mdIndex > -1) { |
151 |
| - item['selected'] = true; |
152 |
| - } |
153 |
| - }); |
154 | 116 | }
|
155 | 117 |
|
156 | 118 | return codelist;
|
157 |
| - }) |
| 119 | + }), |
| 120 | + |
| 121 | + /** |
| 122 | + * Set the value on the select. |
| 123 | + * |
| 124 | + * @method setValue |
| 125 | + * @param {Array|Object} selected The value to set. Generally, an array of |
| 126 | + * selected objects, unless using the create option. |
| 127 | + */ |
| 128 | + setValue(selected) { |
| 129 | + let sel; |
| 130 | + |
| 131 | + //power-select-with-create always sends a single object oncreate |
| 132 | + //we need to add that object to the selectedItem array |
| 133 | + if(this.get('create') && !Ember.isArray(selected)) { |
| 134 | + sel = this.get('selectedItem') |
| 135 | + .compact(); |
| 136 | + sel.pushObject(selected); |
| 137 | + } else { |
| 138 | + sel = selected; |
| 139 | + } |
| 140 | + |
| 141 | + this.set('value', sel.mapBy('codeName')); |
| 142 | + this.change(); |
| 143 | + } |
158 | 144 | });
|
0 commit comments