forked from fluid-project/infusion
/
TextfieldButtons.js
123 lines (114 loc) · 3.93 KB
/
TextfieldButtons.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
/*
Copyright 2016 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
var fluid_2_0_0 = fluid_2_0_0 || {};
(function ($, fluid) {
"use strict";
/********************
* Textfield Buttons *
********************/
fluid.defaults("fluid.textfieldButtons", {
gradeNames: ["fluid.viewComponent"],
distributeOptions: {
target: "{that > fluid.stepper.button}.options",
record: {
model: "{fluid.textfieldButtons}.model",
range: "{textfieldButtons}.options.range",
buttonOptions: "{fluid.textfieldButtons}.options.buttonOptions"
}
},
components: {
textfield: {
type: "fluid.textfieldSlider.textfield",
container: "{textfieldButtons}.dom.textfield",
options: {
model: "{textfieldButtons}.model",
range: "{textfieldButtons}.options.range"
}
},
increaseButton: {
type: "fluid.stepper.button",
container: "{textfieldButtons}.dom.increaseButton",
options: {
incrementCoefficient: 1
}
},
decreaseButton: {
type: "fluid.stepper.button",
container: "{textfieldButtons}.dom.decreaseButton",
options: {
incrementCoefficient: -1
}
}
},
selectors: {
textfield: ".flc-textfieldButtons-field",
increaseButton: ".fl-increase-button",
decreaseButton: ".fl-decrease-button"
},
model: {
value: null
},
modelRelay: {
target: "value",
singleTransform: {
type: "fluid.transforms.limitRange",
input: "{that}.model.value",
min: "{that}.options.range.min",
max: "{that}.options.range.max"
}
},
range: {
min: 0,
max: 100
}
});
fluid.defaults("fluid.stepper.button", {
gradeNames: ["fluid.viewComponent"],
range: {}, // should be used to specify the min, max range e.g. {min: 0, max: 100}
invokers: {
setModel: {
changePath: "value",
value: {
expander: {
funcName: "{that}.calculateValue"
}
}
},
calculateValue: {
funcName: "fluid.stepper.button.buttonCalculateValue",
args: ["{that}.options.incrementCoefficient", "{that}.options.buttonOptions.stepMultiplier", "{that}.model.value", "{that}.options.range.min", "{that}.options.range.max"]
}
},
listeners: {
"onCreate.bindClickEvt": {
"this": "{that}.container",
"method": "click",
"args": "{that}.setModel"
}
},
incrementCoefficient: 1,
buttonOptions: {
stepMultiplier: 10
}
});
fluid.stepper.button.buttonCalculateValue = function (value, stepMultiplier, modelValue, min, max) {
modelValue = Math.round(modelValue * stepMultiplier);
min = Math.round(min * stepMultiplier);
max = Math.round(max * stepMultiplier);
if ((modelValue + value) < min) {
return min/stepMultiplier;
}
else if ((modelValue + value) > max) {
return max/stepMultiplier;
}
else {
return (modelValue + value)/stepMultiplier;
}
};
})(jQuery, fluid_2_0_0);