-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp.js
More file actions
183 lines (148 loc) · 4.3 KB
/
Copy pathhelp.js
File metadata and controls
183 lines (148 loc) · 4.3 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @author Guille Paz <guille87paz@gmail.com>
*/
(function (window, document, gesturekit) {
'use strict';
var body = document.body,
url = 'http://api.gesturekit.com/v1.1/index.php/sdk/getgestures_help/',
defaults = {
'title': 'Gestures'
};
function render(gesture) {
return [
'<div class="gk-help-gesture">',
'<img src="data:image/png;base64,' + gesture.img + '" height="150">',
'<p class="gk-help-label">' + gesture.method + '</p>',
'<p class="gk-help-label">' + gesture.img_description + '</p>',
'</div>'
].join('');
}
function createNode(tag, classes, parent) {
parent = parent || body;
var node = document.createElement(tag);
node.className = classes;
parent.appendChild(node);
return node;
}
function customizeOptions(options) {
var prop;
for (prop in defaults) {
if (!options.hasOwnProperty(prop)) {
options[prop] = defaults[prop];
}
}
return options;
}
/**
* Creates a new instance of Help.
* @constructor
* @returns {help}
*/
function Help(options) {
this._init(options);
}
/**
* Initializes a new instance of Help.
* @memberof! Help.prototype
* @function
* @private
* @returns {help}
*/
Help.prototype._init = function (options) {
var that = this;
this._options = customizeOptions(options || {});
this.container = createNode('div', 'gk-help-container gk-help-hide');
this.title = createNode('h2', 'gk-help-title', this.container);
this.title.innerHTML = this._options.title;
this.closeBtn = createNode('button', 'gk-help-close', this.container);
this.closeBtn.addEventListener('touchend', function () {
that.hide();
});
if (this._options.uid) {
this.loadGestures();
}
return this;
};
/**
* Show help.
* @memberof! Help.prototype
* @function
* @returns {help}
* @example
* // Show help.
* help.show();
*/
Help.prototype.show = function () {
this.container.style.display = 'block';
gesturekit.disable();
return this;
};
/**
* Hide help.
* @memberof! Help.prototype
* @function
* @returns {help}
* @example
* // Hide help.
* help.hide();
*/
Help.prototype.hide = function () {
this.container.style.display = 'none';
gesturekit.enable();
return this;
};
/**
* Loads gestures.
* @memberof! Help.prototype
* @function
* @returns {help} Returns a new instance of Help.
*/
Help.prototype.loadGestures = function () {
var that = this,
xhr = new window.XMLHttpRequest(),
status,
response;
xhr.open('GET', url + this._options.uid);
// Add events
xhr.onreadystatechange = function () {
if (xhr.readyState === xhr.DONE) {
status = xhr.status;
if ((status >= 200 && status < 300) || status === 304 || status === 0) {
response = JSON.parse(xhr.response || xhr.responseText);
that.renderGestures(response.gestureset.gestures);
}
}
};
xhr.send();
return this;
};
/**
* Render a given collection of gestures.
* @memberof! Help.prototype
* @function
* @returns {help}
*/
Help.prototype.renderGestures = function (gestures) {
var tmp = '';
gestures.forEach(function (gesture) {
tmp += render(gesture);
});
this.container.insertAdjacentHTML('beforeend', tmp);
return this;
};
/**
* Expose Help
*/
// AMD suppport
if (typeof window.define === 'function' && window.define.amd !== undefined) {
window.define('Help', [], function () {
return Help;
});
// CommonJS suppport
} else if (typeof module !== 'undefined' && module.exports !== undefined) {
module.exports = Help;
// Default
} else {
window.Help = Help;
}
}(this, this.document, this.gesturekit));