-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathclasses_form.js.html
301 lines (258 loc) · 11 KB
/
classes_form.js.html
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: classes/form.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: classes/form.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @name Form
* @class
*/
var formCounter = 0;
/**
* the form class is responsible for rendering html inputs to the dom and provides an interface for retrieving this information
* @param {number} x coorddinate
* @param {number} y coordinate
* @param {Function} list of fields
* @param {Object} @optional options object
*/
var Form = function(x, y, fields, options) {
this.formName = "default-form-" + formCounter + "-name-";
formCounter++;
// if an object is passed in for x, then we know it is actually the fields and shift the arguments over
if (typeof(x) === "object") {
options = y;
fields = x;
x = 0;
y = 0;
}
this.fields = fields;
this.header = options ? options.header || null : null;
this.message = options ? options.message || null : null;
this.cssClass = options ? options.cssClass || null : null;
// inherited constructor
Widget.apply(this, [{x: x, y: y, cssClass: this.cssClass}]);
/**
* [getValue gets the value of an internal form field]
* @param {value}
* @return {string}
* @public
*/
this.getValue = function(value) {
return this.el.find("#" + value).val();
};
/**
* [values returns the values of all fields within a form]
* @return {Object}
* @public
*/
this.values = function() {
obj = {};
this.fields.forEach(function(field) {
obj[field.text] = this.getValue(field.text);
}, this);
return obj;
};
/**
* [render renders the form to the screen]
* @public
*/
this.render = function() {
this.el.html(template.call(this));
if(this.message){
this.el.prepend("<p>"+this.message+"</p>");
}
if(this.header){
this.el.prepend("<h2>"+this.header+"</h2>");
}
this.container.append(this.el);
attachCallbacks.call(this);
};
// private
/**
* [getId gets the idea of a form field by its number]
* @param {number|string} index
* @return {string}
* @private
*/
function getId(index) {
if (typeof(index) === "number") {
return this.formName + index.toString();
} else {
return index;
}
}
/**
* [template returns the proper template for the desired form.]
* @return {string}
* @private
*/
function template() {
return inputFields.call(this);
}
/**
* Displays raw text on the form field
* @param {string} textToRender The string of text to render
* @return {string} HTML code to be injected to display text
* @private
*/
function basicText(textToRender) {
return "<p class='basic-text'>" + textToRender + "</p>";
}
/**
* [textField diplays a text field]
* @param {string} name
* @param {number} index
* @return {string}
* @private
*/
function textField(name, index) {
return "<input class='input-default' placeholder='" + name + "' id='" + getId.call(this, index) + "' autofocus ></input>";
}
/**
* [buttonField displays a button field]
* @param {string} name
* @param {number} index
* @return {string}
* @private
*/
function buttonField(name, index) {
return "<button class='button-default' placeholder='" + name + "' id='" + getId.call(this, index) + "'>" + name + "</button>";
}
/**
* [selectField displays a select field]
* @param {string} name
* @param {string} options
* @param {number} index
* @return {string}
*/
function selectField(name, options, index) {
var html = "<select class='select-default' id='" + getId.call(this, index) + "'>";
options.forEach(function(option) {
html += "<option class='option-default' id='" + option + "'>" + option + "</option>";
});
html += "</select>"
return html;
}
/**
* [radioField displays a radio field]
* @param {string} name
* @param {string} options
* @param {number} index
* @return {string}
* @private
*/
function radioField(name, options, index) {
var html = name ? "<p>choose the " + name + "</p>" : "";
firstOption = options.shift();
html += "<input checked type='radio' class='radio-default' id='" + getId.call(this, index) + "' value='" + firstOption + "'>" + firstOption + "</br> "
options.forEach(function(option) {
html += "<input type='radio' id='" + name + "' value='" + option + "'>" + option + "</br> "
});
return html;
}
/**
* [numberField displays a numberField]
* @param {string} name
* @param {number} min
* @param {number} max
* @param {number} index
* @return {string}
* @private
*/
function numberField(name, min, max, index) {
return "<input class='input-default' placeholder='" + min + "-" + max + "' type='number' id='" + getId.call(this, index) + "' min='" + min + "' max='" + max + "' autofocus></input>"
}
/**
* [rangeField displays a range field]
* @param {string} name
* @param {number} min
* @param {number} max
* @param {number} index
* @return {string}
* @private
*/
function rangeField(name, min, max, defaultValue, index) {
var html = name ? "</br>" + name + "</br>" : "";
return html + "<input class='range-default' type='range' id='" + getId.call(this, index) + "' min='" + min + "' max='" + max + "' value='"+defaultValue+"'></input>";
}
/**
* [inputFields handles the display of all types of fields]
* Returns a string of the fields' HTML
* @return {string}
* @private
*/
function inputFields() {
var html = "";
var index = 0;
this.fields.forEach(function(field) {
if (field.type === 'text') {
html += textField.call(this, field.text, (field.id) ? field.id : index);
} else if (field.type === 'select') {
html += selectField.call(this, field.text, field.options, (field.id) ? field.id : index);
} else if (field.type === 'radio') {
html += radioField.call(this, field.text, field.options, (field.id) ? field.id : index);
} else if (field.type === 'number') {
html += numberField.call(this, field.text, field.min, field.max, (field.id) ? field.id : index);
} else if (field.type === 'range') {
html += rangeField.call(this, field.text, field.min, field.max, field.value, (field.id) ? field.id : index);
} else if (field.type === 'button') {
html += buttonField.call(this, field.text, (field.id) ? field.id : index);
} else if (field.type === 'basic-text') {
html += basicText.call(this, field.text);
}
index++;
}.bind(this));
return html;
}
/**
* [attachCallbacks attaches the proper callback functions to each form.]
* @private
*/
function attachCallbacks() {
var index = 0;
this.fields.forEach(function(field) {
if (field.type === 'button') {
$("#" + getId.call(this, (field.id) ? field.id : index)).click(field.callback);
}
if(field.key){
// $("body").unbind();
// console.log(Mousetrap);
// $("body").keypress(function(event){
// if(event.which == field.key.charCodeAt()){
// console.log(field);
// field.callback();
// }
// });
}
index++;
}.bind(this));
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="CollisionSystem.html">CollisionSystem</a></li><li><a href="ComplexObject.html">ComplexObject</a></li><li><a href="DisableFogOfWar.html">DisableFogOfWar</a></li><li><a href="Dragon.html">Dragon</a></li><li><a href="EndGame.html">EndGame</a></li><li><a href="Enemy.html">Enemy</a></li><li><a href="ExampleComplexObject.html">ExampleComplexObject</a></li><li><a href="ExampleSimpleObject.html">ExampleSimpleObject</a></li><li><a href="global.html#Form">Form</a></li><li><a href="MapLoader.html">MapLoader</a></li><li><a href="Person.html">Person</a></li><li><a href="Player.html">Player</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="Robot.html">Robot</a></li><li><a href="SimpleObject.html">SimpleObject</a></li><li><a href="global.html#Widget">Widget</a></li></ul><h3>Global</h3><ul><li><a href="global.html#advanceTurn">advanceTurn</a></li><li><a href="global.html#calculateDamage">calculateDamage</a></li><li><a href="global.html#Constants">Constants</a></li><li><a href="global.html#DEFAULT_PERSON_ATTACK">DEFAULT_PERSON_ATTACK</a></li><li><a href="global.html#DEFAULT_PERSON_HEALTH">DEFAULT_PERSON_HEALTH</a></li><li><a href="global.html#DEFAULT_PERSON_SIGHT_DISTANCE">DEFAULT_PERSON_SIGHT_DISTANCE</a></li><li><a href="global.html#destroy">destroy</a></li><li><a href="global.html#fixViewport">fixViewport</a></li><li><a href="global.html#getPositionCSS">getPositionCSS</a></li><li><a href="global.html#getValue">getValue</a></li><li><a href="global.html#handleComplete">handleComplete</a></li><li><a href="global.html#handleTick">handleTick</a></li><li><a href="global.html#hide">hide</a></li><li><a href="global.html#init">init</a></li><li><a href="global.html#initVars">initVars</a></li><li><a href="global.html#isSelectionInSelectableBounds">isSelectionInSelectableBounds</a></li><li><a href="global.html#LOG_FPS">LOG_FPS</a></li><li><a href="global.html#Main">Main</a></li><li><a href="global.html#MAP_MOVE_SPEED">MAP_MOVE_SPEED</a></li><li><a href="global.html#MapLink">MapLink</a></li><li><a href="global.html#MAX_ENEMY_DISTANCE">MAX_ENEMY_DISTANCE</a></li><li><a href="global.html#MAX_HIGH_SCORES">MAX_HIGH_SCORES</a></li><li><a href="global.html#MAX_TURN_COUNTER">MAX_TURN_COUNTER</a></li><li><a href="global.html#MIN_ENEMY_DISTANCE">MIN_ENEMY_DISTANCE</a></li><li><a href="global.html#namePlayers">namePlayers</a></li><li><a href="global.html#NUM_ENEMIES">NUM_ENEMIES</a></li><li><a href="global.html#NUM_PLAYERS">NUM_PLAYERS</a></li><li><a href="global.html#removeSelectableArea">removeSelectableArea</a></li><li><a href="global.html#render">render</a></li><li><a href="global.html#selectMap">selectMap</a></li><li><a href="global.html#selectPlayers">selectPlayers</a></li><li><a href="global.html#show">show</a></li><li><a href="global.html#showSelectableArea">showSelectableArea</a></li><li><a href="global.html#startMenu">startMenu</a></li><li><a href="global.html#StartPoint">StartPoint</a></li><li><a href="global.html#tickActions">tickActions</a></li><li><a href="global.html#TILESET_FILE_TYPE">TILESET_FILE_TYPE</a></li><li><a href="global.html#TREASURE_VALUE">TREASURE_VALUE</a></li><li><a href="global.html#updateFogOfWar">updateFogOfWar</a></li><li><a href="global.html#values">values</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta1</a> on Thu Mar 12 2015 10:48:29 GMT-0700 (PDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>