slim-select 1.27.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/.nvmrc +1 -0
  2. package/.prettierrc +6 -0
  3. package/.vscode/extensions.json +3 -0
  4. package/.vscode/settings.json +15 -5
  5. package/README.md +2 -1
  6. package/dist/helper.d.ts +1 -4
  7. package/dist/index.d.ts +38 -58
  8. package/dist/render.d.ts +109 -0
  9. package/dist/select.d.ts +31 -20
  10. package/dist/settings.d.ts +33 -0
  11. package/dist/slimselect.cjs.js +2 -0
  12. package/dist/slimselect.cjs.js.map +1 -0
  13. package/dist/slimselect.css +1 -2
  14. package/dist/slimselect.css.map +1 -0
  15. package/dist/slimselect.es.js +2 -0
  16. package/dist/slimselect.es.js.map +1 -0
  17. package/dist/slimselect.global.js +2 -0
  18. package/dist/slimselect.global.js.map +1 -0
  19. package/dist/slimselect.js +2 -1970
  20. package/dist/slimselect.js.map +1 -0
  21. package/dist/slimselect.umd.js +2 -0
  22. package/dist/slimselect.umd.js.map +1 -0
  23. package/dist/store.d.ts +71 -0
  24. package/docs/assets/check-mark.svg +6 -0
  25. package/docs/assets/data.js +59 -0
  26. package/docs/assets/home.css +1 -0
  27. package/docs/assets/home.js +1 -0
  28. package/docs/assets/index.css +1 -0
  29. package/docs/assets/index.js +16 -0
  30. package/docs/assets/index2.js +274 -0
  31. package/docs/assets/index3.js +126 -0
  32. package/docs/assets/index4.js +60 -0
  33. package/docs/assets/install.js +34 -0
  34. package/docs/assets/selects.js +55 -0
  35. package/docs/index.html +18 -5
  36. package/docs.go +3 -1
  37. package/index.html +16 -0
  38. package/jest.config.js +7 -0
  39. package/package.json +39 -35
  40. package/src/docs/app.vue +185 -76
  41. package/src/docs/assets/scss/_variables.scss +26 -10
  42. package/src/docs/assets/scss/alert.scss +12 -0
  43. package/src/docs/assets/scss/animations.scss +5 -3
  44. package/src/docs/assets/scss/button.scss +21 -0
  45. package/src/docs/assets/scss/index.scss +8 -6
  46. package/src/docs/assets/scss/input.scss +2 -2
  47. package/src/docs/assets/scss/layout.scss +195 -65
  48. package/src/docs/assets/scss/misc.scss +144 -34
  49. package/src/docs/assets/scss/prism.scss +155 -153
  50. package/src/docs/env.d.ts +12 -0
  51. package/src/docs/images/check-mark.svg +6 -0
  52. package/src/docs/images/chrome.svg +22 -0
  53. package/src/docs/images/firefox.svg +58 -0
  54. package/src/docs/images/github.png +0 -0
  55. package/src/docs/images/ie.svg +11 -0
  56. package/src/docs/images/npm.png +0 -0
  57. package/src/docs/images/opera.svg +15 -0
  58. package/src/docs/images/safari.svg +22 -0
  59. package/src/docs/main.ts +27 -0
  60. package/src/docs/pages/data.vue +130 -0
  61. package/src/docs/pages/events/addable.vue +79 -0
  62. package/src/docs/pages/events/before_after_change.vue +140 -0
  63. package/src/docs/pages/events/before_after_open_close.vue +122 -0
  64. package/src/docs/pages/events/error.vue +59 -0
  65. package/src/docs/pages/events/index.vue +33 -0
  66. package/src/docs/pages/events/search.vue +154 -0
  67. package/src/docs/pages/events/search_filter.vue +45 -0
  68. package/src/docs/pages/home.vue +96 -116
  69. package/src/docs/pages/install.vue +47 -74
  70. package/src/docs/pages/methods/destroy.vue +66 -0
  71. package/src/docs/pages/methods/enable_disable.vue +70 -0
  72. package/src/docs/pages/methods/get_data.vue +81 -0
  73. package/src/docs/pages/methods/get_selected.vue +78 -0
  74. package/src/docs/pages/methods/index.vue +36 -0
  75. package/src/docs/pages/methods/open_close.vue +63 -0
  76. package/src/docs/pages/methods/search.vue +62 -0
  77. package/src/docs/pages/methods/set_data.vue +106 -0
  78. package/src/docs/pages/methods/set_selected.vue +70 -0
  79. package/src/docs/pages/selects.vue +108 -114
  80. package/src/docs/pages/settings/always_open.vue +88 -0
  81. package/src/docs/pages/settings/close_on_select.vue +69 -0
  82. package/src/docs/pages/settings/content_location.vue +64 -0
  83. package/src/docs/pages/settings/content_position.vue +86 -0
  84. package/src/docs/pages/settings/css.vue +73 -0
  85. package/src/docs/pages/settings/data_attributes.vue +39 -0
  86. package/src/docs/pages/settings/deselect.vue +69 -0
  87. package/src/docs/pages/settings/display.vue +88 -0
  88. package/src/docs/pages/settings/hide_selected.vue +73 -0
  89. package/src/docs/pages/settings/html.vue +86 -0
  90. package/src/docs/pages/settings/index.vue +73 -0
  91. package/src/docs/pages/settings/mandatory.vue +91 -0
  92. package/src/docs/pages/settings/min_max.vue +61 -0
  93. package/src/docs/pages/settings/open_position.vue +60 -0
  94. package/src/docs/pages/settings/placeholder.vue +72 -0
  95. package/src/docs/pages/settings/search.vue +140 -0
  96. package/src/docs/pages/settings/select.vue +32 -0
  97. package/src/docs/pages/settings/select_by_group.vue +46 -0
  98. package/src/docs/pages/settings/show_tooltip.vue +44 -0
  99. package/src/docs/pages/settings/styles.vue +51 -0
  100. package/src/docs/router.ts +41 -13
  101. package/src/slim-select/helper.ts +25 -86
  102. package/src/slim-select/index.ts +317 -420
  103. package/src/slim-select/render.test.ts +39 -0
  104. package/src/slim-select/render.ts +1129 -0
  105. package/src/slim-select/rollup.config.mjs +39 -0
  106. package/src/slim-select/select.test.ts +155 -0
  107. package/src/slim-select/select.ts +280 -108
  108. package/src/slim-select/settings.ts +68 -0
  109. package/src/slim-select/slimselect.scss +263 -278
  110. package/src/slim-select/store.test.ts +220 -0
  111. package/src/slim-select/store.ts +320 -0
  112. package/src/slim-select/tsconfig.json +11 -5
  113. package/src/vue/slimselect.vue +61 -0
  114. package/tsconfig.json +10 -29
  115. package/vite.config.ts +18 -0
  116. package/.browserslistrc +0 -4
  117. package/.postcssrc.js +0 -5
  118. package/.travis.yml +0 -22
  119. package/babel.config.js +0 -5
  120. package/dist/config.d.ts +0 -83
  121. package/dist/data.d.ts +0 -50
  122. package/dist/slim.d.ts +0 -50
  123. package/dist/slimselect.min.css +0 -1
  124. package/dist/slimselect.min.js +0 -1
  125. package/dist/slimselect.min.mjs +0 -1
  126. package/docs/css/app.d5840d21.css +0 -1
  127. package/docs/css/chunk-vendors.fa8199af.css +0 -1
  128. package/docs/css/home.57020178.css +0 -1
  129. package/docs/css/install.baa5d3fb.css +0 -1
  130. package/docs/css/methods.728c742a.css +0 -1
  131. package/docs/css/options.b28a32ef.css +0 -1
  132. package/docs/css/selects.1a7e1e6a.css +0 -1
  133. package/docs/js/app.238ee382.js +0 -2
  134. package/docs/js/app.238ee382.js.map +0 -1
  135. package/docs/js/chunk-vendors.5125e3d8.js +0 -21
  136. package/docs/js/chunk-vendors.5125e3d8.js.map +0 -1
  137. package/docs/js/home.e7b28581.js +0 -2
  138. package/docs/js/home.e7b28581.js.map +0 -1
  139. package/docs/js/install.d4300783.js +0 -2
  140. package/docs/js/install.d4300783.js.map +0 -1
  141. package/docs/js/methods.fa1608f1.js +0 -11
  142. package/docs/js/methods.fa1608f1.js.map +0 -1
  143. package/docs/js/options.2c4ceb87.js +0 -2
  144. package/docs/js/options.2c4ceb87.js.map +0 -1
  145. package/docs/js/selects.1eeba693.js +0 -2
  146. package/docs/js/selects.1eeba693.js.map +0 -1
  147. package/src/docs/pages/methods.vue +0 -427
  148. package/src/docs/pages/options.vue +0 -1480
  149. package/src/main.ts +0 -29
  150. package/src/shims-vue.d.ts +0 -8
  151. package/src/slim-select/config.ts +0 -118
  152. package/src/slim-select/data.ts +0 -377
  153. package/src/slim-select/slim.ts +0 -775
  154. package/tslint.json +0 -26
  155. package/vue.config.js +0 -23
