This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyre.js
740 lines (659 loc) · 20.5 KB
/
gyre.js
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*!
* Gyre.js 1.0.0
* @author: Brandt A. Abbott
*
* Includes Simple JavaScript Inheritance
* http://ejohn.org/
*
* Released under the MIT license
*
* Date: 2014-01-23T21:02Z
*/
(function(global) {
"use strict";
var fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
function BaseClass(){}
// Create a new Class that inherits from this class
BaseClass.extend = function(props) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
var proto = Object.create(_super);
// Copy the properties over onto the new prototype
for (var name in props) {
// Check if we're overwriting an existing function
proto[name] = typeof props[name] === "function" &&
typeof _super[name] == "function" && fnTest.test(props[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, props[name]) :
props[name];
}
// The new constructor
var newClass = typeof proto.init === "function" ?
proto.init : // All construction is actually done in the init method
function(){};
// Populate our constructed prototype object
newClass.prototype = proto;
// Enforce the constructor to be what we expect
proto.constructor = newClass;
// And make this class extendable
newClass.extend = BaseClass.extend;
return newClass;
};
// export
global.Class = BaseClass;
})(this);
if ('undefined' === typeof Gyre) {
/**
* Gyre
* @namespace
* @version 1.0.0
*/
Gyre = {};
Gyre.version = '1.0.0';
console.clear();
console.info('Gyre version:',Gyre.version);
}
Gyre.Logging = function(level){
if(typeof(level) == 'undefined' || level == null)
return;
if(level=='DEBUG'){
//Modify console logging
if(window.console && console.debug){
var old = console.debug;
console.debug = function(){
Array.prototype.unshift.call(arguments, 'DEBUG');
old.apply(this, arguments);
}
}
//Hack for IE 9 to work correctly
else if(window.console && !console.debug && console.log){
var old = console.log;
console.debug = function(){
Function.prototype.bind.call(old, console).apply(this, arguments);
}
}
}
else{
console.debug = function(){};
}
};
/**
* Gyre.MVC
* @namespace
*/
Gyre.MVC = {};
/**
* jQuery
*/
Gyre.$ = jQuery;
/**
* Global Environment
*/
Gyre.ENV = {};
/**
* A class to hold a socket.io instance. Each instance forces a new websocket connection.
* @class
* @memberOf Gyre
* @example var webSocket = new Gyre.WebSocket();
* webSocket.socket.emit('foo');
*/
Gyre.WebSocket = Class.extend({
init: function() {
this.name="Gyre.WebSocket";
/**
* @member {object} socket
* @memberOf Gyre.WebSocket#
*/
this.socket=io.connect('http://'+location.host, {'force new connection': true});
console.debug(this.name+'.init - WebSocket initialized and ready for use');
}
});
/**
* AJAX Loading class.
* @class
* @memberOf Gyre
*/
Gyre.AjaxLoader = Class.extend({
init: function() {
this.name="Gyre.AjaxLoader";
},
/**
* @method
* @memberOf Gyre.AjaxLoader#
* @param {string} url - url
* @param {string} data - data to pass to the request
*/
loadJSON: function(url, data){
self = this;
return Gyre.$.ajax({
dataType: "json",
url: url,
data: data,
cache: false
});
},
/**
* @method
* @memberOf Gyre.AjaxLoader#
* @param {string} url - url
* @param {string} data - data to pass to the request
*/
load: function(url, data){
return Gyre.$.ajax({
url: url,
async: false,
type: "GET",
cache: false
});
},
/**
* @method
* @memberOf Gyre.AjaxLoader#
* @param {string} url - url
* @param {string} data - data to pass to the request
*/
recoverFrom401: function(url, data){
console.debug(this.name+'.recoverFrom401 - ','Recovering from 401, sending request again to url:',url);
return Gyre.$.ajax({
headers:Gyre.ENV.AUTHORIZATION_HEADERS,
dataType: "json",
url: url,
data: data,
cache: false
});
}
});
/**
* AJAX Post class.
* @class
* @memberOf Gyre
*/
Gyre.AjaxPoster = Class.extend({
init: function() {
this.name="Gyre.AjaxPoster";
},
/**
* @method
* @memberOf Gyre.AjaxPoster#
* @param {string} url - url
* @param {string} data - json serialized form data
* @param {function} beforeSend - beforeSend callback
* @param {function} beforeSend - success callback
* @param {function} error - error callback
*/
post: function(url, data, beforeSend, success, error){
return Gyre.$.ajax({
type: "POST",
dataType: "json",
url: url,
data: data,
cache: false,
beforeSend: beforeSend,
success: success,
error: error
});
}
});
/**
* {@link http://handlebarsjs.com Handlebars} utility class
* @class
* @memberOf Gyre
*/
Gyre.Handlebars = Class.extend({
init: function() {
this.name="Gyre.Handlebars";
},
/**
* Loads a {@link http://handlebarsjs.com handlebars} template from a URL (via AJAX) and then renders it
* @method
* @memberOf Gyre.Handlebars#
* @param {string} url - url of the .hbs template
* @param {string} templateId - the Id of the handlebars template script tag
* @param {string} context - the data to load into the template
*/
loadAndRenderHandlebarsTemplate: function(url, templateId, context) {
var self = this;
var loader = new Gyre.AjaxLoader();
loader.load(url).done(function(template){
Gyre.$('#'+templateId).html(template);
self.renderHandlebarsTemplate(templateId,context);
});
},
/**
* Renders a {@link http://handlebarsjs.com handlebars} template
* @method
* @memberOf Gyre.Handlebars#
* @param {string} templateId - the Id of the handlebars template script tag
* @param {string} context - the data to load into the template
*/
renderHandlebarsTemplate: function(templateId, context){
console.debug(this.name+'.renderHandlebarsTemplate - template:',templateId,'context:',context);
var template = Handlebars.compile(Gyre.$('#'+templateId).html());
if(Gyre.$('.'+templateId).length==0)
Gyre.$(template(context)).insertBefore(Gyre.$('#'+templateId));
else
Gyre.$('.'+templateId).replaceWith(template(context));
}
});
/**
* @class
* @memberOf Gyre.MVC
* @example //Extend as follows:
MyApp.MyModel = Gyre.MVC.Model.extend({
init: function() {
this._super();
this.name='MyApp.MyModel';
}
});
*/
Gyre.MVC.Model = Class.extend({
init: function() {
this.name="Gyre.MVC.Model";
this.content = null;
this.contentToSave = null;
this.isLoaded = false;
this.promise = Gyre.$.Deferred();
this.lastURL = null;
this.lastData = null;
this.gyreWebSocket = null;
this.loadURL = null;
this.saveURL = null;
},
/**
* Used to munge/serialize json content
* @method
* @memberOf Gyre.MVC.Model#
* @param {string} content - json content loaded into the model
*/
serialize: function(content) {
},
/**
* Utilizes jQuery to load a model via AJAX
* @method
* @memberOf Gyre.MVC.Model#
* @param {string} url - url of the JSON model
* @param {string} data - data to pass to the request
*/
loadModel: function(url, data) {
//Store the last url, and last data for reLoadModel
this.lastURL = url;
this.lastData = data;
console.debug(this.name+'.loadModel -','loading model from url:',url);
var self = this;
var loader = new Gyre.AjaxLoader();
self.promise = loader.loadJSON(url, data)
.then(function(data, textStatus, jqXHR) {
self.content = data;
self.isLoaded = true;
}, function(jqXHR, textStatus, errorThrown) {
if(jqXHR.status===401){
return loader.recoverFrom401(url, data)
.done(function(data, textStatus, jqXHR){
self.content = data;
self.isLoaded = true;
});
}
});
},
/**
* Utilizes socket.io to load a model
* @method
* @memberOf Gyre.MVC.Model#
* @param {string} emitEvent - Event to emit. Server should listen for this event and emit "data" and/or "error".
* @param {object} emitObject - Object data to emit to the server
*/
loadModelFromWebSocket: function(emitEvent, emitObject){
var self = this;
this.gyreWebSocket = new Gyre.WebSocket();
this.gyreWebSocket.socket.emit(emitEvent,emitObject);
console.debug(this.name+'.loadModelFromWebSocket -','loading model from websocket with:',emitEvent,emitObject);
this.gyreWebSocket.socket.on('data', function(data){
console.debug(self.name+'.loadModelFromWebSocket -','received data from WebSocket:',data);
self.content = data.json;
self.isLoaded = true;
self.promise.notify(data.json);
});
this.gyreWebSocket.socket.on('error', function(e){
console.debug(self.name+'.loadModelFromWebSocket -','could not load model from websocket because of:',e);
Gyre.Messaging.PageMessage.pushAutoFadeOutMessage('There is a problem communicating with the server. Attempting to reconnect...', '.content', 'alert-warning"');
});
},
/**
* Reload a model loaded via AJAX (loadModel). Models loaded via WebSocket should utilize the socket to reload the model.
* @method
* @memberOf Gyre.MVC.Model#
*/
reLoadModel: function() {
var self = this;
if(this.isLoaded)
this.loadModel(this.lastURL, this.lastData);
else
this.promise.then(function() {this.loadModel(self.lastURL, self.lastData)});
},
save: function() {
console.debug(this.name+" - saving model with data: ",this.contentToSave);
Gyre.Messaging.PageMessage.clearAll();
Gyre.Messaging.FieldMessage.clearAll();
return this.saveModel(this.saveURL, Gyre.$(this.contentToSave.form).serialize(), this.contentToSave.formId);
},
/**
* Save a model via AJAX. Must provide a serialized form
* @method
* @memberOf Gyre.MVC.Model#
* @param {string} url - URL to POST to
* @param {string} data - serialized data to POST
* @param {function} beforeSend - optional callback
* @param {function} success - optional callback
* @param {function} error - optional callback
*/
saveModel: function(url, postData, formId, beforeSend, success, error) {
var self = this;
var poster = new Gyre.AjaxPoster();
beforeSend = typeof(beforeSend)!='undefined' ? beforeSend : function(request){
Gyre.Messaging.PageMessage.clearAll();
Gyre.Messaging.FieldMessage.clearAll();
};
success = typeof(success)!='undefined' ? success : function(data, textStatus, XMLHttpRequest){
//If there is no data returned, then display an error
if(typeof(data) == 'undefined' || data == null){
//Return to login hack
if(XMLHttpRequest.status == 200)
Gyre.Messaging.ReLoginModal.show();
}
//If there are errors, then display them
//Assume: Errors are in data.fieldErrorList
else if(!data.success && data.fieldErrorList){
Gyre.Messaging.PageMessage.pushMessage('Changes not saved. Please see error messages below.', '.content', 'alert-danger');
//Display field error messages from the response, i.e. {fieldErrorList: {field: "foo", defaultMessage: "errorMessage"}})
var fieldsWithErrors = [];
$.each(data.fieldErrorList.reverse(), function(){
if($.inArray(this.field, fieldsWithErrors) == -1){
fieldsWithErrors.push(this.field);
Gyre.Messaging.FieldMessage.pushMessage(this.defaultMessage, Gyre.Utilities.esc('#'+formId+' [name="'+this.field+'"]'), 'error');
}
});
}
else if(!data.success && !data.fieldErrorList){
Gyre.Messaging.PageMessage.pushMessage('Changes not saved. Please refresh the page and try again.', '.content', 'alert-danger');
}
else if(data.success){
Gyre.Messaging.PageMessage.clearAll();
Gyre.Messaging.PageMessage.pushAutoFadeOutMessage('Information saved.', '.content', 'alert-success');
}
};
error = typeof(error)!='undefined' ? success : function(request, status, error){
//Return to login hack
if(request.status == 200)
Gyre.Messaging.ReLoginModal.show();
else
Gyre.Messaging.PageMessage.pushMessage('Changes not saved. Please refresh the page and try again.', '.content', 'alert-danger');
};
return poster.post(url,postData,beforeSend,success,error);
},
/**
* Utilizes jQuery to delete a model via AJAX
* @method
* @memberOf Gyre.MVC.Model#
* @param {string} url - url of the JSON model
* @param {string} data - data to pass to the request
*/
remove: function(url, data) {
console.debug(this.name+'.delete -','delete calling url:',url);
var self = this;
var loader = new Gyre.AjaxLoader();
self.promise = loader.loadJSON(url, data).then(function(data, textStatus, jqXHR){
self.content = data;
self.isLoaded = true;
}, function(jqXHR, textStatus, errorThrown) {
if(jqXHR.status===401){
return loader.recoverFrom401(url, data).done(function(data, textStatus, jqXHR){
self.content = data;
self.isLoaded = true;
});
}
});
}
});
/**
* @class
* @memberOf Gyre.MVC
* @example //Extend as follows:
MyApp.MyController = Gyre.MVC.Controller.extend({
init: function() {
this._super();
this.name="MyApp.MyController"
this.template='invoice';
}
});
*/
Gyre.MVC.Controller = Class.extend({
init: function() {
/**
* @member {String} template
* @memberOf Gyre.MVC.Controller#
*/
this.template = null;
this.name="Gyre.MVC.Controller"
},
/**
* Render a Handlebars template using the template set on the controller and the model
* @method
* @memberOf Gyre.MVC.Controller#
* @param {string} model - the model to render
*/
renderTemplate: function(model){
var handlebars = new Gyre.Handlebars();
handlebars.renderHandlebarsTemplate(this.template, model);
},
/**
* Load a Handlebars template, then render it using template set on the controller and the model
* @method
* @memberOf Gyre.MVC.Controller#
* @param {string} model - the model to render
*/
renderLoadedTemplate: function(model){
var handlebars = new Gyre.Handlebars();
handlebars.loadAndRenderHandlebarsTemplate(Gyre.ENV.HANDLEBARS_TEMPLATE_URL+this.template+'.hbs', this.template, model);
}
});
/**
* Gyre.Messaging
* @namespace
*/
Gyre.Messaging = {};
/**
* @static
* @example
//Create a page message 'Foo' prepended to #mydiv with bg-success color
Gyre.Messaging.PageMessage.pushMessage('Foo', '#myDiv', 'bg-success');
*/
Gyre.Messaging.PageMessage = {
pushMessage: function(message, selector, level){
this.clearMessage(selector);
//This is kinda crapy, oh well
var constructedMessage = '<div id="gyreMessage" style="margin: 5px 100px 10px 100px; display: none" class="alert '+level+' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>'+message+'</div>';
Gyre.$(constructedMessage).prependTo(selector).fadeIn(400);
},
pushAutoFadeOutMessage: function(message, selector, level){
var id = new Date().getTime();
var constructedMessage = '<div id="gyreFadingMessage'+id+'" style="margin: 5px 100px 10px 100px; display: none" class="alert '+level+' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>'+message+'</div>';
Gyre.$(constructedMessage).prependTo(selector).fadeIn(400);
Gyre.$('#gyreFadingMessage'+id).delay(5000).fadeOut(400).queue(function(){$(this).remove();});
},
clearMessage: function(selector){
if(Gyre.$(selector+' :first').attr('id')==='gyreMessage')
Gyre.$(selector+' :first').remove();
},
clearAll: function() {
Gyre.$('#gyreMessage').remove();
Gyre.$('[id*=gyreFadingMessage]').remove();
}
};
/**
* @static
* @example
//Create a field message 'Foo'
Gyre.Messaging.FieldMessage.pushMessage('Foo', '#myDiv', 'bg-success');
*/
Gyre.Messaging.FieldMessage = {
pushMessage: function(message, selector, level){
var id = new Date().getTime();
if($(selector).offset() != null && $(selector).offset != 'undefined'){
offLeft = $(selector).offset().left;
div = $('<div id="gyreFieldMessage'+id+'" class="'+level+'" style="white-space:pre-wrap;">'+message+'</div>');
$(selector).parent().last().append(div);
$(selector).parent().find('div#gyreFieldMessage'+id).offset({left: offLeft, top: $(selector).parent().find('div#gyreFieldMessage'+id).offset().top});
}
},
clearAll: function(){
Gyre.$('div[id*="gyreFieldMessage"]').remove();
}
};
/**
* @static
* @example
//Create a new spinner at the top of the #foo element
Gyre.Messaging.LoadingSpinner.startSpinner('#foo');
*/
Gyre.Messaging.LoadingSpinner = {
spinner: null,
/**
* @memberOf Gyre.Messaging.LoadingSpinner#
* @param {string} String name of the element where the spinner should go
*/
startSpinner: function(id){
if(this.spinner!=null){
this.spinner.start();
return;
}
this.spinner = new imageLoader(cImageSrc, 'startAnimation()');
},
/**
* @memberOf Gyre.Messaging.LoadingSpinner#
*/
stopSpinner: function() {
this.spinner.stopAnimation();
}
};
/**
* @static
* @example
//Show a Modal for the user to re-login and kick them back to "/"
Gyre.Messaging.ReLoginModal();
*/
Gyre.Messaging.ReLoginModal = {
show: function() {
Gyre.Messaging.Modal.show('Error: Session Expired', 'Your session has expired. Please re-login', [
$('<button/>',{
text: 'OK',
'class': 'btn btn-primary',
click: function() {
location.href="/";
Gyre.Messaging.Modal.hide();
}
})
]);
}
};
/**
* @static
*/
Gyre.Messaging.Modal = {
show: function(title, body, buttons){
Gyre.$('.modal-title').text(title);
Gyre.$('.modal-body').html(body);
Gyre.$('.modal-footer').html(buttons);
$('.modal').modal('show');
},
hide: function() {
$('.modal').modal('hide');
}
}
/**
* Gyre.Namespace - Used to create a custom namespace
* @namespace
*/
Gyre.Namespace = {
/**
* @memberOf Gyre.Namespace#
* @returns {object}
* @example var MyApp = Gyre.Namespace.createNS('MyApp');
*/
createNS: function(name){
var NS = {};
return NS;
}
};
/**
* Gyre.Utilities
* @namespace
* @memberOf Gyre
*/
Gyre.Utilities = {
/**
* @static
* @memberOf Gyre.Utilities#
* @returns {string}
*/
getLastParameterFromPathhName: function(){
var splitPathName = window.location.pathname.split('/');
var lastValue = splitPathName[splitPathName.length-1];
return lastValue;
},
/**
* @static
* @memberOf Gyre.Utilities#
* @returns {string}
* @example Gyre.Utilities.getParameterValueByName('q') //Returns 'foo' if location.search is '?q=foo'
*/
getParameterValueByName : function(name){
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else{
results[1] = results[1].replace(/\+/g, " ");
results[1] = results[1].replace(/\.json/g, "");
return decodeURIComponent(results[1]);
}
},
/**
* @static
* @memberOf Gyre.Utilities#
* @returns {string}
*/
compareFormObjects: function(originalForms,modifiedForms){
console.debug('Comparing ',originalForms,' and ',modifiedForms);
var formsToSave=[];
$.each(originalForms, function(index, obj){
$.each(modifiedForms, function(index, modObj){
if(modObj.formId == obj.formId){
var originalSerialized = $(obj.form).serialize();
var modSerialized = $(modObj.form).serialize();
if(originalSerialized != modSerialized){
formsToSave.push(modObj);
}
}
});
});
return formsToSave;
},
esc: function(str){
return str.replace(/([.])/g,'\\$1');
}
};