semantic-ui 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/semantic-ui.rb +8 -0
- data/lib/semantic-ui/version.rb +5 -0
- data/vendor/assets/fonts/icons.eot +0 -0
- data/vendor/assets/fonts/icons.svg +450 -0
- data/vendor/assets/fonts/icons.ttf +0 -0
- data/vendor/assets/fonts/icons.woff +0 -0
- data/vendor/assets/images/loader-large-inverted.gif +0 -0
- data/vendor/assets/images/loader-large.gif +0 -0
- data/vendor/assets/images/loader-medium-inverted.gif +0 -0
- data/vendor/assets/images/loader-medium.gif +0 -0
- data/vendor/assets/images/loader-mini-inverted.gif +0 -0
- data/vendor/assets/images/loader-mini.gif +0 -0
- data/vendor/assets/images/loader-small-inverted.gif +0 -0
- data/vendor/assets/images/loader-small.gif +0 -0
- data/vendor/assets/javascripts/modules/accordion.js +411 -0
- data/vendor/assets/javascripts/modules/behavior/api.js +666 -0
- data/vendor/assets/javascripts/modules/behavior/colorize.js +271 -0
- data/vendor/assets/javascripts/modules/behavior/form.js +703 -0
- data/vendor/assets/javascripts/modules/behavior/state.js +752 -0
- data/vendor/assets/javascripts/modules/chatroom.js +766 -0
- data/vendor/assets/javascripts/modules/checkbox.js +348 -0
- data/vendor/assets/javascripts/modules/dimmer.js +524 -0
- data/vendor/assets/javascripts/modules/dropdown.js +707 -0
- data/vendor/assets/javascripts/modules/modal.js +478 -0
- data/vendor/assets/javascripts/modules/nag.js +542 -0
- data/vendor/assets/javascripts/modules/popup.js +721 -0
- data/vendor/assets/javascripts/modules/rating.js +358 -0
- data/vendor/assets/javascripts/modules/search.js +769 -0
- data/vendor/assets/javascripts/modules/shape.js +776 -0
- data/vendor/assets/javascripts/modules/sidebar.js +489 -0
- data/vendor/assets/javascripts/modules/tab.js +674 -0
- data/vendor/assets/javascripts/modules/transition.js +650 -0
- data/vendor/assets/javascripts/modules/video.js +459 -0
- data/vendor/assets/javascripts/semantic-ui.js +1 -0
- data/vendor/assets/stylesheets/collections/breadcrumb.less +79 -0
- data/vendor/assets/stylesheets/collections/form.less +536 -0
- data/vendor/assets/stylesheets/collections/grid.less +655 -0
- data/vendor/assets/stylesheets/collections/menu.less +1765 -0
- data/vendor/assets/stylesheets/collections/message.less +337 -0
- data/vendor/assets/stylesheets/collections/table.less +532 -0
- data/vendor/assets/stylesheets/elements/button.less +1190 -0
- data/vendor/assets/stylesheets/elements/divider.less +200 -0
- data/vendor/assets/stylesheets/elements/header.less +325 -0
- data/vendor/assets/stylesheets/elements/icon.less +484 -0
- data/vendor/assets/stylesheets/elements/image.less +170 -0
- data/vendor/assets/stylesheets/elements/input.less +300 -0
- data/vendor/assets/stylesheets/elements/label.less +824 -0
- data/vendor/assets/stylesheets/elements/loader.less +178 -0
- data/vendor/assets/stylesheets/elements/progress.less +353 -0
- data/vendor/assets/stylesheets/elements/segment.less +465 -0
- data/vendor/assets/stylesheets/elements/step.less +294 -0
- data/vendor/assets/stylesheets/modules/accordion.less +135 -0
- data/vendor/assets/stylesheets/modules/chatroom.less +271 -0
- data/vendor/assets/stylesheets/modules/checkbox.less +380 -0
- data/vendor/assets/stylesheets/modules/dimmer.less +230 -0
- data/vendor/assets/stylesheets/modules/dropdown.less +506 -0
- data/vendor/assets/stylesheets/modules/modal.less +160 -0
- data/vendor/assets/stylesheets/modules/nag.less +175 -0
- data/vendor/assets/stylesheets/modules/popup.less +238 -0
- data/vendor/assets/stylesheets/modules/rating.less +151 -0
- data/vendor/assets/stylesheets/modules/reveal.less +367 -0
- data/vendor/assets/stylesheets/modules/search.less +275 -0
- data/vendor/assets/stylesheets/modules/shape.less +115 -0
- data/vendor/assets/stylesheets/modules/sidebar.less +147 -0
- data/vendor/assets/stylesheets/modules/tab.less +59 -0
- data/vendor/assets/stylesheets/modules/transition.less +839 -0
- data/vendor/assets/stylesheets/modules/video.less +99 -0
- data/vendor/assets/stylesheets/semantic-ui.less +1 -0
- data/vendor/assets/stylesheets/views/comment.less +221 -0
- data/vendor/assets/stylesheets/views/feed.less +151 -0
- data/vendor/assets/stylesheets/views/item.less +641 -0
- data/vendor/assets/stylesheets/views/list.less +508 -0
- data/vendor/assets/stylesheets/views/statistic.css +27 -0
- metadata +187 -0
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,411 @@
|
|
1
|
+
/* ******************************
|
2
|
+
Accordion
|
3
|
+
Author: Jack Lukic
|
4
|
+
Notes: First Commit July 19, 2012
|
5
|
+
|
6
|
+
Simple accordion design
|
7
|
+
****************************** */
|
8
|
+
|
9
|
+
;(function ($, window, document, undefined) {
|
10
|
+
|
11
|
+
$.fn.accordion = function(parameters) {
|
12
|
+
var
|
13
|
+
$allModules = $(this),
|
14
|
+
|
15
|
+
settings = ( $.isPlainObject(parameters) )
|
16
|
+
? $.extend(true, {}, $.fn.accordion.settings, parameters)
|
17
|
+
: $.fn.accordion.settings,
|
18
|
+
|
19
|
+
className = settings.className,
|
20
|
+
namespace = settings.namespace,
|
21
|
+
selector = settings.selector,
|
22
|
+
error = settings.error,
|
23
|
+
|
24
|
+
eventNamespace = '.' + namespace,
|
25
|
+
moduleNamespace = 'module-' + namespace,
|
26
|
+
moduleSelector = $allModules.selector || '',
|
27
|
+
|
28
|
+
time = new Date().getTime(),
|
29
|
+
performance = [],
|
30
|
+
|
31
|
+
query = arguments[0],
|
32
|
+
methodInvoked = (typeof query == 'string'),
|
33
|
+
queryArguments = [].slice.call(arguments, 1),
|
34
|
+
invokedResponse
|
35
|
+
;
|
36
|
+
$allModules
|
37
|
+
.each(function() {
|
38
|
+
var
|
39
|
+
$module = $(this),
|
40
|
+
$title = $module.find(selector.title),
|
41
|
+
$icon = $module.find(selector.icon),
|
42
|
+
$content = $module.find(selector.content),
|
43
|
+
|
44
|
+
element = this,
|
45
|
+
instance = $module.data(moduleNamespace),
|
46
|
+
module
|
47
|
+
;
|
48
|
+
|
49
|
+
module = {
|
50
|
+
|
51
|
+
initialize: function() {
|
52
|
+
module.debug('Initializing accordion with bound events', $module);
|
53
|
+
// initializing
|
54
|
+
$title
|
55
|
+
.on('click' + eventNamespace, module.event.click)
|
56
|
+
;
|
57
|
+
module.instantiate();
|
58
|
+
},
|
59
|
+
|
60
|
+
instantiate: function() {
|
61
|
+
$module
|
62
|
+
.data(moduleNamespace, module)
|
63
|
+
;
|
64
|
+
},
|
65
|
+
|
66
|
+
destroy: function() {
|
67
|
+
module.debug('Destroying previous accordion for', $module);
|
68
|
+
$module
|
69
|
+
.removeData(moduleNamespace)
|
70
|
+
;
|
71
|
+
$title
|
72
|
+
.off(eventNamespace)
|
73
|
+
;
|
74
|
+
},
|
75
|
+
|
76
|
+
event: {
|
77
|
+
click: function() {
|
78
|
+
module.verbose('Title clicked', this);
|
79
|
+
var
|
80
|
+
$activeTitle = $(this),
|
81
|
+
index = $title.index($activeTitle)
|
82
|
+
;
|
83
|
+
module.toggle(index);
|
84
|
+
},
|
85
|
+
resetStyle: function() {
|
86
|
+
module.verbose('Resetting styles on element', this);
|
87
|
+
$(this)
|
88
|
+
.removeAttr('style')
|
89
|
+
.children()
|
90
|
+
.removeAttr('style')
|
91
|
+
;
|
92
|
+
}
|
93
|
+
},
|
94
|
+
|
95
|
+
toggle: function(index) {
|
96
|
+
module.debug('Toggling content content at index', index);
|
97
|
+
var
|
98
|
+
$activeTitle = $title.eq(index),
|
99
|
+
$activeContent = $activeTitle.next($content),
|
100
|
+
contentIsOpen = $activeContent.is(':visible')
|
101
|
+
;
|
102
|
+
if(contentIsOpen) {
|
103
|
+
if(settings.collapsible) {
|
104
|
+
module.close(index);
|
105
|
+
}
|
106
|
+
else {
|
107
|
+
module.debug('Cannot close accordion content collapsing is disabled');
|
108
|
+
}
|
109
|
+
}
|
110
|
+
else {
|
111
|
+
module.open(index);
|
112
|
+
}
|
113
|
+
},
|
114
|
+
|
115
|
+
open: function(index) {
|
116
|
+
var
|
117
|
+
$activeTitle = $title.eq(index),
|
118
|
+
$activeContent = $activeTitle.next($content),
|
119
|
+
$previousTitle = $title.filter('.' + className.active),
|
120
|
+
$previousContent = $previousTitle.next($title),
|
121
|
+
contentIsOpen = ($previousTitle.size() > 0)
|
122
|
+
;
|
123
|
+
if( !$activeContent.is(':animated') ) {
|
124
|
+
module.debug('Opening accordion content', $activeTitle);
|
125
|
+
if(settings.exclusive && contentIsOpen) {
|
126
|
+
$previousTitle
|
127
|
+
.removeClass(className.active)
|
128
|
+
;
|
129
|
+
$previousContent
|
130
|
+
.stop()
|
131
|
+
.children()
|
132
|
+
.animate({
|
133
|
+
opacity: 0
|
134
|
+
}, settings.speed, module.event.resetStyle)
|
135
|
+
.end()
|
136
|
+
.slideUp(settings.speed , settings.easing, function() {
|
137
|
+
$previousContent
|
138
|
+
.removeClass(className.active)
|
139
|
+
.removeAttr('style')
|
140
|
+
.children()
|
141
|
+
.removeAttr('style')
|
142
|
+
;
|
143
|
+
})
|
144
|
+
;
|
145
|
+
}
|
146
|
+
$activeTitle
|
147
|
+
.addClass(className.active)
|
148
|
+
;
|
149
|
+
$activeContent
|
150
|
+
.stop()
|
151
|
+
.children()
|
152
|
+
.removeAttr('style')
|
153
|
+
.end()
|
154
|
+
.slideDown(settings.speed, settings.easing, function() {
|
155
|
+
$activeContent
|
156
|
+
.addClass(className.active)
|
157
|
+
.removeAttr('style')
|
158
|
+
;
|
159
|
+
$.proxy(settings.onOpen, $activeContent)();
|
160
|
+
$.proxy(settings.onChange, $activeContent)();
|
161
|
+
})
|
162
|
+
;
|
163
|
+
}
|
164
|
+
},
|
165
|
+
|
166
|
+
close: function(index) {
|
167
|
+
var
|
168
|
+
$activeTitle = $title.eq(index),
|
169
|
+
$activeContent = $activeTitle.next($content)
|
170
|
+
;
|
171
|
+
module.debug('Closing accordion content', $activeTitle);
|
172
|
+
$activeTitle
|
173
|
+
.removeClass(className.active)
|
174
|
+
;
|
175
|
+
$activeContent
|
176
|
+
.removeClass(className.active)
|
177
|
+
.show()
|
178
|
+
.stop()
|
179
|
+
.children()
|
180
|
+
.animate({
|
181
|
+
opacity: 0
|
182
|
+
}, settings.speed, module.event.resetStyle)
|
183
|
+
.end()
|
184
|
+
.slideUp(settings.speed, settings.easing, function(){
|
185
|
+
$activeContent
|
186
|
+
.removeAttr('style')
|
187
|
+
;
|
188
|
+
$.proxy(settings.onClose, $activeContent)();
|
189
|
+
$.proxy(settings.onChange, $activeContent)();
|
190
|
+
})
|
191
|
+
;
|
192
|
+
},
|
193
|
+
|
194
|
+
setting: function(name, value) {
|
195
|
+
module.debug('Changing setting', name, value);
|
196
|
+
if(value !== undefined) {
|
197
|
+
if( $.isPlainObject(name) ) {
|
198
|
+
$.extend(true, settings, name);
|
199
|
+
}
|
200
|
+
else {
|
201
|
+
settings[name] = value;
|
202
|
+
}
|
203
|
+
}
|
204
|
+
else {
|
205
|
+
return settings[name];
|
206
|
+
}
|
207
|
+
},
|
208
|
+
internal: function(name, value) {
|
209
|
+
module.debug('Changing internal', name, value);
|
210
|
+
if(value !== undefined) {
|
211
|
+
if( $.isPlainObject(name) ) {
|
212
|
+
$.extend(true, module, name);
|
213
|
+
}
|
214
|
+
else {
|
215
|
+
module[name] = value;
|
216
|
+
}
|
217
|
+
}
|
218
|
+
else {
|
219
|
+
return module[name];
|
220
|
+
}
|
221
|
+
},
|
222
|
+
debug: function() {
|
223
|
+
if(settings.debug) {
|
224
|
+
if(settings.performance) {
|
225
|
+
module.performance.log(arguments);
|
226
|
+
}
|
227
|
+
else {
|
228
|
+
module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
|
229
|
+
module.debug.apply(console, arguments);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
},
|
233
|
+
verbose: function() {
|
234
|
+
if(settings.verbose && settings.debug) {
|
235
|
+
if(settings.performance) {
|
236
|
+
module.performance.log(arguments);
|
237
|
+
}
|
238
|
+
else {
|
239
|
+
module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
|
240
|
+
module.verbose.apply(console, arguments);
|
241
|
+
}
|
242
|
+
}
|
243
|
+
},
|
244
|
+
error: function() {
|
245
|
+
module.error = Function.prototype.bind.call(console.error, console, settings.moduleName + ':');
|
246
|
+
module.error.apply(console, arguments);
|
247
|
+
},
|
248
|
+
performance: {
|
249
|
+
log: function(message) {
|
250
|
+
var
|
251
|
+
currentTime,
|
252
|
+
executionTime,
|
253
|
+
previousTime
|
254
|
+
;
|
255
|
+
if(settings.performance) {
|
256
|
+
currentTime = new Date().getTime();
|
257
|
+
previousTime = time || currentTime;
|
258
|
+
executionTime = currentTime - previousTime;
|
259
|
+
time = currentTime;
|
260
|
+
performance.push({
|
261
|
+
'Element' : element,
|
262
|
+
'Name' : message[0],
|
263
|
+
'Arguments' : [].slice.call(message, 1) || '',
|
264
|
+
'Execution Time' : executionTime
|
265
|
+
});
|
266
|
+
}
|
267
|
+
clearTimeout(module.performance.timer);
|
268
|
+
module.performance.timer = setTimeout(module.performance.display, 100);
|
269
|
+
},
|
270
|
+
display: function() {
|
271
|
+
var
|
272
|
+
title = settings.name + ':',
|
273
|
+
totalTime = 0
|
274
|
+
;
|
275
|
+
time = false;
|
276
|
+
clearTimeout(module.performance.timer);
|
277
|
+
$.each(performance, function(index, data) {
|
278
|
+
totalTime += data['Execution Time'];
|
279
|
+
});
|
280
|
+
title += ' ' + totalTime + 'ms';
|
281
|
+
if(moduleSelector) {
|
282
|
+
title += ' \'' + moduleSelector + '\'';
|
283
|
+
}
|
284
|
+
if($allModules.size() > 1) {
|
285
|
+
title += ' ' + '(' + $allModules.size() + ')';
|
286
|
+
}
|
287
|
+
if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
|
288
|
+
console.groupCollapsed(title);
|
289
|
+
if(console.table) {
|
290
|
+
console.table(performance);
|
291
|
+
}
|
292
|
+
else {
|
293
|
+
$.each(performance, function(index, data) {
|
294
|
+
console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
|
295
|
+
});
|
296
|
+
}
|
297
|
+
console.groupEnd();
|
298
|
+
}
|
299
|
+
performance = [];
|
300
|
+
}
|
301
|
+
},
|
302
|
+
invoke: function(query, passedArguments, context) {
|
303
|
+
var
|
304
|
+
maxDepth,
|
305
|
+
found,
|
306
|
+
response
|
307
|
+
;
|
308
|
+
passedArguments = passedArguments || queryArguments;
|
309
|
+
context = element || context;
|
310
|
+
if(typeof query == 'string' && instance !== undefined) {
|
311
|
+
query = query.split(/[\. ]/);
|
312
|
+
maxDepth = query.length - 1;
|
313
|
+
$.each(query, function(depth, value) {
|
314
|
+
var camelCaseValue = (depth != maxDepth)
|
315
|
+
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
|
316
|
+
: query
|
317
|
+
;
|
318
|
+
if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
|
319
|
+
instance = instance[value];
|
320
|
+
}
|
321
|
+
else if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) {
|
322
|
+
instance = instance[camelCaseValue];
|
323
|
+
}
|
324
|
+
else if( instance[value] !== undefined ) {
|
325
|
+
found = instance[value];
|
326
|
+
return false;
|
327
|
+
}
|
328
|
+
else if( instance[camelCaseValue] !== undefined ) {
|
329
|
+
found = instance[camelCaseValue];
|
330
|
+
return false;
|
331
|
+
}
|
332
|
+
else {
|
333
|
+
module.error(error.method);
|
334
|
+
return false;
|
335
|
+
}
|
336
|
+
});
|
337
|
+
}
|
338
|
+
if ( $.isFunction( found ) ) {
|
339
|
+
response = found.apply(context, passedArguments);
|
340
|
+
}
|
341
|
+
else if(found !== undefined) {
|
342
|
+
response = found;
|
343
|
+
}
|
344
|
+
if($.isArray(invokedResponse)) {
|
345
|
+
invokedResponse.push(response);
|
346
|
+
}
|
347
|
+
else if(typeof invokedResponse == 'string') {
|
348
|
+
invokedResponse = [invokedResponse, response];
|
349
|
+
}
|
350
|
+
else if(response !== undefined) {
|
351
|
+
invokedResponse = response;
|
352
|
+
}
|
353
|
+
return found;
|
354
|
+
}
|
355
|
+
};
|
356
|
+
if(methodInvoked) {
|
357
|
+
if(instance === undefined) {
|
358
|
+
module.initialize();
|
359
|
+
}
|
360
|
+
module.invoke(query);
|
361
|
+
}
|
362
|
+
else {
|
363
|
+
if(instance !== undefined) {
|
364
|
+
module.destroy();
|
365
|
+
}
|
366
|
+
module.initialize();
|
367
|
+
}
|
368
|
+
})
|
369
|
+
;
|
370
|
+
return (invokedResponse !== undefined)
|
371
|
+
? invokedResponse
|
372
|
+
: this
|
373
|
+
;
|
374
|
+
};
|
375
|
+
|
376
|
+
$.fn.accordion.settings = {
|
377
|
+
name : 'Accordion',
|
378
|
+
namespace : 'accordion',
|
379
|
+
|
380
|
+
debug : true,
|
381
|
+
verbose : true,
|
382
|
+
performance : true,
|
383
|
+
|
384
|
+
exclusive : true,
|
385
|
+
collapsible : true,
|
386
|
+
|
387
|
+
onOpen : function(){},
|
388
|
+
onClose : function(){},
|
389
|
+
onChange : function(){},
|
390
|
+
|
391
|
+
error: {
|
392
|
+
method : 'The method you called is not defined'
|
393
|
+
},
|
394
|
+
|
395
|
+
className : {
|
396
|
+
active : 'active',
|
397
|
+
hover : 'hover'
|
398
|
+
},
|
399
|
+
|
400
|
+
selector : {
|
401
|
+
title : '.title',
|
402
|
+
icon : '.icon',
|
403
|
+
content : '.content'
|
404
|
+
},
|
405
|
+
|
406
|
+
speed : 500,
|
407
|
+
easing : 'easeInOutQuint'
|
408
|
+
|
409
|
+
};
|
410
|
+
|
411
|
+
})( jQuery, window , document );
|
@@ -0,0 +1,666 @@
|
|
1
|
+
/* ******************************
|
2
|
+
API
|
3
|
+
Author: Jack Lukic
|
4
|
+
Notes: First Commit May 08, 2012
|
5
|
+
|
6
|
+
These are modules which bind API functionality to the DOM
|
7
|
+
|
8
|
+
Requires: nada
|
9
|
+
|
10
|
+
Initialization:
|
11
|
+
$('.button')
|
12
|
+
.apiButton({
|
13
|
+
success: function() {}
|
14
|
+
})
|
15
|
+
;
|
16
|
+
|
17
|
+
in our example api is automapped to an object literal
|
18
|
+
@ quirky.config.endpoint.api
|
19
|
+
|
20
|
+
HTML:
|
21
|
+
<div class="button" action="follow" data-id="5">
|
22
|
+
|
23
|
+
URL : quirky.config.endpoint.api.follow
|
24
|
+
Given Value: /follow/{$id}/
|
25
|
+
Sent Value : /follow/5/
|
26
|
+
|
27
|
+
(4 ways to map api endpoint, each will be looked for in succession)
|
28
|
+
url mapping order:
|
29
|
+
first : defined in plugin init as url (arbitrary url)
|
30
|
+
second : defined in plugin init as action (action in obj literal grouping 'api')
|
31
|
+
third : defined in data-url
|
32
|
+
fourth : defined in data-action
|
33
|
+
|
34
|
+
beforeSend:
|
35
|
+
this callback can be used to modify request settings before XHR
|
36
|
+
it also can be used to look for for pre-conditions to prevent API
|
37
|
+
call by returning "false"
|
38
|
+
|
39
|
+
****************************** */
|
40
|
+
|
41
|
+
;(function ( $, window, document, undefined ) {
|
42
|
+
|
43
|
+
$.api = $.fn.api = function(parameters) {
|
44
|
+
|
45
|
+
var
|
46
|
+
settings = $.extend(true, {}, $.api.settings, parameters),
|
47
|
+
|
48
|
+
// if this keyword isn't a jQuery object, create one
|
49
|
+
context = (typeof this != 'function')
|
50
|
+
? this
|
51
|
+
: $('<div/>'),
|
52
|
+
// context defines the element used for loading/error state
|
53
|
+
$context = (settings.stateContext)
|
54
|
+
? $(settings.stateContext)
|
55
|
+
: $(context),
|
56
|
+
// module is the thing that initiates the api action, can be independent of context
|
57
|
+
$module = typeof this == 'object'
|
58
|
+
? $(context)
|
59
|
+
: $context,
|
60
|
+
|
61
|
+
element = this,
|
62
|
+
time = new Date().getTime(),
|
63
|
+
performance = [],
|
64
|
+
|
65
|
+
moduleSelector = $module.selector || '',
|
66
|
+
moduleNamespace = settings.namespace + '-module',
|
67
|
+
|
68
|
+
className = settings.className,
|
69
|
+
metadata = settings.metadata,
|
70
|
+
error = settings.error,
|
71
|
+
|
72
|
+
instance = $module.data(moduleNamespace),
|
73
|
+
|
74
|
+
query = arguments[0],
|
75
|
+
methodInvoked = (instance !== undefined && typeof query == 'string'),
|
76
|
+
queryArguments = [].slice.call(arguments, 1),
|
77
|
+
|
78
|
+
module,
|
79
|
+
invokedResponse
|
80
|
+
;
|
81
|
+
|
82
|
+
module = {
|
83
|
+
initialize: function() {
|
84
|
+
var
|
85
|
+
runSettings,
|
86
|
+
|
87
|
+
loadingTimer = new Date().getTime(),
|
88
|
+
loadingDelay,
|
89
|
+
|
90
|
+
promise,
|
91
|
+
url,
|
92
|
+
|
93
|
+
formData = {},
|
94
|
+
data,
|
95
|
+
|
96
|
+
ajaxSettings = {},
|
97
|
+
xhr
|
98
|
+
;
|
99
|
+
|
100
|
+
// serialize parent form if requested!
|
101
|
+
if(settings.serializeForm && $(this).toJSON() !== undefined) {
|
102
|
+
formData = module.get.formData();
|
103
|
+
module.debug('Adding form data to API Request', formData);
|
104
|
+
$.extend(true, settings.data, formData);
|
105
|
+
}
|
106
|
+
|
107
|
+
// let beforeSend change settings object
|
108
|
+
runSettings = $.proxy(settings.beforeSend, $module)(settings);
|
109
|
+
|
110
|
+
// check for exit conditions
|
111
|
+
if(runSettings !== undefined && !runSettings) {
|
112
|
+
module.error(error.beforeSend);
|
113
|
+
module.reset();
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
|
117
|
+
// get real url from template
|
118
|
+
url = module.get.url( module.get.templateURL() );
|
119
|
+
|
120
|
+
// exit conditions reached from missing url parameters
|
121
|
+
if( !url ) {
|
122
|
+
module.error(error.missingURL);
|
123
|
+
module.reset();
|
124
|
+
return;
|
125
|
+
}
|
126
|
+
|
127
|
+
// promise handles notification on api request, so loading min. delay can occur for all notifications
|
128
|
+
promise =
|
129
|
+
$.Deferred()
|
130
|
+
.always(function() {
|
131
|
+
if(settings.stateContext) {
|
132
|
+
$context
|
133
|
+
.removeClass(className.loading)
|
134
|
+
;
|
135
|
+
}
|
136
|
+
$.proxy(settings.complete, $module)();
|
137
|
+
})
|
138
|
+
.done(function(response) {
|
139
|
+
module.debug('API request successful');
|
140
|
+
// take a stab at finding success state if json
|
141
|
+
if(settings.dataType == 'json') {
|
142
|
+
if (response.error !== undefined) {
|
143
|
+
$.proxy(settings.failure, $context)(response.error, settings, $module);
|
144
|
+
}
|
145
|
+
else if ($.isArray(response.errors)) {
|
146
|
+
$.proxy(settings.failure, $context)(response.errors[0], settings, $module);
|
147
|
+
}
|
148
|
+
else {
|
149
|
+
$.proxy(settings.success, $context)(response, settings, $module);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
// otherwise
|
153
|
+
else {
|
154
|
+
$.proxy(settings.success, $context)(response, settings, $module);
|
155
|
+
}
|
156
|
+
})
|
157
|
+
.fail(function(xhr, status, httpMessage) {
|
158
|
+
var
|
159
|
+
errorMessage = (settings.error[status] !== undefined)
|
160
|
+
? settings.error[status]
|
161
|
+
: httpMessage,
|
162
|
+
response
|
163
|
+
;
|
164
|
+
// let em know unless request aborted
|
165
|
+
if(xhr !== undefined) {
|
166
|
+
// readyState 4 = done, anything less is not really sent
|
167
|
+
if(xhr.readyState !== undefined && xhr.readyState == 4) {
|
168
|
+
|
169
|
+
// if http status code returned and json returned error, look for it
|
170
|
+
if( xhr.status != 200 && httpMessage !== undefined && httpMessage !== '') {
|
171
|
+
module.error(error.statusMessage + httpMessage);
|
172
|
+
}
|
173
|
+
else {
|
174
|
+
if(status == 'error' && settings.dataType == 'json') {
|
175
|
+
try {
|
176
|
+
response = $.parseJSON(xhr.responseText);
|
177
|
+
if(response && response.error !== undefined) {
|
178
|
+
errorMessage = response.error;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
catch(error) {
|
182
|
+
module.error(error.JSONParse);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
$context
|
187
|
+
.removeClass(className.loading)
|
188
|
+
.addClass(className.error)
|
189
|
+
;
|
190
|
+
// show error state only for duration specified in settings
|
191
|
+
if(settings.errorLength > 0) {
|
192
|
+
setTimeout(function(){
|
193
|
+
$context
|
194
|
+
.removeClass(className.error)
|
195
|
+
;
|
196
|
+
}, settings.errorLength);
|
197
|
+
}
|
198
|
+
module.debug('API Request error:', errorMessage);
|
199
|
+
$.proxy(settings.failure, $context)(errorMessage, settings, this);
|
200
|
+
}
|
201
|
+
else {
|
202
|
+
module.debug('Request Aborted (Most likely caused by page change)');
|
203
|
+
}
|
204
|
+
}
|
205
|
+
})
|
206
|
+
;
|
207
|
+
|
208
|
+
// look for params in data
|
209
|
+
$.extend(true, ajaxSettings, settings, {
|
210
|
+
success : function(){},
|
211
|
+
failure : function(){},
|
212
|
+
complete : function(){},
|
213
|
+
type : settings.method || settings.type,
|
214
|
+
data : data,
|
215
|
+
url : url,
|
216
|
+
beforeSend : settings.beforeXHR
|
217
|
+
});
|
218
|
+
|
219
|
+
if(settings.stateContext) {
|
220
|
+
$context
|
221
|
+
.addClass(className.loading)
|
222
|
+
;
|
223
|
+
}
|
224
|
+
|
225
|
+
if(settings.progress) {
|
226
|
+
module.verbose('Adding progress events');
|
227
|
+
$.extend(true, ajaxSettings, {
|
228
|
+
xhr: function() {
|
229
|
+
var
|
230
|
+
xhr = new window.XMLHttpRequest()
|
231
|
+
;
|
232
|
+
xhr.upload.addEventListener('progress', function(event) {
|
233
|
+
var
|
234
|
+
percentComplete
|
235
|
+
;
|
236
|
+
if (event.lengthComputable) {
|
237
|
+
percentComplete = Math.round(event.loaded / event.total * 10000) / 100 + '%';
|
238
|
+
$.proxy(settings.progress, $context)(percentComplete, event);
|
239
|
+
}
|
240
|
+
}, false);
|
241
|
+
xhr.addEventListener('progress', function(event) {
|
242
|
+
var
|
243
|
+
percentComplete
|
244
|
+
;
|
245
|
+
if (event.lengthComputable) {
|
246
|
+
percentComplete = Math.round(event.loaded / event.total * 10000) / 100 + '%';
|
247
|
+
$.proxy(settings.progress, $context)(percentComplete, event);
|
248
|
+
}
|
249
|
+
}, false);
|
250
|
+
return xhr;
|
251
|
+
}
|
252
|
+
});
|
253
|
+
}
|
254
|
+
|
255
|
+
module.verbose('Creating AJAX request with settings: ', ajaxSettings);
|
256
|
+
xhr =
|
257
|
+
$.ajax(ajaxSettings)
|
258
|
+
.always(function() {
|
259
|
+
// calculate if loading time was below minimum threshold
|
260
|
+
loadingDelay = ( settings.loadingLength - (new Date().getTime() - loadingTimer) );
|
261
|
+
settings.loadingDelay = loadingDelay < 0
|
262
|
+
? 0
|
263
|
+
: loadingDelay
|
264
|
+
;
|
265
|
+
})
|
266
|
+
.done(function(response) {
|
267
|
+
var
|
268
|
+
context = this
|
269
|
+
;
|
270
|
+
setTimeout(function(){
|
271
|
+
promise.resolveWith(context, [response]);
|
272
|
+
}, settings.loadingDelay);
|
273
|
+
})
|
274
|
+
.fail(function(xhr, status, httpMessage) {
|
275
|
+
var
|
276
|
+
context = this
|
277
|
+
;
|
278
|
+
// page triggers abort on navigation, dont show error
|
279
|
+
if(status != 'abort') {
|
280
|
+
setTimeout(function(){
|
281
|
+
promise.rejectWith(context, [xhr, status, httpMessage]);
|
282
|
+
}, settings.loadingDelay);
|
283
|
+
}
|
284
|
+
else {
|
285
|
+
$context
|
286
|
+
.removeClass(className.error)
|
287
|
+
.removeClass(className.loading)
|
288
|
+
;
|
289
|
+
}
|
290
|
+
})
|
291
|
+
;
|
292
|
+
if(settings.stateContext) {
|
293
|
+
$module
|
294
|
+
.data(metadata.promise, promise)
|
295
|
+
.data(metadata.xhr, xhr)
|
296
|
+
;
|
297
|
+
}
|
298
|
+
},
|
299
|
+
|
300
|
+
get: {
|
301
|
+
formData: function() {
|
302
|
+
return $module
|
303
|
+
.closest('form')
|
304
|
+
.toJSON()
|
305
|
+
;
|
306
|
+
},
|
307
|
+
templateURL: function() {
|
308
|
+
var
|
309
|
+
action = $module.data(settings.metadata.action) || settings.action || false,
|
310
|
+
url
|
311
|
+
;
|
312
|
+
if(action) {
|
313
|
+
module.debug('Creating url for: ', action);
|
314
|
+
if(settings.api[action] !== undefined) {
|
315
|
+
url = settings.api[action];
|
316
|
+
}
|
317
|
+
else {
|
318
|
+
module.error(error.missingAction);
|
319
|
+
}
|
320
|
+
}
|
321
|
+
// override with url if specified
|
322
|
+
if(settings.url) {
|
323
|
+
url = settings.url;
|
324
|
+
module.debug('Getting url', url);
|
325
|
+
}
|
326
|
+
return url;
|
327
|
+
},
|
328
|
+
url: function(url, urlData) {
|
329
|
+
var
|
330
|
+
urlVariables
|
331
|
+
;
|
332
|
+
if(url) {
|
333
|
+
urlVariables = url.match(settings.regExpTemplate);
|
334
|
+
urlData = urlData || settings.urlData;
|
335
|
+
|
336
|
+
if(urlVariables) {
|
337
|
+
module.debug('Looking for URL variables', urlVariables);
|
338
|
+
$.each(urlVariables, function(index, templateValue){
|
339
|
+
var
|
340
|
+
term = templateValue.substr( 2, templateValue.length - 3),
|
341
|
+
termValue = ($.isPlainObject(urlData) && urlData[term] !== undefined)
|
342
|
+
? urlData[term]
|
343
|
+
: ($module.data(term) !== undefined)
|
344
|
+
? $module.data(term)
|
345
|
+
: urlData[term]
|
346
|
+
;
|
347
|
+
module.verbose('Looking for variable', term, $module, $module.data(term), urlData[term]);
|
348
|
+
// remove optional value
|
349
|
+
if(termValue === false) {
|
350
|
+
module.debug('Removing variable from URL', urlVariables);
|
351
|
+
url = url.replace('/' + templateValue, '');
|
352
|
+
}
|
353
|
+
// undefined condition
|
354
|
+
else if(termValue === undefined || !termValue) {
|
355
|
+
module.error(error.missingParameter + term);
|
356
|
+
url = false;
|
357
|
+
return false;
|
358
|
+
}
|
359
|
+
else {
|
360
|
+
url = url.replace(templateValue, termValue);
|
361
|
+
}
|
362
|
+
});
|
363
|
+
}
|
364
|
+
}
|
365
|
+
return url;
|
366
|
+
}
|
367
|
+
},
|
368
|
+
|
369
|
+
// reset api request
|
370
|
+
reset: function() {
|
371
|
+
$module
|
372
|
+
.data(metadata.promise, false)
|
373
|
+
.data(metadata.xhr, false)
|
374
|
+
;
|
375
|
+
$context
|
376
|
+
.removeClass(className.error)
|
377
|
+
.removeClass(className.loading)
|
378
|
+
;
|
379
|
+
},
|
380
|
+
|
381
|
+
setting: function(name, value) {
|
382
|
+
if(value !== undefined) {
|
383
|
+
if( $.isPlainObject(name) ) {
|
384
|
+
$.extend(true, settings, name);
|
385
|
+
}
|
386
|
+
else {
|
387
|
+
settings[name] = value;
|
388
|
+
}
|
389
|
+
}
|
390
|
+
else {
|
391
|
+
return settings[name];
|
392
|
+
}
|
393
|
+
},
|
394
|
+
internal: function(name, value) {
|
395
|
+
if(value !== undefined) {
|
396
|
+
if( $.isPlainObject(name) ) {
|
397
|
+
$.extend(true, module, name);
|
398
|
+
}
|
399
|
+
else {
|
400
|
+
module[name] = value;
|
401
|
+
}
|
402
|
+
}
|
403
|
+
else {
|
404
|
+
return module[name];
|
405
|
+
}
|
406
|
+
},
|
407
|
+
debug: function() {
|
408
|
+
if(settings.debug) {
|
409
|
+
if(settings.performance) {
|
410
|
+
module.performance.log(arguments);
|
411
|
+
}
|
412
|
+
else {
|
413
|
+
module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
|
414
|
+
module.debug.apply(console, arguments);
|
415
|
+
}
|
416
|
+
}
|
417
|
+
},
|
418
|
+
verbose: function() {
|
419
|
+
if(settings.verbose && settings.debug) {
|
420
|
+
if(settings.performance) {
|
421
|
+
module.performance.log(arguments);
|
422
|
+
}
|
423
|
+
else {
|
424
|
+
module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
|
425
|
+
module.verbose.apply(console, arguments);
|
426
|
+
}
|
427
|
+
}
|
428
|
+
},
|
429
|
+
error: function() {
|
430
|
+
module.error = Function.prototype.bind.call(console.error, console, settings.moduleName + ':');
|
431
|
+
module.error.apply(console, arguments);
|
432
|
+
},
|
433
|
+
performance: {
|
434
|
+
log: function(message) {
|
435
|
+
var
|
436
|
+
currentTime,
|
437
|
+
executionTime,
|
438
|
+
previousTime
|
439
|
+
;
|
440
|
+
if(settings.performance) {
|
441
|
+
currentTime = new Date().getTime();
|
442
|
+
previousTime = time || currentTime;
|
443
|
+
executionTime = currentTime - previousTime;
|
444
|
+
time = currentTime;
|
445
|
+
performance.push({
|
446
|
+
'Element' : element,
|
447
|
+
'Name' : message[0],
|
448
|
+
'Arguments' : [].slice.call(message, 1) || '',
|
449
|
+
'Execution Time' : executionTime
|
450
|
+
});
|
451
|
+
}
|
452
|
+
clearTimeout(module.performance.timer);
|
453
|
+
module.performance.timer = setTimeout(module.performance.display, 100);
|
454
|
+
},
|
455
|
+
display: function() {
|
456
|
+
var
|
457
|
+
title = settings.moduleName + ':',
|
458
|
+
totalTime = 0
|
459
|
+
;
|
460
|
+
clearTimeout(module.performance.timer);
|
461
|
+
time = false;
|
462
|
+
$.each(performance, function(index, data) {
|
463
|
+
totalTime += data['Execution Time'];
|
464
|
+
});
|
465
|
+
title += ' ' + totalTime + 'ms';
|
466
|
+
if(moduleSelector) {
|
467
|
+
title += ' \'' + moduleSelector + '\'';
|
468
|
+
}
|
469
|
+
if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
|
470
|
+
console.groupCollapsed(title);
|
471
|
+
if(console.table) {
|
472
|
+
console.table(performance);
|
473
|
+
}
|
474
|
+
else {
|
475
|
+
$.each(performance, function(index, data) {
|
476
|
+
console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
|
477
|
+
});
|
478
|
+
}
|
479
|
+
console.groupEnd();
|
480
|
+
}
|
481
|
+
performance = [];
|
482
|
+
}
|
483
|
+
},
|
484
|
+
invoke: function(query, passedArguments, context) {
|
485
|
+
var
|
486
|
+
maxDepth,
|
487
|
+
found,
|
488
|
+
response
|
489
|
+
;
|
490
|
+
passedArguments = passedArguments || queryArguments;
|
491
|
+
context = element || context;
|
492
|
+
if(typeof query == 'string' && instance !== undefined) {
|
493
|
+
query = query.split(/[\. ]/);
|
494
|
+
maxDepth = query.length - 1;
|
495
|
+
$.each(query, function(depth, value) {
|
496
|
+
var camelCaseValue = (depth != maxDepth)
|
497
|
+
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
|
498
|
+
: query
|
499
|
+
;
|
500
|
+
if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
|
501
|
+
instance = instance[value];
|
502
|
+
}
|
503
|
+
else if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) {
|
504
|
+
instance = instance[camelCaseValue];
|
505
|
+
}
|
506
|
+
else if( instance[value] !== undefined ) {
|
507
|
+
found = instance[value];
|
508
|
+
return false;
|
509
|
+
}
|
510
|
+
else if( instance[camelCaseValue] !== undefined ) {
|
511
|
+
found = instance[camelCaseValue];
|
512
|
+
return false;
|
513
|
+
}
|
514
|
+
else {
|
515
|
+
module.error(error.method);
|
516
|
+
return false;
|
517
|
+
}
|
518
|
+
});
|
519
|
+
}
|
520
|
+
if ( $.isFunction( found ) ) {
|
521
|
+
response = found.apply(context, passedArguments);
|
522
|
+
}
|
523
|
+
else if(found !== undefined) {
|
524
|
+
response = found;
|
525
|
+
}
|
526
|
+
if($.isArray(invokedResponse)) {
|
527
|
+
invokedResponse.push(response);
|
528
|
+
}
|
529
|
+
else if(typeof invokedResponse == 'string') {
|
530
|
+
invokedResponse = [invokedResponse, response];
|
531
|
+
}
|
532
|
+
else if(response !== undefined) {
|
533
|
+
invokedResponse = response;
|
534
|
+
}
|
535
|
+
return found;
|
536
|
+
}
|
537
|
+
};
|
538
|
+
|
539
|
+
if(methodInvoked) {
|
540
|
+
if(instance === undefined) {
|
541
|
+
module.initialize();
|
542
|
+
}
|
543
|
+
module.invoke(query);
|
544
|
+
}
|
545
|
+
else {
|
546
|
+
if(instance !== undefined) {
|
547
|
+
module.destroy();
|
548
|
+
}
|
549
|
+
module.initialize();
|
550
|
+
}
|
551
|
+
|
552
|
+
return (invokedResponse !== undefined)
|
553
|
+
? invokedResponse
|
554
|
+
: this
|
555
|
+
;
|
556
|
+
};
|
557
|
+
|
558
|
+
// handle DOM attachment to API functionality
|
559
|
+
$.fn.apiButton = function(parameters) {
|
560
|
+
$(this)
|
561
|
+
.each(function(){
|
562
|
+
var
|
563
|
+
// if only function passed it is success callback
|
564
|
+
$module = $(this),
|
565
|
+
selector = $(this).selector || '',
|
566
|
+
|
567
|
+
settings = ( $.isFunction(parameters) )
|
568
|
+
? $.extend(true, {}, $.api.settings, $.fn.apiButton.settings, { stateContext: this, success: parameters })
|
569
|
+
: $.extend(true, {}, $.api.settings, $.fn.apiButton.settings, { stateContext: this}, parameters),
|
570
|
+
module
|
571
|
+
;
|
572
|
+
module = {
|
573
|
+
initialize: function() {
|
574
|
+
if(settings.context && selector !== '') {
|
575
|
+
$(settings.context)
|
576
|
+
.on(selector, 'click.' + settings.namespace, module.click)
|
577
|
+
;
|
578
|
+
}
|
579
|
+
else {
|
580
|
+
$module
|
581
|
+
.on('click.' + settings.namespace, module.click)
|
582
|
+
;
|
583
|
+
}
|
584
|
+
},
|
585
|
+
click: function() {
|
586
|
+
if(!settings.filter || $(this).filter(settings.filter).size() === 0) {
|
587
|
+
$.proxy( $.api, this )(settings);
|
588
|
+
}
|
589
|
+
}
|
590
|
+
};
|
591
|
+
module.initialize();
|
592
|
+
})
|
593
|
+
;
|
594
|
+
return this;
|
595
|
+
};
|
596
|
+
|
597
|
+
$.api.settings = {
|
598
|
+
name : 'API',
|
599
|
+
namespace : 'api',
|
600
|
+
|
601
|
+
debug : true,
|
602
|
+
verbose : true,
|
603
|
+
performance : true,
|
604
|
+
|
605
|
+
api : {},
|
606
|
+
|
607
|
+
beforeSend : function(settings) {
|
608
|
+
return settings;
|
609
|
+
},
|
610
|
+
beforeXHR : function(xhr) {},
|
611
|
+
success : function(response) {},
|
612
|
+
complete : function(response) {},
|
613
|
+
failure : function(errorCode) {},
|
614
|
+
progress : false,
|
615
|
+
|
616
|
+
error : {
|
617
|
+
missingAction : 'API action used but no url was defined',
|
618
|
+
missingURL : 'URL not specified for the API action',
|
619
|
+
missingParameter : 'Missing an essential URL parameter: ',
|
620
|
+
|
621
|
+
timeout : 'Your request timed out',
|
622
|
+
error : 'There was an error with your request',
|
623
|
+
parseError : 'There was an error parsing your request',
|
624
|
+
JSONParse : 'JSON could not be parsed during error handling',
|
625
|
+
statusMessage : 'Server gave an error: ',
|
626
|
+
beforeSend : 'The before send function has aborted the request',
|
627
|
+
exitConditions : 'API Request Aborted. Exit conditions met'
|
628
|
+
},
|
629
|
+
|
630
|
+
className: {
|
631
|
+
loading : 'loading',
|
632
|
+
error : 'error'
|
633
|
+
},
|
634
|
+
|
635
|
+
metadata: {
|
636
|
+
action : 'action',
|
637
|
+
promise : 'promise',
|
638
|
+
xhr : 'xhr'
|
639
|
+
},
|
640
|
+
|
641
|
+
regExpTemplate: /\{\$([A-z]+)\}/g,
|
642
|
+
|
643
|
+
action : false,
|
644
|
+
url : false,
|
645
|
+
urlData : false,
|
646
|
+
serializeForm : false,
|
647
|
+
|
648
|
+
stateContext : false,
|
649
|
+
|
650
|
+
method : 'get',
|
651
|
+
data : {},
|
652
|
+
dataType : 'json',
|
653
|
+
cache : true,
|
654
|
+
|
655
|
+
loadingLength : 200,
|
656
|
+
errorLength : 2000
|
657
|
+
|
658
|
+
};
|
659
|
+
|
660
|
+
$.fn.apiButton.settings = {
|
661
|
+
filter : '.disabled, .loading',
|
662
|
+
context : false,
|
663
|
+
stateContext : false
|
664
|
+
};
|
665
|
+
|
666
|
+
})( jQuery, window , document );
|