-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle.js
More file actions
144 lines (118 loc) · 3.81 KB
/
toggle.js
File metadata and controls
144 lines (118 loc) · 3.81 KB
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
/**
* 04/12/2017
* ----------
* ToggleElement by @Shad0wflame
* http://twitter.com/shad0wflame
*
*/
var ToggleElement = (function(){
var _TYPES = {
'string': '[object String]',
'function': '[object Function]',
'object': '[object Object]',
'null': '[object Null]',
'array': '[object Array]'
};
/**
* @param {string} id
* @param {function} rightFn (true position)
* @param {function} [leftFn] (false position)
* @constructor
*/
function ToggleElement(id, rightFn, leftFn){
leftFn = leftFn || null;
_handleTypeCoherency.call(this,
[
{obj: id, type: [_TYPES.string]},
{obj: rightFn, type: [_TYPES.function]},
{obj: leftFn, type: [_TYPES.function, _TYPES.null]}
], function(){
this.id = id;
this.domElement = _createDomElement(this);
this.position = false;
this.actionArray = [];
this.actionArray.push(leftFn, rightFn);
}, function(err){
throw new TypeError(err);
});
}
ToggleElement.prototype.toggle = toggle;
return ToggleElement;
/**
*
* ==== FUNCTION DEFINITIONS ====
*
*/
/**
* Toggles the toggle element.
* @public
*/
function toggle(){
this.domElement.querySelector('input[type="checkbox"]').checked = this.position;
this.position = !this.position;
if(this.position){
this.actionArray[1]();
} else if(_isType(this.actionArray[0], _TYPES.function).result){
this.actionArray[0]();
}
}
/**
*
* @param {ToggleElement} toggleElement
* @returns {Element|null}
* @private
*/
function _createDomElement(toggleElement){
var domElement = null;
if(toggleElement !== null && toggleElement.hasOwnProperty('id')) {
domElement = document.createElement('toggle');
domElement['toggleElement'] = toggleElement;
domElement.id = toggleElement.id;
var label = document.createElement('label');
label.className = 'switch';
var input = document.createElement('input');
input.type = 'checkbox';
var span = document.createElement('span');
span.className = 'slider round';
span.onclick = toggleElement.toggle.bind(toggleElement);
label.appendChild(input);
label.appendChild(span);
domElement.appendChild(label);
}
return domElement;
}
/**
* Makes sure every object in the array is the type it should be.
* @param {array} objArray
* @param {function} next
* @param {function} err
* @private
*/
function _handleTypeCoherency(objArray, next, err){
objArray.forEach(function(element){
if(element.hasOwnProperty('obj') &&
element.hasOwnProperty('type') &&
_isType(element.type, _TYPES.array).result){
var type = _isType(element.obj).type;
if(element.type.indexOf(type) === -1){
err.call(this, 'Expected types: '+ element.type.join(', ') +'. Actual type: '+ type);
}
} else {
err.call(this, 'Provided object coulnd\'t be read. Provide one with the proper structure.');
}
});
next.apply(this);
}
/**
* Auxiliary function that evaluates types.
* @param {*} obj
* @param {string} [expectedType]
* @returns {{result: boolean, type: *}}
* @private
*/
function _isType(obj, expectedType){
expectedType = expectedType || undefined;
var type = Object.prototype.toString.call(obj);
return {result: type === expectedType, type: type};
}
}());