This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.js
170 lines (137 loc) · 4.51 KB
/
application.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
define([
'jquery',
'underscore',
'backbone',
'component',
'intent'
], function ($, _, Backbone, component, Intent) {
'use strict';
var definedComponents = {};
var Application = {
DEBUG: false,
/**
* Initalization functions
* @type {Function[]}
*/
_initCallbacks: [],
/**
* Components registered to trigger specific intent types
* Intent type: Component[]
*/
_intentHandlers: {},
/**
* Array of intents and callbacks that are sent when
* application is started
* @type {Array}
*/
_initIntents: [],
extend: function (options) {
return Object.create(Application);
},
/**
* Starts application. Calls initial callbacks and send start intents
*
* @param {Object} [options] Options passed to all initial callbacks
*/
start: function(options) {
if (this.DEBUG) console.info('APP Starting application..');
component._application = this;
Intent._application = this;
// Layout init function
if (window.layoutInit) window.layoutInit.call(this);
// Apply page specific init function if set
if (window.pageInit) window.pageInit.call(this);
var initCallback;
while (this._initCallbacks.length > 0) {
initCallback = this._initCallbacks.shift();
initCallback(options);
}
// Send start intents
var def, cmp;
while (this._initIntents.length > 0) {
def = this._initIntents.shift();
cmp = this.getComponent(def.uid);
cmp.start(Intent.create('predefined', def.data));
}
},
/**
* Defines component to be used in application
*
* @param {Componen} cmp
*/
defineComponent: function (cmp) {
if (this.DEBUG) console.info('APP defining ' + cmp.uid);
var inst = this,
intentTypes = [];
if (!cmp.uid) throw new Error('Component UID missing');
if (definedComponents[cmp.uid]) throw new Error('UID already in use: ' + cmp.uid);
definedComponents[cmp.uid] = cmp;
_.each(cmp.callbacks, function (callbacks) {
intentTypes = _.union(_.keys(callbacks), intentTypes);
});
// Bind intent types to component
_.each(intentTypes, function (intentType) {
inst.registerIntentHandler(intentType, cmp);
});
return cmp;
},
/**
* Creates intent that is sent when application starts
*
*/
predefined: function(uid, data) {
this._initIntents.push({
uid: uid,
data: data
});
},
/**
* Adds initialization callback function
*
* @param {Function} initFunction
*/
onInit: function(initFunction) {
this._initCallbacks.push(_.bind(initFunction, this));
},
/**
* Register component as intent handler
*
* @param {String} intentType
* @param {Component} component
*/
registerIntentHandler: function(intentType, component) {
if (!this._intentHandlers[intentType]) {
this._intentHandlers[intentType] = [];
}
this._intentHandlers[intentType].push(component);
},
/**
* Broadcasts intent
*
* @param {Intent} intent
* @return void
*/
sendIntent: function(intent) {
if (this._intentHandlers[intent.type]) {
_.forEach(this._intentHandlers[intent.type], function (component) {
if (!intent.handled) {
component.start(intent);
}
});
}
},
/**
* Returns component.
*
* This sould be used only for debugging and in server side view
* specific javascript.
* @param {String} uid Component uid
* @return {Component}
*/
getComponent: function(uid) {
if (!definedComponents[uid]) throw new Error('No component: ' + uid);
return definedComponents[uid];
}
};
return Application;
});