package/src/main.ts DELETED
@@ -1,29 +0,0 @@
1
- import Vue from 'vue'
2
- import router from '@/docs/router'
3
-
4
- import app from '@/docs/app.vue'
5
- import Prism from 'prismjs'
6
- import Normalizer from 'prismjs/plugins/normalize-whitespace/prism-normalize-whitespace'
7
- import 'prismjs/plugins/toolbar/prism-toolbar'
8
- import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard'
9
-
10
- import '@/docs/assets/scss/index.scss'
11
- import '@/slim-select/slimselect.scss'
12
-
13
- new Normalizer({
14
- 'remove-trailing': true,
15
- 'remove-indent': true,
16
- 'left-trim': true,
17
- 'right-trim': true
18
- })
19
-
20
- Vue.mixin({
21
- updated() {
22
- Prism.highlightAll()
23
- }
24
- })
25
-
26
- new Vue({
27
- router,
28
- render: (h) => h(app)
29
- }).$mount('#app')
@@ -1,8 +0,0 @@
1
- declare module '*.vue' {
2
- import Vue from 'vue'
3
- export default Vue
4
- }
5
-
6
- declare module 'chance'
7
- declare module 'prismjs'
8
- declare module 'prismjs/*'
@@ -1,118 +0,0 @@
1
- import { Option } from './data'
2
-
3
- interface Constructor {
4
- select: HTMLSelectElement
5
- isAjax: boolean
6
- showSearch?: boolean
7
- searchPlaceholder?: string
8
- searchText?: string
9
- searchingText?: string
10
- searchFocus?: boolean
11
- searchHighlight?: boolean
12
- searchFilter?: (opt: Option, search: string) => boolean
13
- closeOnSelect?: boolean
14
- showContent?: string
15
- placeholderText?: string
16
- allowDeselect?: boolean
17
- allowDeselectOption?: boolean
18
- hideSelectedOption?: boolean
19
- deselectLabel?: string
20
- isEnabled?: boolean
21
- valuesUseText?: boolean
22
- showOptionTooltips?: boolean
23
- selectByGroup?: boolean
24
- limit?: number
25
- timeoutDelay?: number
26
- addToBody?: boolean
27
- }
28
-
29
- export class Config {
30
- public id: string = ''
31
- public style: string
32
- public class: string[]
33
- public isMultiple: boolean = false
34
- public isAjax: boolean = false
35
- public isSearching: boolean = false
36
- public showSearch: boolean = true
37
- public searchFocus: boolean = true
38
- public searchHighlight: boolean = false
39
- public closeOnSelect: boolean = true
40
- public showContent: string = 'auto' // options: auto, up, down
41
- public searchPlaceholder: string = 'Search'
42
- public searchText: string = 'No Results'
43
- public searchingText: string = 'Searching...'
44
- public placeholderText: string = 'Select Value'
45
- public allowDeselect: boolean = false
46
- public allowDeselectOption: boolean = false
47
- public hideSelectedOption: boolean = false
48
- public deselectLabel: string = 'x'
49
- public isEnabled: boolean = true
50
- public valuesUseText: boolean = false
51
- public showOptionTooltips: boolean = false
52
- public selectByGroup: boolean = false
53
- public limit: number = 0
54
- public timeoutDelay: number = 200
55
- public addToBody: boolean = false
56
-
57
- // Classes
58
- public readonly main: string = 'ss-main'
59
- public readonly singleSelected: string = 'ss-single-selected'
60
- public readonly arrow: string = 'ss-arrow'
61
- public readonly multiSelected: string = 'ss-multi-selected'
62
- public readonly add: string = 'ss-add'
63
- public readonly plus: string = 'ss-plus'
64
- public readonly values: string = 'ss-values'
65
- public readonly value: string = 'ss-value'
66
- public readonly valueText: string = 'ss-value-text'
67
- public readonly valueDelete: string = 'ss-value-delete'
68
- public readonly content: string = 'ss-content'
69
- public readonly open: string = 'ss-open'
70
- public readonly openAbove: string = 'ss-open-above'
71
- public readonly openBelow: string = 'ss-open-below'
72
- public readonly search: string = 'ss-search'
73
- public readonly searchHighlighter: string = 'ss-search-highlight'
74
- public readonly addable: string = 'ss-addable'
75
- public readonly list: string = 'ss-list'
76
- public readonly optgroup: string = 'ss-optgroup'
77
- public readonly optgroupLabel: string = 'ss-optgroup-label'
78
- public readonly optgroupLabelSelectable: string = 'ss-optgroup-label-selectable'
79
- public readonly option: string = 'ss-option'
80
- public readonly optionSelected: string = 'ss-option-selected'
81
- public readonly highlighted: string = 'ss-highlighted'
82
- public readonly disabled: string = 'ss-disabled'
83
- public readonly hide: string = 'ss-hide'
84
-
85
- constructor(info: Constructor) {
86
- this.id = 'ss-' + Math.floor(Math.random() * 100000)
87
- this.style = info.select.style.cssText
88
- this.class = info.select.className.split(' ')
89
-
90
- this.isMultiple = info.select.multiple
91
- this.isAjax = info.isAjax
92
- this.showSearch = (info.showSearch === false ? false : true)
93
- this.searchFocus = (info.searchFocus === false ? false : true)
94
- this.searchHighlight = (info.searchHighlight === true ? true : false)
95
- this.closeOnSelect = (info.closeOnSelect === false ? false : true)
96
- if (info.showContent) { this.showContent = info.showContent }
97
- this.isEnabled = (info.isEnabled === false ? false : true)
98
- if (info.searchPlaceholder) { this.searchPlaceholder = info.searchPlaceholder }
99
- if (info.searchText) { this.searchText = info.searchText }
100
- if (info.searchingText) { this.searchingText = info.searchingText }
101
- if (info.placeholderText) { this.placeholderText = info.placeholderText }
102
- this.allowDeselect = (info.allowDeselect === true ? true : false)
103
- this.allowDeselectOption = (info.allowDeselectOption === true ? true : false)
104
- this.hideSelectedOption = (info.hideSelectedOption === true ? true : false)
105
- if (info.deselectLabel) { this.deselectLabel = info.deselectLabel }
106
- if (info.valuesUseText) { this.valuesUseText = info.valuesUseText }
107
- if (info.showOptionTooltips) { this.showOptionTooltips = info.showOptionTooltips }
108
- if (info.selectByGroup) { this.selectByGroup = info.selectByGroup }
109
- if (info.limit) { this.limit = info.limit }
110
- if (info.searchFilter) { this.searchFilter = info.searchFilter }
111
- if (info.timeoutDelay != null) { this.timeoutDelay = info.timeoutDelay }
112
- this.addToBody = (info.addToBody === true ? true : false)
113
- }
114
-
115
- public searchFilter(opt: Option, search: string): boolean {
116
- return opt.text.toLowerCase().indexOf(search.toLowerCase()) !== -1
117
- }
118
- }
@@ -1,377 +0,0 @@
1
- import SlimSelect from './index'
2
-
3
- interface Constructor { main: SlimSelect }
4
-
5
- export type dataArray = dataObject[]
6
- export type dataObject = Optgroup | Option
7
- export interface Optgroup {
8
- label: string
9
- options?: Option[]
10
- }
11
-
12
- export interface Option {
13
- id?: string
14
- value?: string
15
- text: string
16
- innerHTML?: string
17
- selected?: boolean
18
- display?: boolean
19
- disabled?: boolean
20
- placeholder?: boolean|string
21
- class?: string
22
- style?: string
23
- data?: object
24
- mandatory?: boolean
25
- }
26
-
27
- // Class is responsible for managing the data
28
- export class Data {
29
- public main: SlimSelect
30
- public searchValue: string
31
- public data: dataObject[]
32
- public filtered: dataObject[] | null
33
- public contentOpen: boolean = false
34
- public contentPosition: string = 'below'
35
- public isOnChangeEnabled: boolean = true
36
- constructor(info: Constructor) {
37
- this.main = info.main
38
- this.searchValue = ''
39
- this.data = []
40
- this.filtered = null
41
-
42
- this.parseSelectData()
43
- this.setSelectedFromSelect()
44
- }
45
-
46
- public newOption(info: any): Option {
47
- return {
48
- id: (info.id ? info.id : String(Math.floor(Math.random() * 100000000))),
49
- value: (info.value ? info.value : ''),
50
- text: (info.text ? info.text : ''),
51
- innerHTML: (info.innerHTML ? info.innerHTML : ''),
52
- selected: (info.selected ? info.selected : false),
53
- display: (info.display !== undefined ? info.display : true),
54
- disabled: (info.disabled ? info.disabled : false),
55
- placeholder: (info.placeholder ? info.placeholder : false),
56
- class: (info.class ? info.class : undefined),
57
- data: (info.data ? info.data : {}),
58
- mandatory: (info.mandatory ? info.mandatory : false)
59
- }
60
- }
61
-
62
- // Add to the current data array
63
- public add(data: Option) {
64
- this.data.push({
65
- id: String(Math.floor(Math.random() * 100000000)),
66
- value: data.value,
67
- text: data.text,
68
- innerHTML: '',
69
- selected: false,
70
- display: true,
71
- disabled: false,
72
- placeholder: false,
73
- class: undefined,
74
- mandatory: data.mandatory,
75
- data: {}
76
- })
77
- }
78
-
79
- // From passed in select element pull optgroup and options into data
80
- public parseSelectData() {
81
- this.data = []
82
- // Loop through nodes and create data
83
- const nodes = (this.main.select.element as HTMLSelectElement).childNodes as any as HTMLOptGroupElement[] | HTMLOptionElement[]
84
- for (const n of nodes) {
85
- if (n.nodeName === 'OPTGROUP') {
86
- const node = n as HTMLOptGroupElement
87
- const optgroup = {
88
- label: node.label,
89
- options: [] as Option[]
90
- }
91
- const options = n.childNodes as any as HTMLOptionElement[]
92
- for (const o of options) {
93
- if (o.nodeName === 'OPTION') {
94
- const option = this.pullOptionData(o as HTMLOptionElement)
95
- optgroup.options.push(option)
96
-
97
- // If option has placeholder set to true set placeholder text
98
- if (option.placeholder && option.text.trim() !== '') {
99
- this.main.config.placeholderText = option.text
100
- }
101
- }
102
- }
103
- this.data.push(optgroup)
104
- } else if (n.nodeName === 'OPTION') {
105
- const option = this.pullOptionData(n as HTMLOptionElement)
106
- this.data.push(option)
107
-
108
- // If option has placeholder set to true set placeholder text
109
- if (option.placeholder && option.text.trim() !== '') {
110
- this.main.config.placeholderText = option.text
111
- }
112
- }
113
- }
114
- }
115
-
116
- // From passed in option pull pieces of usable information
117
- public pullOptionData(option: HTMLOptionElement): Option {
118
- return {
119
- id: (option.dataset ? option.dataset.id : false) || String(Math.floor(Math.random() * 100000000)),
120
- value: option.value,
121
- text: option.text,
122
- innerHTML: option.innerHTML,
123
- selected: option.selected,
124
- disabled: option.disabled,
125
- placeholder: option.dataset.placeholder === 'true',
126
- class: option.className,
127
- style: option.style.cssText,
128
- data: option.dataset,
129
- mandatory: (option.dataset ? option.dataset.mandatory === 'true' : false)
130
- }
131
- }
132
-
133
- // From select element get current selected and set selected
134
- public setSelectedFromSelect(): void {
135
- if (this.main.config.isMultiple) {
136
- const options = this.main.select.element.options as any as HTMLOptionElement[]
137
- const newSelected: string[] = []
138
- for (const o of options) {
139
- if (o.selected) {
140
- const newOption = this.getObjectFromData(o.value, 'value')
141
- if (newOption && newOption.id) {
142
- newSelected.push(newOption.id)
143
- }
144
- }
145
- }
146
- this.setSelected(newSelected, 'id')
147
- } else {
148
- const element = this.main.select.element
149
-
150
- // Single select element
151
- if (element.selectedIndex !== -1) {
152
- const option = element.options[element.selectedIndex] as HTMLOptionElement
153
- const value = option.value
154
- this.setSelected(value, 'value')
155
- }
156
- }
157
- }
158
-
159
- // From value set the selected value
160
- public setSelected(value: string | string[], type = 'id'): void {
161
- // Loop through data and set selected values
162
- for (const d of this.data) {
163
- // Deal with optgroups
164
- if (d.hasOwnProperty('label')) {
165
- if (d.hasOwnProperty('options')) {
166
- const options = (d as Optgroup).options
167
- if (options) {
168
- for (const o of options) {
169
- // Do not select if its a placeholder
170
- if (o.placeholder) {continue}
171
-
172
- o.selected = this.shouldBeSelected(o, value, type)
173
- }
174
- }
175
- }
176
- } else {
177
- (d as Option).selected = this.shouldBeSelected((d as Option), value, type)
178
- }
179
- }
180
- }
181
-
182
- // Determines whether or not passed in option should be selected based upon possible values
183
- public shouldBeSelected(option: Option, value: string | string[], type: string = 'id'): boolean {
184
- if (Array.isArray(value)) {
185
- for (const v of value) {
186
- if (type in option && String((option as any)[type]) === String(v)) {
187
- return true
188
- }
189
- }
190
- } else {
191
- if (type in option && String((option as any)[type]) === String(value)) {
192
- return true
193
- }
194
- }
195
-
196
- return false
197
- }
198
-
199
- // From data get option | option[] of selected values
200
- // If single select return last selected value
201
- public getSelected(): Option | Option[] {
202
- let value: Option = { text: '', placeholder: this.main.config.placeholderText } // Dont worry about setting this(make typescript happy). If single a value will be selected
203
- const values: Option[] = []
204
- for (const d of this.data) {
205
- // Deal with optgroups
206
- if (d.hasOwnProperty('label')) {
207
- if (d.hasOwnProperty('options')) {
208
- const options = (d as Optgroup).options
209
- if (options) {
210
- for (const o of options) {
211
- if (o.selected) {
212
- // If single return option
213
- if (!this.main.config.isMultiple) {
214
- value = o
215
- } else {
216
- // Push to multiple array
217
- values.push(o)
218
- }
219
- }
220
- }
221
- }
222
- }
223
- } else {
224
- // Push options to array
225
- if ((d as Option).selected) {
226
- // If single return option
227
- if (!this.main.config.isMultiple) {
228
- value = d as Option
229
- } else {
230
- // Push to multiple array
231
- values.push(d as Option)
232
- }
233
- }
234
- }
235
- }
236
-
237
- // Either return array or object or null
238
- if (this.main.config.isMultiple) {
239
- return values
240
- }
241
- return value
242
- }
243
-
244
- // If select type is multiple append value and set selected
245
- public addToSelected(value: string, type = 'id') {
246
- if (this.main.config.isMultiple) {
247
- const values = []
248
- const selected = this.getSelected()
249
- if (Array.isArray(selected)) {
250
- for (const s of selected) {
251
- values.push((s as any)[type])
252
- }
253
- }
254
- values.push(value)
255
-
256
- this.setSelected(values, type)
257
- }
258
- }
259
-
260
- // Remove object from selected
261
- public removeFromSelected(value: string, type = 'id') {
262
- if (this.main.config.isMultiple) {
263
- const values = []
264
- const selected = this.getSelected() as Option[]
265
- for (const s of selected) {
266
- if (String((s as any)[type]) !== String(value)) {
267
- values.push((s as any)[type])
268
- }
269
- }
270
-
271
- this.setSelected(values, type)
272
- }
273
- }
274
-
275
- // Trigger onChange callback
276
- public onDataChange(): void {
277
- if (this.main.onChange && this.isOnChangeEnabled) {
278
- this.main.onChange(JSON.parse(JSON.stringify(this.getSelected())))
279
- }
280
- }
281
-
282
- // Take in a value loop through the data till you find it and return it
283
- public getObjectFromData(value: string, type = 'id'): Option | null {
284
- for (const d of this.data) {
285
- // If option check if value is the same
286
- if (type in d && String((d as any)[type]) === String(value)) {
287
- return d as Option
288
- }
289
- // If optgroup loop through options
290
- if (d.hasOwnProperty('options')) {
291
- const optgroupObject = d as Optgroup
292
- if (optgroupObject.options) {
293
- for (const oo of optgroupObject.options) {
294
- if (String((oo as any)[type]) === String(value)) {
295
- return oo
296
- }
297
- }
298
- }
299
- }
300
- }
301
-
302
- return null
303
- }
304
-
305
- // Take in search string and return filtered list of values
306
- public search(search: string) {
307
- this.searchValue = search
308
- if (search.trim() === '') { this.filtered = null; return }
309
-
310
- const searchFilter = this.main.config.searchFilter
311
- const valuesArray = this.data.slice(0)
312
- search = search.trim()
313
- const filtered = valuesArray.map((obj) => {
314
- // If optgroup
315
- if (obj.hasOwnProperty('options')) {
316
- const optgroupObj = obj as Optgroup
317
- let options: Option[] = []
318
- if (optgroupObj.options) {
319
- options = optgroupObj.options.filter((opt) => {
320
- return searchFilter(opt, search)
321
- })
322
- }
323
- if (options.length !== 0) {
324
- const optgroup = (Object as any).assign({}, optgroupObj) // Break pointer
325
- optgroup.options = options
326
- return optgroup
327
- }
328
- }
329
-
330
- // If single option
331
- if (obj.hasOwnProperty('text')) {
332
- const optionObj = obj as Option
333
- if (searchFilter(optionObj, search)) { return obj }
334
- }
335
-
336
- return null
337
- })
338
-
339
- // Filter out false values
340
- this.filtered = filtered.filter((info) => info)
341
- }
342
- }
343
-
344
- export function validateData(data: dataArray): boolean {
345
- if (!data) { console.error('Data must be an array of objects'); return false }
346
- let isValid = false
347
- let errorCount = 0
348
-
349
- for (const d of data) {
350
- if (d.hasOwnProperty('label')) {
351
- if (d.hasOwnProperty('options')) {
352
- const optgroup = d as Optgroup
353
- const options = optgroup.options
354
- if (options) {
355
- for (const o of options) {
356
- isValid = validateOption(o)
357
- if (!isValid) { errorCount++ }
358
- }
359
- }
360
- }
361
- } else {
362
- const option = d as Option
363
- isValid = validateOption(option)
364
- if (!isValid) { errorCount++ }
365
- }
366
- }
367
-
368
- return errorCount === 0
369
- }
370
-
371
- export function validateOption(option: Option): boolean {
372
- if (option.text === undefined) {
373
- console.error('Data object option must have at least have a text value. Check object: ' + JSON.stringify(option))
374
- return false
375
- }
376
- return true
377
- }