Skip to content

Commit 667d89b

Browse files
author
Mark
committed
Upgraded package to use latest versions of its dependencies and nuked unnecessary ones. This also marks the time at which IE versions 10 and lower will no longer be supported (this is not to say they won't still work, since we are using babel and browserify, but just a warning). More specifically, this major version included the following changes:
- Changed to latest ES6 syntax - Removed module-js dependency - Removed deprecated element-kit package - Removed old `Form.mapElementsByName()` - Fixed a bug where events on Radios and Checkboxes were not being removed on destroy
1 parent 449f07e commit 667d89b

18 files changed

+896
-756
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"main": "src/index.js",
4242
"devDependencies": {
43-
"build-tools": "^3.0.4",
43+
"build-tools": "^3.1.0",
4444
"sinon": "^1.17.3"
4545
},
4646
"scripts": {
@@ -50,8 +50,6 @@
5050
},
5151
"dependencies": {
5252
"device-manager": "^1.1.1",
53-
"element-kit": "^0.5.0",
54-
"module-js": "^3.0.1",
5553
"observe-js": "^0.4.2",
5654
"promise": "^6.1.0",
5755
"underscore": "^1.8.2"

src/checkbox.js

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
2-
var _ = require('underscore');
3-
var FormElement = require('./form-element');
4-
require('element-kit');
2+
import _ from 'underscore';
3+
import FormElement from './form-element';
54

65
/**
76
* A callback function that fires when the checkbox is checked
@@ -24,7 +23,7 @@ require('element-kit');
2423
* @class Checkbox
2524
* @extends FormElement
2625
*/
27-
var Checkbox = FormElement.extend({
26+
class Checkbox extends FormElement {
2827

2928
/**
3029
* Initialization.
@@ -38,9 +37,9 @@ var Checkbox = FormElement.extend({
3837
* @param {string} [options.disabledClass] - The css class that will be applied to the checkbox (UI-version) when it is disabled
3938
* @param {boolean} [options.value] - The initial checked value to set
4039
*/
41-
initialize: function (options) {
40+
constructor (options) {
4241

43-
this.options = _.extend({
42+
options = _.extend({
4443
el: null,
4544
onChecked: null,
4645
onUnchecked: null,
@@ -51,25 +50,27 @@ var Checkbox = FormElement.extend({
5150
value: null
5251
}, options);
5352

54-
this.el = this.options.el;
53+
super(options);
5554

56-
if (this.el.tagName.toLowerCase() !== 'input') {
55+
56+
if (options.el.tagName.toLowerCase() !== 'input') {
5757
console.warn('checkbox error: element passed in instantiation was not an input element');
5858
}
5959

60-
FormElement.prototype.initialize.call(this, this.options);
60+
this.el = options.el;
61+
this.options = options;
6162

6263
this.setup();
6364

64-
},
65+
}
6566

6667
/**
6768
* Sets up html.
6869
*/
69-
setup: function () {
70+
setup () {
7071
var input = this.getFormElement();
7172

72-
input.kit.classList.add(this.options.inputClass);
73+
input.classList.add(this.options.inputClass);
7374

7475
this._container = this._buildUIElement(this.el);
7576

@@ -82,143 +83,147 @@ var Checkbox = FormElement.extend({
8283

8384
this.isInitDisabled = input.disabled;
8485
if (this.isInitDisabled) {
85-
this._container.kit.classList.add(this.options.disabledClass);
86+
this._container.classList.add(this.options.disabledClass);
8687
}
8788

8889
// setup events
89-
this.getUIElement().kit.addEventListener('click', '_onClick', this);
90-
},
90+
this.addEventListener(this.getUIElement(), 'click', '_onClick', this);
91+
}
9192

9293
/**
9394
* When the checkbox element is clicked.
9495
* @private
9596
*/
96-
_onClick: function () {
97+
_onClick () {
9798
var input = this.getFormElement();
9899
if (!input.disabled) {
99-
if (!this.getUIElement().kit.classList.contains(this.options.checkedClass)) {
100+
if (!this.getUIElement().classList.contains(this.options.checkedClass)) {
100101
this.check();
101102
} else {
102103
this.uncheck();
103104
}
104105
}
105-
},
106+
}
106107

107108
/**
108-
* Builds the checkbox UI-friendly version.
109+
* Wraps the checkbox in a UI-friendly container div.
109110
* @param {HTMLInputElement} inputEl - The input element
110111
* @returns {HTMLElement} Returns the input element wrapped in a new container
111112
* @private
112113
*/
113-
_buildUIElement: function (inputEl) {
114-
return inputEl.kit.appendOuterHtml('<div class="' + this.options.containerClass + '"></div>');
115-
},
114+
_buildUIElement (inputEl) {
115+
let parent = inputEl.parentNode;
116+
var outerEl = document.createElement('div');
117+
outerEl.classList.add(this.options.containerClass);
118+
parent.replaceChild(outerEl, inputEl);
119+
outerEl.appendChild(inputEl);
120+
return outerEl;
121+
}
116122

117123

118124
/**
119125
* Checks the checkbox.
120126
*/
121-
check: function () {
127+
check () {
122128
var input = this.getFormElement(),
123129
container = this.getUIElement();
124130
if (!input.checked) {
125131
input.checked = true;
126132
}
127-
container.kit.classList.add(this.options.checkedClass);
133+
container.classList.add(this.options.checkedClass);
128134
if (this.options.onChecked) {
129135
this.options.onChecked(input.value, input, container);
130136
}
131-
},
137+
}
132138

133139
/**
134140
* Un-checks the checkbox.
135141
*/
136-
uncheck: function () {
142+
uncheck () {
137143
var input = this.getFormElement(),
138144
container = this.getUIElement();
139145
if (input.checked) {
140146
input.checked = false;
141147
}
142-
container.kit.classList.remove(this.options.checkedClass);
148+
container.classList.remove(this.options.checkedClass);
143149
if (this.options.onUnchecked) {
144150
this.options.onUnchecked(input.value, input, container);
145151
}
146-
},
152+
}
147153

148154
/**
149155
* Enables the checkbox.
150156
*/
151-
enable: function () {
157+
enable () {
152158
this.getFormElement().disabled = false;
153-
this.getUIElement().kit.classList.remove(this.options.disabledClass);
154-
},
159+
this.getUIElement().classList.remove(this.options.disabledClass);
160+
}
155161

156162
/**
157163
* Disables the checkbox.
158164
*/
159-
disable: function () {
165+
disable () {
160166
this.getFormElement().disabled = true;
161-
this.getUIElement().kit.classList.add(this.options.disabledClass);
162-
},
167+
this.getUIElement().classList.add(this.options.disabledClass);
168+
}
163169

164170
/**
165171
* Gets the checkbox input element.
166172
* @returns {HTMLInputElement} Returns the checkbox input element
167173
*/
168-
getFormElement: function () {
174+
getFormElement () {
169175
return this.el;
170-
},
176+
}
171177

172178
/**
173179
* Gets the checkbox div element.
174180
* @returns {HTMLElement} Returns the checkbox div element.
175181
*/
176-
getUIElement: function () {
182+
getUIElement () {
177183
return this._container;
178-
},
184+
}
179185

180186
/**
181187
* Gets the unique identifier for checkboxes.
182188
* @returns {string}
183189
*/
184-
getElementKey: function () {
190+
getElementKey () {
185191
return 'checkbox';
186-
},
192+
}
187193

188194
/**
189195
* Unselects the checkbox if its selected.
190196
*/
191-
clear: function () {
197+
clear () {
192198
this.uncheck();
193-
},
199+
}
194200

195201
/**
196202
* Returns whether the checkbox is checked or not
197203
* @returns {boolean} Returns a truthy value if checkbox is checked, falsy if not
198204
*/
199-
getValue: function () {
205+
getValue () {
200206
return this.getFormElement().checked;
201-
},
207+
}
202208

203209
/**
204210
* Checks the checkbox if a truthy value is passed.
205211
* @param {string|boolean} value
206212
*/
207-
setValue: function (value) {
213+
setValue (value) {
208214
// check it if the value is truthy
209215
value = value ? true : false;
210216
this.getFormElement().checked = value;
211-
},
217+
}
212218

213219
/**
214220
* Destruction of this class.
215221
*/
216-
destroy: function () {
222+
destroy () {
217223
var container = this.getUIElement(),
218224
input = this.getFormElement();
219225

220-
// remove event listener
221-
container.kit.removeEventListener('click', '_onClick', this);
226+
this.removeEventListener(container, 'click', '_onClick', this);
222227

223228
// remove stray html
224229
container.parentNode.replaceChild(input, container);
@@ -229,9 +234,9 @@ var Checkbox = FormElement.extend({
229234
if (this.isInitDisabled) {
230235
input.disabled = true;
231236
}
232-
FormElement.prototype.destroy.call(this);
237+
super.destroy();
233238
}
234239

235-
});
240+
}
236241

237-
module.exports = Checkbox;
242+
module.exports = Checkbox;

src/checkboxes.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
2-
var _ = require('underscore');
3-
var FormElementGroup = require('./form-element-group');
4-
require('element-kit');
2+
import _ from 'underscore';
3+
import FormElementGroup from './form-element-group';
54
/**
65
* A callback function that fires when one of the checkbox elements are selected
76
* @callback Checkboxes~onChange
@@ -31,7 +30,7 @@ require('element-kit');
3130
* @class Checkboxes
3231
* @extends FormElement
3332
*/
34-
var Checkboxes = FormElementGroup.extend({
33+
class Checkboxes extends FormElementGroup {
3534

3635
/**
3736
* Initialization.
@@ -46,9 +45,9 @@ var Checkboxes = FormElementGroup.extend({
4645
* @param {string} [options.disabledClass] - The css class that will be applied to a checkbox item (UI-version) when it is disabled
4746
* @param {string|Array} [options.value] - The string matching the name attribute of the checkbox button to have selected initially (or an array of such strings)
4847
*/
49-
initialize: function (options) {
48+
constructor (options) {
5049

51-
this.options = _.extend({
50+
options = _.extend({
5251
inputs: [],
5352
onChange: null,
5453
containerClass: 'ui-checkbox',
@@ -58,48 +57,49 @@ var Checkboxes = FormElementGroup.extend({
5857
value: null
5958
}, options);
6059

61-
FormElementGroup.prototype.initialize.call(this, this.options);
62-
},
60+
super(options);
61+
this.options = options;
62+
}
6363

6464
/**
6565
* When a checkbox is clicked that is a checkbox input element.
6666
* @param {HTMLInputElement} formElement - The checkbox element
6767
* @param {HTMLElement} UIElement - The ui element
6868
* @private
6969
*/
70-
_processClick: function (formElement, UIElement) {
71-
if (!UIElement.kit.classList.contains(this.options.selectedClass)) {
70+
_processClick (formElement, UIElement) {
71+
if (!UIElement.classList.contains(this.options.selectedClass)) {
7272
formElement.checked = true;
73-
UIElement.kit.classList.add(this.options.selectedClass);
73+
UIElement.classList.add(this.options.selectedClass);
7474
} else {
7575
formElement.checked = false;
76-
UIElement.kit.classList.remove(this.options.selectedClass);
76+
UIElement.classList.remove(this.options.selectedClass);
7777
}
7878
this.triggerChange(formElement, UIElement);
79-
},
79+
}
8080

8181
/**
8282
* Selects the checkbox item.
8383
* @param {Number} index - The index of the checkbox item
8484
*/
85-
select: function (index) {
85+
select (index) {
8686
var input = this.getFormElement(index),
8787
checkbox = this.getUIElement(index);
8888
if (!input.checked) {
8989
input.checked = true;
90-
checkbox.kit.classList.add(this.options.selectedClass);
90+
checkbox.classList.add(this.options.selectedClass);
9191
this.triggerChange(input, checkbox);
9292
}
93-
},
93+
}
9494

9595
/**
9696
* Gets the unique identifier for checkboxes.
9797
* @returns {string}
9898
*/
99-
getElementKey: function () {
99+
getElementKey () {
100100
return 'checkboxes';
101101
}
102102

103-
});
103+
}
104104

105-
module.exports = Checkboxes;
105+
module.exports = Checkboxes;

0 commit comments

Comments
 (0)