/
utils.js
115 lines (96 loc) · 2.58 KB
/
utils.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
/**
* Template utilities to asynchronously load the template files
* into the browser and make them available for their respective views
*/
window.templateUtils = utils = {
loadTemplates: function(views, cb) {
var tasks = [];
utils.queueJobs(views, tasks);
$.when.apply(null, tasks).done(cb);
},
queueJobs: function(views, tasks) {
_.each(views, function(view) { utils.setTasks(view, tasks) });
},
setTasks: function(view, tasks) {
if (window[view]) {
tasks.push(utils.tempify(view, utils.getView(view)));
} else {
utils.log(view);
}
},
tempify: function(view, temp) {
return utils.getTemplate(temp, function(data) {
utils.setTemplate(view, data);
});
},
log: function(view) {
console.log(view + ' not found');
},
getView: function(view) {
return 'app/templates/' + view + '.html';
},
getTemplate: function(temp, cb) {
return $.get(temp, cb);
},
setTemplate: function(view, temp) {
window[view].prototype.template = _.template(temp);
}
};
/**
* Global app utilities
*/
window.appUtils = {
startRouter: function(router) {
var app = new router();
Backbone.history.start();
return app;
},
injectRouter: function(views, router) {
_.each(views, function(view) {
window[view].prototype.router = router;
});
},
checkForToken: function() {
if (window.localStorage.getItem('_user.token')) {
this.globalNavAuth = new GlobalNavViewAuth();
appUtils.swapView(this.globalNavAuth, '.main-header-container');
return true;
}
this.globalNav = new GlobalNavView();
appUtils.swapView(this.globalNav, '.main-header-container');
},
swapView: function(view, container) {
container = container || '.global-container';
$(container).html(view.el);
this.footerFix.call(this);
},
clearFields: function(fields) {
if (Array.isArray(fields)) {
_.each(fields, function(field) { field.val(''); });
} else {
fields.val('');
}
},
footerFix: function() {
var $footer = $('.main-footer');
if (window.innerHeight > $('body').innerHeight() + 50) {
$footer.addClass('fixed');
} else {
$footer.removeClass('fixed');
}
},
getStepsData: function(fields, model) {
var steps = [];
fields.each(function(idx, field) {
var $field = $(field);
var stepData = {
stepNum: $field[0].attributes.name.value.split('').pop(),
content: $field.val(),
guideId: model.get('_id'),
userId: model.get('userId')
};
steps.push(stepData);
});
return steps;
}
};