forked from ElevateDev/meteor-autoform-nouislider
-
Notifications
You must be signed in to change notification settings - Fork 9
/
autoform-nouislider.js
169 lines (145 loc) · 4.72 KB
/
autoform-nouislider.js
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
/* global AutoForm, Template */
import noUiSlider from 'nouislider'
import 'nouislider/distribute/nouislider.css'
import './autoform-nouislider.html'
AutoForm.addInputType('noUiSlider', {
template: 'afNoUiSlider',
valueOut: function () {
const slider = this.find('.nouislider')[ 0 ]
const sliderData = slider.noUiSlider.get()
if (this.attr('data-type') === 'Object') {
const isDecimal = this.closest('.at-nouislider').data('decimal')
const parser = (isDecimal) ? parseFloat : parseInt
const first = parser.call(null, sliderData[ 0 ])
const second = parser.call(null, sliderData[ 1 ])
return {
lower: first > second ? second : first,
upper: first > second ? first : second
}
} else {
return sliderData
}
}
})
const omit = (object, ...keys) => {
const copy = Object.assign({}, object)
keys.forEach(key => {
delete copy[key]
})
return copy
}
const pick = (object, ...keys) => {
const tmp = {}
keys.forEach(key => {
tmp[key] = object[key]
})
return tmp
}
const exists = x => typeof x !== 'undefined' && x !== null
const merge = (...objects) => {
const tmp = {}
objects.forEach(object => {
object && Object.keys(object).forEach(key => {
const value = object[key]
if (exists(value)) {
tmp[key] = value
}
})
})
return tmp
}
Template.afNoUiSlider.helpers({
atts: function () {
const data = Template.currentData() // get data reactively
const atts = Object.assign({}, data.atts)
atts[ 'data-type' ] = data.schemaType.name || 'Object' // fallback if type is a Schema and name becomes undefined
if (atts[ 'class' ]) {
atts[ 'class' ] += ' at-nouislider'
} else {
atts[ 'class' ] = 'at-nouislider'
}
atts.doLabels = (atts.labelLeft || atts.labelRight)
atts[ 'data-decimal' ] = data.decimal
return omit(atts, 'noUiSliderOptions', 'noUiSlider_pipsOptions')
}
})
const calculateOptions = function (data) {
const schemaMinMax = pick(data, 'max', 'min')
const autoformOptions = pick(data.atts || {}, 'max', 'min', 'step', 'start', 'range')
const noUiSliderOptions = (data.atts || {}).noUiSliderOptions
const options = merge(schemaMinMax, autoformOptions, noUiSliderOptions)
// Adjust data initialization based on schema type
if (!options.start) {
if (data.schemaType.name === 'Object') {
if (data.value && data.value.lower) {
options.start = [
data.value.lower,
data.value.upper
]
} else {
options.start = [
typeof data.min === 'number' ? data.min : 0,
typeof data.max === 'number' ? data.max : 100
]
}
options.connect = true
} else {
options.start = data.value || 0
}
}
if (options.range === undefined) {
options.range = {
min: typeof options.min === 'number' ? options.min : 0,
max: typeof options.max === 'number' ? options.max : 100
}
}
delete options.min
delete options.max
// default step to 1 if not otherwise defined
if (options.step === undefined) {
options.step = 1
}
return options
}
Template.afNoUiSlider.rendered = function () {
const template = this
const $s = template.$('.nouislider')
let nonReactiveValue
const setup = function (c) {
const data = Template.currentData() // get data reactively
const options = calculateOptions(data)
const sliderElem = $s[ 0 ]
// if we have a new computation and
// we have a given value (for example by editing a saved document) and
// we have already changed the value
// then we will use the changed value instead of the initial value
// to avoid a potential "jumping" effect when saving the form
if (typeof data.value !== 'undefined' && typeof nonReactiveValue !== 'undefined') {
options.start = nonReactiveValue
}
if (sliderElem.noUiSlider) {
sliderElem.noUiSlider.updateOptions(options, true)
} else {
noUiSlider.create(sliderElem, options)
}
if (c.firstRun) {
sliderElem.noUiSlider.on('slide', function () {
// This is a trick to fool some logic in AutoForm that makes
// sure values have actually changed on whichever element
// emits a change event. Eventually AutoForm will give
// input types the control of indicating exactly when
// their value changes rather than relying on the change event
nonReactiveValue = sliderElem.noUiSlider.get()
$s.parent()[ 0 ].value = JSON.stringify(nonReactiveValue)
$s.parent().change()
$s.data('changed', 'true')
})
}
if (data.atts.noUiSlider_pipsOptions) {
sliderElem.noUiSlider.pips(
data.atts.noUiSlider_pipsOptions
)
}
}
template.autorun(setup)
}