blob: 803607c1c99408bfc981a54c736d0bd92533e8ba [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Base Framework
19
20 @author Simon Hunt
21 */
22
23(function ($) {
24 'use strict';
25 var tsI = new Date().getTime(), // initialize time stamp
26 tsB, // build time stamp
Simon Hunt25248912014-11-04 11:25:48 -080027 mastHeight = 36, // see mast2.css
Simon Hunt142d0032014-11-04 20:13:09 -080028 defaultVid = 'sample';
Simon Hunt195cb382014-11-03 17:50:51 -080029
30
31 // attach our main function to the jQuery object
32 $.onos = function (options) {
Simon Hunt25248912014-11-04 11:25:48 -080033 var uiApi,
34 viewApi,
Simon Hunt1a9eff92014-11-07 11:06:34 -080035 navApi,
Simon Huntbb282f52014-11-10 11:08:19 -080036 libApi,
37 exported = {};
Simon Hunt25248912014-11-04 11:25:48 -080038
39 var defaultOptions = {
Simon Hunt142d0032014-11-04 20:13:09 -080040 trace: false,
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080041 theme: 'dark',
Simon Hunt142d0032014-11-04 20:13:09 -080042 startVid: defaultVid
Simon Hunt25248912014-11-04 11:25:48 -080043 };
44
45 // compute runtime settings
46 var settings = $.extend({}, defaultOptions, options);
Simon Hunt195cb382014-11-03 17:50:51 -080047
Thomas Vachuska65368e32014-11-08 16:10:20 -080048 // set the selected theme
49 d3.select('body').classed(settings.theme, true);
50
Simon Hunt195cb382014-11-03 17:50:51 -080051 // internal state
52 var views = {},
Simon Hunt61d04042014-11-11 17:27:16 -080053 fpanels = {},
Simon Hunt195cb382014-11-03 17:50:51 -080054 current = {
55 view: null,
Thomas Vachuska65368e32014-11-08 16:10:20 -080056 ctx: '',
Simon Hunt56d51852014-11-09 13:03:35 -080057 flags: {},
Thomas Vachuska65368e32014-11-08 16:10:20 -080058 theme: settings.theme
Simon Hunt195cb382014-11-03 17:50:51 -080059 },
60 built = false,
Simon Hunt61d04042014-11-11 17:27:16 -080061 buildErrors = [],
Simon Hunt934c3ce2014-11-05 11:45:07 -080062 keyHandler = {
Thomas Vachuska65368e32014-11-08 16:10:20 -080063 globalKeys: {},
64 maskedKeys: {},
65 viewKeys: {},
Simon Hunt87514342014-11-24 16:41:27 -080066 viewFn: null,
67 viewGestures: []
Thomas Vachuska65368e32014-11-08 16:10:20 -080068 },
69 alerts = {
70 open: false,
71 count: 0
Simon Hunt934c3ce2014-11-05 11:45:07 -080072 };
Simon Hunt195cb382014-11-03 17:50:51 -080073
74 // DOM elements etc.
Simon Hunt61d04042014-11-11 17:27:16 -080075 // TODO: verify existence of following elements...
76 var $view = d3.select('#view'),
77 $floatPanels = d3.select('#floatPanels'),
78 $alerts = d3.select('#alerts'),
79 // note, following elements added programmatically...
Simon Huntdb9eb072014-11-04 19:12:46 -080080 $mastRadio;
Simon Hunt195cb382014-11-03 17:50:51 -080081
82
Simon Hunt0df1b1d2014-11-04 22:58:29 -080083 function whatKey(code) {
84 switch (code) {
85 case 13: return 'enter';
86 case 16: return 'shift';
87 case 17: return 'ctrl';
88 case 18: return 'alt';
89 case 27: return 'esc';
90 case 32: return 'space';
91 case 37: return 'leftArrow';
92 case 38: return 'upArrow';
93 case 39: return 'rightArrow';
94 case 40: return 'downArrow';
95 case 91: return 'cmdLeft';
96 case 93: return 'cmdRight';
Simon Hunt8f40cce2014-11-23 15:57:30 -080097 case 187: return 'equals';
98 case 189: return 'dash';
Simon Hunt988c6fc2014-11-20 17:43:03 -080099 case 191: return 'slash';
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800100 case 192: return 'backQuote';
Simon Hunt41effbe2014-12-04 09:41:44 -0800101 case 220: return 'backSlash';
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800102 default:
103 if ((code >= 48 && code <= 57) ||
104 (code >= 65 && code <= 90)) {
105 return String.fromCharCode(code);
106 } else if (code >= 112 && code <= 123) {
107 return 'F' + (code - 111);
108 }
109 return '.';
110 }
111 }
112
113
Simon Hunt195cb382014-11-03 17:50:51 -0800114 // ..........................................................
115 // Internal functions
116
117 // throw an error
118 function throwError(msg) {
119 // separate function, as we might add tracing here too, later
120 throw new Error(msg);
121 }
122
123 function doError(msg) {
Simon Hunt25248912014-11-04 11:25:48 -0800124 console.error(msg);
Simon Hunt56d51852014-11-09 13:03:35 -0800125 doAlert(msg);
Simon Hunt25248912014-11-04 11:25:48 -0800126 }
127
128 function trace(msg) {
129 if (settings.trace) {
130 console.log(msg);
131 }
132 }
133
134 function traceFn(fn, params) {
135 if (settings.trace) {
136 console.log('*FN* ' + fn + '(...): ' + params);
137 }
Simon Hunt195cb382014-11-03 17:50:51 -0800138 }
139
140 // hash navigation
141 function hash() {
142 var hash = window.location.hash,
143 redo = false,
144 view,
145 t;
146
Simon Hunt25248912014-11-04 11:25:48 -0800147 traceFn('hash', hash);
148
Simon Hunt195cb382014-11-03 17:50:51 -0800149 if (!hash) {
Simon Hunt142d0032014-11-04 20:13:09 -0800150 hash = settings.startVid;
Simon Hunt195cb382014-11-03 17:50:51 -0800151 redo = true;
152 }
153
154 t = parseHash(hash);
155 if (!t || !t.vid) {
Simon Hunt56d51852014-11-09 13:03:35 -0800156 doError('Unable to parse target hash: "' + hash + '"');
Simon Hunt195cb382014-11-03 17:50:51 -0800157 }
158
159 view = views[t.vid];
160 if (!view) {
161 doError('No view defined with id: ' + t.vid);
162 }
163
164 if (redo) {
165 window.location.hash = makeHash(t);
166 // the above will result in a hashchange event, invoking
167 // this function again
168 } else {
169 // hash was not modified... navigate to where we need to be
170 navigate(hash, view, t);
171 }
Simon Hunt195cb382014-11-03 17:50:51 -0800172 }
173
174 function parseHash(s) {
175 // extract navigation coordinates from the supplied string
Simon Hunt56d51852014-11-09 13:03:35 -0800176 // "vid,ctx?flag1,flag2" --> { vid:vid, ctx:ctx, flags:{...} }
Simon Hunt25248912014-11-04 11:25:48 -0800177 traceFn('parseHash', s);
Simon Hunt195cb382014-11-03 17:50:51 -0800178
Simon Hunt56d51852014-11-09 13:03:35 -0800179 // look for use of flags, first
180 var vidctx,
181 vid,
182 ctx,
183 flags,
184 flagMap,
185 m;
186
187 // RE that includes flags ('?flag1,flag2')
188 m = /^[#]{0,1}(.+)\?(.+)$/.exec(s);
Simon Hunt195cb382014-11-03 17:50:51 -0800189 if (m) {
Simon Hunt56d51852014-11-09 13:03:35 -0800190 vidctx = m[1];
191 flags = m[2];
192 flagMap = {};
193 } else {
194 // no flags
195 m = /^[#]{0,1}((.+)(,.+)*)$/.exec(s);
196 if (m) {
197 vidctx = m[1];
198 } else {
199 // bad hash
200 return null;
201 }
Simon Hunt195cb382014-11-03 17:50:51 -0800202 }
203
Simon Hunt56d51852014-11-09 13:03:35 -0800204 vidctx = vidctx.split(',');
205 vid = vidctx[0];
206 ctx = vidctx[1];
207 if (flags) {
208 flags.split(',').forEach(function (f) {
209 flagMap[f.trim()] = true;
210 });
211 }
212
213 return {
214 vid: vid.trim(),
215 ctx: ctx ? ctx.trim() : '',
216 flags: flagMap
217 };
218
Simon Hunt195cb382014-11-03 17:50:51 -0800219 }
220
Simon Hunt56d51852014-11-09 13:03:35 -0800221 function makeHash(t, ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800222 traceFn('makeHash');
Simon Hunt56d51852014-11-09 13:03:35 -0800223 // make a hash string from the given navigation coordinates,
224 // and optional flags map.
Simon Hunt195cb382014-11-03 17:50:51 -0800225 // if t is not an object, then it is a vid
226 var h = t,
Simon Hunt56d51852014-11-09 13:03:35 -0800227 c = ctx || '',
228 f = $.isPlainObject(flags) ? flags : null;
Simon Hunt195cb382014-11-03 17:50:51 -0800229
230 if ($.isPlainObject(t)) {
231 h = t.vid;
232 c = t.ctx || '';
Simon Hunt56d51852014-11-09 13:03:35 -0800233 f = t.flags || null;
Simon Hunt195cb382014-11-03 17:50:51 -0800234 }
235
236 if (c) {
237 h += ',' + c;
238 }
Simon Hunt56d51852014-11-09 13:03:35 -0800239 if (f) {
240 h += '?' + d3.map(f).keys().join(',');
241 }
Simon Hunt25248912014-11-04 11:25:48 -0800242 trace('hash = "' + h + '"');
Simon Hunt195cb382014-11-03 17:50:51 -0800243 return h;
244 }
245
246 function navigate(hash, view, t) {
Simon Hunt25248912014-11-04 11:25:48 -0800247 traceFn('navigate', view.vid);
Simon Hunt195cb382014-11-03 17:50:51 -0800248 // closePanes() // flyouts etc.
Simon Hunt25248912014-11-04 11:25:48 -0800249 // updateNav() // accordion / selected nav item etc.
Simon Hunt195cb382014-11-03 17:50:51 -0800250 createView(view);
251 setView(view, hash, t);
252 }
253
Simon Hunt61d04042014-11-11 17:27:16 -0800254 function buildError(msg) {
255 buildErrors.push(msg);
256 }
257
Simon Hunt195cb382014-11-03 17:50:51 -0800258 function reportBuildErrors() {
Simon Hunt25248912014-11-04 11:25:48 -0800259 traceFn('reportBuildErrors');
Simon Hunt61d04042014-11-11 17:27:16 -0800260 var nerr = buildErrors.length,
261 errmsg;
262 if (!nerr) {
263 console.log('(no build errors)');
264 } else {
265 errmsg = 'Build errors: ' + nerr + ' found...\n\n' +
266 buildErrors.join('\n');
267 doAlert(errmsg);
268 console.error(errmsg);
269 }
Simon Hunt195cb382014-11-03 17:50:51 -0800270 }
271
Simon Hunt25248912014-11-04 11:25:48 -0800272 // returns the reference if it is a function, null otherwise
273 function isF(f) {
274 return $.isFunction(f) ? f : null;
275 }
276
Simon Hunt988c6fc2014-11-20 17:43:03 -0800277 // returns the reference if it is an array, null otherwise
278 function isA(a) {
279 return $.isArray(a) ? a : null;
280 }
281
Simon Hunt195cb382014-11-03 17:50:51 -0800282 // ..........................................................
283 // View life-cycle functions
284
Simon Hunt25248912014-11-04 11:25:48 -0800285 function setViewDimensions(sel) {
286 var w = window.innerWidth,
287 h = window.innerHeight - mastHeight;
288 sel.each(function () {
289 $(this)
290 .css('width', w + 'px')
291 .css('height', h + 'px')
292 });
293 }
294
Simon Hunt195cb382014-11-03 17:50:51 -0800295 function createView(view) {
296 var $d;
Simon Hunt25248912014-11-04 11:25:48 -0800297
Simon Hunt195cb382014-11-03 17:50:51 -0800298 // lazy initialization of the view
299 if (view && !view.$div) {
Simon Hunt25248912014-11-04 11:25:48 -0800300 trace('creating view for ' + view.vid);
Simon Hunt195cb382014-11-03 17:50:51 -0800301 $d = $view.append('div')
302 .attr({
Simon Hunt25248912014-11-04 11:25:48 -0800303 id: view.vid,
304 class: 'onosView'
Simon Hunt195cb382014-11-03 17:50:51 -0800305 });
Simon Hunt25248912014-11-04 11:25:48 -0800306 setViewDimensions($d);
307 view.$div = $d; // cache a reference to the D3 selection
Simon Hunt195cb382014-11-03 17:50:51 -0800308 }
309 }
310
311 function setView(view, hash, t) {
Simon Hunt25248912014-11-04 11:25:48 -0800312 traceFn('setView', view.vid);
Simon Hunt195cb382014-11-03 17:50:51 -0800313 // set the specified view as current, while invoking the
314 // appropriate life-cycle callbacks
315
Simon Hunt56d51852014-11-09 13:03:35 -0800316 // first, we'll start by closing the alerts pane, if open
317 closeAlerts();
318
Simon Hunt195cb382014-11-03 17:50:51 -0800319 // if there is a current view, and it is not the same as
320 // the incoming view, then unload it...
Simon Hunt25248912014-11-04 11:25:48 -0800321 if (current.view && (current.view.vid !== view.vid)) {
Simon Hunt195cb382014-11-03 17:50:51 -0800322 current.view.unload();
Simon Huntdb9eb072014-11-04 19:12:46 -0800323
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800324 // detach radio buttons, key handlers, etc.
325 $('#mastRadio').children().detach();
Thomas Vachuska65368e32014-11-08 16:10:20 -0800326 keyHandler.viewKeys = {};
327 keyHandler.viewFn = null;
Simon Hunt195cb382014-11-03 17:50:51 -0800328 }
329
330 // cache new view and context
331 current.view = view;
332 current.ctx = t.ctx || '';
Simon Hunt56d51852014-11-09 13:03:35 -0800333 current.flags = t.flags || {};
Simon Hunt195cb382014-11-03 17:50:51 -0800334
Simon Hunta2994cc2014-12-02 14:19:15 -0800335 // init is called only once, after the view is in the DOM
336 if (!view.inited) {
337 view.init(current.ctx, current.flags);
338 view.inited = true;
Simon Hunt195cb382014-11-03 17:50:51 -0800339 }
340
341 // clear the view of stale data
342 view.reset();
343
344 // load the view
Simon Hunt56d51852014-11-09 13:03:35 -0800345 view.load(current.ctx, current.flags);
Simon Hunt195cb382014-11-03 17:50:51 -0800346 }
347
Simon Huntdb9eb072014-11-04 19:12:46 -0800348 // generate 'unique' id by prefixing view id
Simon Hunt934c3ce2014-11-05 11:45:07 -0800349 function makeUid(view, id) {
Simon Huntdb9eb072014-11-04 19:12:46 -0800350 return view.vid + '-' + id;
351 }
352
353 // restore id by removing view id prefix
Simon Hunt934c3ce2014-11-05 11:45:07 -0800354 function unmakeUid(view, uid) {
Simon Huntdb9eb072014-11-04 19:12:46 -0800355 var re = new RegExp('^' + view.vid + '-');
356 return uid.replace(re, '');
357 }
358
Simon Hunt934c3ce2014-11-05 11:45:07 -0800359 function setRadioButtons(vid, btnSet) {
Simon Huntdb9eb072014-11-04 19:12:46 -0800360 var view = views[vid],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800361 btnG,
362 api = {};
Simon Huntdb9eb072014-11-04 19:12:46 -0800363
364 // lazily create the buttons...
365 if (!(btnG = view.radioButtons)) {
366 btnG = d3.select(document.createElement('div'));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800367 btnG.buttonDef = {};
Simon Huntdb9eb072014-11-04 19:12:46 -0800368
369 btnSet.forEach(function (btn, i) {
370 var bid = btn.id || 'b' + i,
371 txt = btn.text || 'Button #' + i,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800372 uid = makeUid(view, bid),
373 button = btnG.append('span')
Simon Huntdb9eb072014-11-04 19:12:46 -0800374 .attr({
Simon Hunt934c3ce2014-11-05 11:45:07 -0800375 id: uid,
Simon Huntdb9eb072014-11-04 19:12:46 -0800376 class: 'radio'
377 })
378 .text(txt);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800379
Simon Hunt9462e8c2014-11-14 17:28:09 -0800380 btn.id = bid;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800381 btnG.buttonDef[uid] = btn;
382
Simon Huntdb9eb072014-11-04 19:12:46 -0800383 if (i === 0) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800384 button.classed('active', true);
Simon Hunt9462e8c2014-11-14 17:28:09 -0800385 btnG.selected = bid;
Simon Huntdb9eb072014-11-04 19:12:46 -0800386 }
387 });
388
389 btnG.selectAll('span')
390 .on('click', function (d) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800391 var button = d3.select(this),
392 uid = button.attr('id'),
393 btn = btnG.buttonDef[uid],
394 act = button.classed('active');
Simon Huntdb9eb072014-11-04 19:12:46 -0800395
396 if (!act) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800397 btnG.selectAll('span').classed('active', false);
398 button.classed('active', true);
Simon Hunt9462e8c2014-11-14 17:28:09 -0800399 btnG.selected = btn.id;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800400 if (isF(btn.cb)) {
401 btn.cb(view.token(), btn);
402 }
Simon Huntdb9eb072014-11-04 19:12:46 -0800403 }
404 });
405
406 view.radioButtons = btnG;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800407
408 api.selected = function () {
409 return btnG.selected;
410 }
Simon Huntdb9eb072014-11-04 19:12:46 -0800411 }
412
413 // attach the buttons to the masthead
414 $mastRadio.node().appendChild(btnG.node());
Simon Hunt9462e8c2014-11-14 17:28:09 -0800415 // return an api for interacting with the button set
416 return api;
Simon Huntdb9eb072014-11-04 19:12:46 -0800417 }
418
Thomas Vachuska65368e32014-11-08 16:10:20 -0800419 function setupGlobalKeys() {
Simon Hunta1a00c22014-12-04 16:10:40 -0800420 $.extend(keyHandler, {
421 globalKeys: {
422 backSlash: [quickHelp, 'Show / hide Quick Help'],
423 slash: [quickHelp, 'Show / hide Quick Help'],
424 esc: [escapeKey, 'Dismiss dialog or cancel selections'],
425 T: [toggleTheme, "Toggle theme"]
426 },
427 globalFormat: ['backSlash', 'slash', 'esc', 'T'],
428
429 // Masked keys are global key handlers that always return true.
430 // That is, the view will never see the event for that key.
431 maskedKeys: {
432 slash: true,
433 backSlash: true,
434 T: true
435 }
436 });
Thomas Vachuska65368e32014-11-08 16:10:20 -0800437 }
438
Simon Hunt5cef9062014-11-24 15:24:35 -0800439 function quickHelp(view, key, code, ev) {
440 libApi.quickHelp.show(keyHandler);
Simon Hunt988c6fc2014-11-20 17:43:03 -0800441 return true;
442 }
443
Thomas Vachuska65368e32014-11-08 16:10:20 -0800444 function escapeKey(view, key, code, ev) {
445 if (alerts.open) {
446 closeAlerts();
447 return true;
448 }
Simon Hunt5cef9062014-11-24 15:24:35 -0800449 if (libApi.quickHelp.hide()) {
450 return true;
451 }
Thomas Vachuska65368e32014-11-08 16:10:20 -0800452 return false;
453 }
454
455 function toggleTheme(view, key, code, ev) {
456 var body = d3.select('body');
457 current.theme = (current.theme === 'light') ? 'dark' : 'light';
458 body.classed('light dark', false);
459 body.classed(current.theme, true);
Simon Hunt8f40cce2014-11-23 15:57:30 -0800460 theme(view);
Thomas Vachuska65368e32014-11-08 16:10:20 -0800461 return true;
462 }
463
Simon Hunt87514342014-11-24 16:41:27 -0800464 function setGestureNotes(g) {
465 keyHandler.viewGestures = isA(g) || [];
466 }
467
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800468 function setKeyBindings(keyArg) {
Thomas Vachuska65368e32014-11-08 16:10:20 -0800469 var viewKeys,
470 masked = [];
471
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800472 if ($.isFunction(keyArg)) {
473 // set general key handler callback
Thomas Vachuska65368e32014-11-08 16:10:20 -0800474 keyHandler.viewFn = keyArg;
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800475 } else {
476 // set specific key filter map
Thomas Vachuska65368e32014-11-08 16:10:20 -0800477 viewKeys = d3.map(keyArg).keys();
478 viewKeys.forEach(function (key) {
479 if (keyHandler.maskedKeys[key]) {
480 masked.push(' Key "' + key + '" is reserved');
481 }
482 });
483
484 if (masked.length) {
485 doAlert('WARNING...\n\nsetKeys():\n' + masked.join('\n'));
486 }
487 keyHandler.viewKeys = keyArg;
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800488 }
489 }
490
Thomas Vachuska65368e32014-11-08 16:10:20 -0800491 function keyIn() {
492 var event = d3.event,
493 keyCode = event.keyCode,
494 key = whatKey(keyCode),
Simon Hunta1162d82014-12-03 14:34:43 -0800495 kh = keyHandler,
496 gk = kh.globalKeys[key],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800497 gcb = isF(gk) || (isA(gk) && isF(gk[0])),
Simon Hunta1162d82014-12-03 14:34:43 -0800498 vk = kh.viewKeys[key],
499 vcb = isF(vk) || (isA(vk) && isF(vk[0])) || isF(kh.viewFn),
500 token = current.view.token();
Thomas Vachuska65368e32014-11-08 16:10:20 -0800501
502 // global callback?
Simon Hunta1162d82014-12-03 14:34:43 -0800503 if (gcb && gcb(token, key, keyCode, event)) {
Thomas Vachuska65368e32014-11-08 16:10:20 -0800504 // if the event was 'handled', we are done
505 return;
506 }
507 // otherwise, let the view callback have a shot
508 if (vcb) {
Simon Hunta1162d82014-12-03 14:34:43 -0800509 vcb(token, key, keyCode, event);
Thomas Vachuska65368e32014-11-08 16:10:20 -0800510 }
511 }
Simon Hunt1a9eff92014-11-07 11:06:34 -0800512
513 function createAlerts() {
Simon Hunt61d04042014-11-11 17:27:16 -0800514 $alerts.style('display', 'block');
515 $alerts.append('span')
Simon Hunt1a9eff92014-11-07 11:06:34 -0800516 .attr('class', 'close')
517 .text('X')
518 .on('click', closeAlerts);
Simon Hunt61d04042014-11-11 17:27:16 -0800519 $alerts.append('pre');
520 $alerts.append('p').attr('class', 'footnote')
Thomas Vachuska65368e32014-11-08 16:10:20 -0800521 .text('Press ESCAPE to close');
Simon Hunt1a9eff92014-11-07 11:06:34 -0800522 alerts.open = true;
523 alerts.count = 0;
524 }
525
526 function closeAlerts() {
Simon Hunt61d04042014-11-11 17:27:16 -0800527 $alerts.style('display', 'none')
Thomas Vachuska65368e32014-11-08 16:10:20 -0800528 .html('');
Simon Hunt1a9eff92014-11-07 11:06:34 -0800529 alerts.open = false;
530 }
531
532 function addAlert(msg) {
533 var lines,
534 oldContent;
535
536 if (alerts.count) {
Simon Hunt61d04042014-11-11 17:27:16 -0800537 oldContent = $alerts.select('pre').html();
Simon Hunt1a9eff92014-11-07 11:06:34 -0800538 }
539
540 lines = msg.split('\n');
541 lines[0] += ' '; // spacing so we don't crowd 'X'
542 lines = lines.join('\n');
543
544 if (oldContent) {
545 lines += '\n----\n' + oldContent;
546 }
547
Simon Hunt61d04042014-11-11 17:27:16 -0800548 $alerts.select('pre').html(lines);
Simon Hunt1a9eff92014-11-07 11:06:34 -0800549 alerts.count++;
550 }
551
552 function doAlert(msg) {
553 if (!alerts.open) {
554 createAlerts();
555 }
556 addAlert(msg);
557 }
558
Simon Hunt25248912014-11-04 11:25:48 -0800559 function resize(e) {
560 d3.selectAll('.onosView').call(setViewDimensions);
561 // allow current view to react to resize event...
Simon Hunt195cb382014-11-03 17:50:51 -0800562 if (current.view) {
Simon Hunt56d51852014-11-09 13:03:35 -0800563 current.view.resize(current.ctx, current.flags);
Simon Hunt195cb382014-11-03 17:50:51 -0800564 }
565 }
566
Simon Hunt8f40cce2014-11-23 15:57:30 -0800567 function theme() {
568 // allow current view to react to theme event...
569 if (current.view) {
570 current.view.theme(current.ctx, current.flags);
571 }
572 }
573
Simon Hunt195cb382014-11-03 17:50:51 -0800574 // ..........................................................
575 // View class
576 // Captures state information about a view.
577
578 // Constructor
579 // vid : view id
580 // nid : id of associated nav-item (optional)
Simon Hunta2994cc2014-12-02 14:19:15 -0800581 // cb : callbacks (init, reset, load, unload, resize, theme, error)
Simon Hunt195cb382014-11-03 17:50:51 -0800582 function View(vid) {
583 var av = 'addView(): ',
584 args = Array.prototype.slice.call(arguments),
585 nid,
Simon Hunt25248912014-11-04 11:25:48 -0800586 cb;
Simon Hunt195cb382014-11-03 17:50:51 -0800587
588 args.shift(); // first arg is always vid
589 if (typeof args[0] === 'string') { // nid specified
590 nid = args.shift();
591 }
592 cb = args.shift();
Simon Hunt195cb382014-11-03 17:50:51 -0800593
594 this.vid = vid;
595
596 if (validateViewArgs(vid)) {
597 this.nid = nid; // explicit navitem id (can be null)
598 this.cb = $.isPlainObject(cb) ? cb : {}; // callbacks
Simon Huntdb9eb072014-11-04 19:12:46 -0800599 this.$div = null; // view not yet added to DOM
600 this.radioButtons = null; // no radio buttons yet
601 this.ok = true; // valid view
Simon Hunt195cb382014-11-03 17:50:51 -0800602 }
Simon Hunt195cb382014-11-03 17:50:51 -0800603 }
604
605 function validateViewArgs(vid) {
Simon Hunt25248912014-11-04 11:25:48 -0800606 var av = "ui.addView(...): ",
607 ok = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800608 if (typeof vid !== 'string' || !vid) {
609 doError(av + 'vid required');
610 } else if (views[vid]) {
611 doError(av + 'View ID "' + vid + '" already exists');
612 } else {
613 ok = true;
614 }
615 return ok;
616 }
617
618 var viewInstanceMethods = {
Simon Hunt25248912014-11-04 11:25:48 -0800619 token: function () {
Simon Hunt195cb382014-11-03 17:50:51 -0800620 return {
Simon Hunt25248912014-11-04 11:25:48 -0800621 // attributes
Simon Hunt195cb382014-11-03 17:50:51 -0800622 vid: this.vid,
623 nid: this.nid,
Simon Hunt25248912014-11-04 11:25:48 -0800624 $div: this.$div,
625
626 // functions
627 width: this.width,
Simon Huntdb9eb072014-11-04 19:12:46 -0800628 height: this.height,
Simon Hunt142d0032014-11-04 20:13:09 -0800629 uid: this.uid,
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800630 setRadio: this.setRadio,
Simon Huntc7ee0662014-11-05 16:44:37 -0800631 setKeys: this.setKeys,
Simon Hunt87514342014-11-24 16:41:27 -0800632 setGestures: this.setGestures,
Simon Hunt1a9eff92014-11-07 11:06:34 -0800633 dataLoadError: this.dataLoadError,
Simon Hunt625dc402014-11-18 10:57:18 -0800634 alert: this.alert,
Simon Hunta3dd9572014-11-20 15:22:41 -0800635 flash: this.flash,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800636 getTheme: this.getTheme
Simon Hunt195cb382014-11-03 17:50:51 -0800637 }
Simon Hunt25248912014-11-04 11:25:48 -0800638 },
639
Simon Huntf67722a2014-11-10 09:32:06 -0800640 // == Life-cycle functions
641 // TODO: factor common code out of life-cycle
Simon Hunta2994cc2014-12-02 14:19:15 -0800642 init: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800643 var c = ctx || '',
Simon Hunta2994cc2014-12-02 14:19:15 -0800644 fn = isF(this.cb.init);
645 traceFn('View.init', this.vid + ', ' + c);
Simon Hunt25248912014-11-04 11:25:48 -0800646 if (fn) {
Simon Hunta2994cc2014-12-02 14:19:15 -0800647 trace('INIT cb for ' + this.vid);
Simon Hunt56d51852014-11-09 13:03:35 -0800648 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800649 }
650 },
651
Simon Huntf67722a2014-11-10 09:32:06 -0800652 reset: function (ctx, flags) {
653 var c = ctx || '',
654 fn = isF(this.cb.reset);
Simon Hunt25248912014-11-04 11:25:48 -0800655 traceFn('View.reset', this.vid);
656 if (fn) {
657 trace('RESET cb for ' + this.vid);
Simon Huntf67722a2014-11-10 09:32:06 -0800658 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800659 } else if (this.cb.reset === true) {
660 // boolean true signifies "clear view"
661 trace(' [true] cleaing view...');
662 viewApi.empty();
663 }
664 },
665
Simon Hunt56d51852014-11-09 13:03:35 -0800666 load: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800667 var c = ctx || '',
668 fn = isF(this.cb.load);
669 traceFn('View.load', this.vid + ', ' + c);
670 this.$div.classed('currentView', true);
Simon Hunt25248912014-11-04 11:25:48 -0800671 if (fn) {
672 trace('LOAD cb for ' + this.vid);
Simon Hunt56d51852014-11-09 13:03:35 -0800673 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800674 }
675 },
676
Simon Huntf67722a2014-11-10 09:32:06 -0800677 unload: function (ctx, flags) {
678 var c = ctx | '',
679 fn = isF(this.cb.unload);
Simon Hunt25248912014-11-04 11:25:48 -0800680 traceFn('View.unload', this.vid);
681 this.$div.classed('currentView', false);
Simon Hunt25248912014-11-04 11:25:48 -0800682 if (fn) {
683 trace('UNLOAD cb for ' + this.vid);
Simon Huntf67722a2014-11-10 09:32:06 -0800684 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800685 }
686 },
687
Simon Hunt56d51852014-11-09 13:03:35 -0800688 resize: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800689 var c = ctx || '',
690 fn = isF(this.cb.resize),
691 w = this.width(),
692 h = this.height();
693 traceFn('View.resize', this.vid + '/' + c +
694 ' [' + w + 'x' + h + ']');
695 if (fn) {
696 trace('RESIZE cb for ' + this.vid);
Simon Hunt56d51852014-11-09 13:03:35 -0800697 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800698 }
699 },
700
Simon Hunt8f40cce2014-11-23 15:57:30 -0800701 theme: function (ctx, flags) {
702 var c = ctx | '',
703 fn = isF(this.cb.theme);
704 traceFn('View.theme', this.vid);
705 if (fn) {
706 trace('THEME cb for ' + this.vid);
707 fn(this.token(), c, flags);
708 }
709 },
710
Simon Huntf67722a2014-11-10 09:32:06 -0800711 error: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800712 var c = ctx || '',
713 fn = isF(this.cb.error);
714 traceFn('View.error', this.vid + ', ' + c);
715 if (fn) {
716 trace('ERROR cb for ' + this.vid);
Simon Huntf67722a2014-11-10 09:32:06 -0800717 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800718 }
719 },
720
Simon Huntf67722a2014-11-10 09:32:06 -0800721 // == Token API functions
Simon Hunt25248912014-11-04 11:25:48 -0800722 width: function () {
723 return $(this.$div.node()).width();
724 },
725
726 height: function () {
727 return $(this.$div.node()).height();
Simon Huntdb9eb072014-11-04 19:12:46 -0800728 },
Simon Hunt25248912014-11-04 11:25:48 -0800729
Simon Hunt934c3ce2014-11-05 11:45:07 -0800730 setRadio: function (btnSet) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800731 return setRadioButtons(this.vid, btnSet);
Simon Hunt142d0032014-11-04 20:13:09 -0800732 },
733
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800734 setKeys: function (keyArg) {
735 setKeyBindings(keyArg);
736 },
737
Simon Hunt87514342014-11-24 16:41:27 -0800738 setGestures: function (g) {
739 setGestureNotes(g);
740 },
741
Simon Hunt8f40cce2014-11-23 15:57:30 -0800742 getTheme: function () {
Simon Hunt625dc402014-11-18 10:57:18 -0800743 return current.theme;
744 },
745
Simon Hunt142d0032014-11-04 20:13:09 -0800746 uid: function (id) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800747 return makeUid(this, id);
Simon Huntc7ee0662014-11-05 16:44:37 -0800748 },
749
Simon Huntbb282f52014-11-10 11:08:19 -0800750 // TODO : add exportApi and importApi methods
Simon Hunt1a9eff92014-11-07 11:06:34 -0800751 // TODO : implement custom dialogs
752
753 // Consider enhancing alert mechanism to handle multiples
754 // as individually closable.
755 alert: function (msg) {
756 doAlert(msg);
757 },
Simon Huntc7ee0662014-11-05 16:44:37 -0800758
Simon Hunta3dd9572014-11-20 15:22:41 -0800759 flash: function (msg) {
760 libApi.feedback.flash(msg);
761 },
762
Simon Huntc7ee0662014-11-05 16:44:37 -0800763 dataLoadError: function (err, url) {
764 var msg = 'Data Load Error\n\n' +
765 err.status + ' -- ' + err.statusText + '\n\n' +
766 'relative-url: "' + url + '"\n\n' +
767 'complete-url: "' + err.responseURL + '"';
Simon Hunt1a9eff92014-11-07 11:06:34 -0800768 this.alert(msg);
Simon Huntdb9eb072014-11-04 19:12:46 -0800769 }
Simon Hunt25248912014-11-04 11:25:48 -0800770
771 // TODO: consider schedule, clearTimer, etc.
Simon Hunt195cb382014-11-03 17:50:51 -0800772 };
773
774 // attach instance methods to the view prototype
775 $.extend(View.prototype, viewInstanceMethods);
776
777 // ..........................................................
Simon Hunt25248912014-11-04 11:25:48 -0800778 // UI API
Simon Hunt195cb382014-11-03 17:50:51 -0800779
Simon Hunta5e89142014-11-14 07:00:33 -0800780 var fpConfig = {
781 TR: {
782 side: 'right'
Simon Hunta5e89142014-11-14 07:00:33 -0800783 },
784 TL: {
785 side: 'left'
786 }
787 };
788
Simon Hunt25248912014-11-04 11:25:48 -0800789 uiApi = {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800790 addLib: function (libName, api) {
791 // TODO: validation of args
792 libApi[libName] = api;
793 },
794
Simon Hunt61d04042014-11-11 17:27:16 -0800795 // TODO: implement floating panel as a class
796 // TODO: parameterize position (currently hard-coded to TopRight)
797 /*
798 * Creates div in floating panels block, with the given id.
799 * Returns panel token used to interact with the panel
800 */
801 addFloatingPanel: function (id, position) {
802 var pos = position || 'TR',
Simon Hunta5e89142014-11-14 07:00:33 -0800803 cfg = fpConfig[pos],
Simon Hunt61d04042014-11-11 17:27:16 -0800804 el,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800805 fp,
806 on = false;
Simon Hunt61d04042014-11-11 17:27:16 -0800807
808 if (fpanels[id]) {
809 buildError('Float panel with id "' + id + '" already exists.');
810 return null;
811 }
812
813 el = $floatPanels.append('div')
814 .attr('id', id)
Simon Hunta5e89142014-11-14 07:00:33 -0800815 .attr('class', 'fpanel')
816 .style('opacity', 0);
817
818 // has to be called after el is set.
819 el.style(cfg.side, pxHide());
820
821 function pxShow() {
822 return '20px';
823 }
824 function pxHide() {
825 return (-20 - widthVal()) + 'px';
826 }
Simon Hunt7b403bc2014-11-22 19:01:00 -0800827 function noPx(what) {
828 return el.style(what).replace(/px$/, '');
829 }
Simon Hunta5e89142014-11-14 07:00:33 -0800830 function widthVal() {
Simon Hunt7b403bc2014-11-22 19:01:00 -0800831 return noPx('width');
832 }
833 function heightVal() {
834 return noPx('height');
Simon Hunta5e89142014-11-14 07:00:33 -0800835 }
Simon Hunt61d04042014-11-11 17:27:16 -0800836
Simon Hunt06811b72014-11-25 18:54:48 -0800837 function noop() {}
838
Simon Hunt61d04042014-11-11 17:27:16 -0800839 fp = {
840 id: id,
841 el: el,
842 pos: pos,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800843 isVisible: function () {
844 return on;
845 },
Simon Hunta5e89142014-11-14 07:00:33 -0800846
Simon Hunt06811b72014-11-25 18:54:48 -0800847 show: function (cb) {
848 var endCb = isF(cb) || noop;
Thomas Vachuska47635c62014-11-22 01:21:36 -0800849 on = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800850 el.transition().duration(750)
Simon Hunt06811b72014-11-25 18:54:48 -0800851 .each('end', endCb)
Simon Hunta5e89142014-11-14 07:00:33 -0800852 .style(cfg.side, pxShow())
Simon Hunt61d04042014-11-11 17:27:16 -0800853 .style('opacity', 1);
854 },
Simon Hunt06811b72014-11-25 18:54:48 -0800855 hide: function (cb) {
856 var endCb = isF(cb) || noop;
Thomas Vachuska47635c62014-11-22 01:21:36 -0800857 on = false;
Simon Hunt61d04042014-11-11 17:27:16 -0800858 el.transition().duration(750)
Simon Hunt06811b72014-11-25 18:54:48 -0800859 .each('end', endCb)
Simon Hunta5e89142014-11-14 07:00:33 -0800860 .style(cfg.side, pxHide())
Simon Hunt61d04042014-11-11 17:27:16 -0800861 .style('opacity', 0);
862 },
863 empty: function () {
864 return el.html('');
865 },
866 append: function (what) {
867 return el.append(what);
Simon Hunta5e89142014-11-14 07:00:33 -0800868 },
869 width: function (w) {
870 if (w === undefined) {
871 return widthVal();
872 }
Simon Huntb82f6902014-11-22 11:53:15 -0800873 el.style('width', w + 'px');
Simon Hunt7b403bc2014-11-22 19:01:00 -0800874 },
875 height: function (h) {
876 if (h === undefined) {
877 return heightVal();
878 }
879 el.style('height', h + 'px');
Simon Hunt61d04042014-11-11 17:27:16 -0800880 }
881 };
882 fpanels[id] = fp;
883 return fp;
884 },
885
Simon Hunt1a9eff92014-11-07 11:06:34 -0800886 // TODO: it remains to be seen whether we keep this style of docs
Simon Hunt25248912014-11-04 11:25:48 -0800887 /** @api ui addView( vid, nid, cb )
888 * Adds a view to the UI.
889 * <p>
890 * Views are loaded/unloaded into the view content pane at
891 * appropriate times, by the navigation framework. This method
892 * adds a view to the UI and returns a token object representing
893 * the view. A view's token is always passed as the first
894 * argument to each of the view's life-cycle callback functions.
895 * <p>
896 * Note that if the view is directly referenced by a nav-item,
897 * or in a group of views with one of those views referenced by
898 * a nav-item, then the <i>nid</i> argument can be omitted as
899 * the framework can infer it.
900 * <p>
901 * <i>cb</i> is a plain object containing callback functions:
Simon Hunta2994cc2014-12-02 14:19:15 -0800902 * "init", "reset", "load", "unload", "resize", "theme", "error".
Simon Hunt25248912014-11-04 11:25:48 -0800903 * <pre>
904 * function myLoad(view, ctx) { ... }
905 * ...
906 * // short form...
907 * onos.ui.addView('viewId', {
908 * load: myLoad
909 * });
910 * </pre>
911 *
912 * @param vid (string) [*] view ID (a unique DOM element id)
913 * @param nid (string) nav-item ID (a unique DOM element id)
914 * @param cb (object) [*] callbacks object
915 * @return the view token
916 */
917 addView: function (vid, nid, cb) {
918 traceFn('addView', vid);
919 var view = new View(vid, nid, cb),
Simon Hunt195cb382014-11-03 17:50:51 -0800920 token;
921 if (view.ok) {
922 views[vid] = view;
923 token = view.token();
924 } else {
925 token = { vid: view.vid, bad: true };
926 }
927 return token;
928 }
929 };
930
Simon Hunt25248912014-11-04 11:25:48 -0800931 // ..........................................................
932 // View API
933
Simon Huntbb282f52014-11-10 11:08:19 -0800934 // TODO: deprecated
Simon Hunt25248912014-11-04 11:25:48 -0800935 viewApi = {
936 /** @api view empty( )
937 * Empties the current view.
938 * <p>
939 * More specifically, removes all DOM elements from the
940 * current view's display div.
941 */
942 empty: function () {
943 if (!current.view) {
944 return;
945 }
946 current.view.$div.html('');
947 }
948 };
949
950 // ..........................................................
951 // Nav API
952 navApi = {
953
954 };
955
956 // ..........................................................
Simon Hunt1a9eff92014-11-07 11:06:34 -0800957 // Library API
958 libApi = {
959
960 };
961
962 // ..........................................................
Simon Hunt25248912014-11-04 11:25:48 -0800963 // Exported API
964
Simon Hunt195cb382014-11-03 17:50:51 -0800965 // function to be called from index.html to build the ONOS UI
966 function buildOnosUi() {
967 tsB = new Date().getTime();
968 tsI = tsB - tsI; // initialization duration
969
970 console.log('ONOS UI initialized in ' + tsI + 'ms');
971
972 if (built) {
973 throwError("ONOS UI already built!");
974 }
975 built = true;
976
Simon Huntdb9eb072014-11-04 19:12:46 -0800977 $mastRadio = d3.select('#mastRadio');
Simon Hunt195cb382014-11-03 17:50:51 -0800978
979 $(window).on('hashchange', hash);
Simon Hunt25248912014-11-04 11:25:48 -0800980 $(window).on('resize', resize);
Simon Hunt195cb382014-11-03 17:50:51 -0800981
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800982 d3.select('body').on('keydown', keyIn);
Thomas Vachuska65368e32014-11-08 16:10:20 -0800983 setupGlobalKeys();
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800984
Simon Hunt195cb382014-11-03 17:50:51 -0800985 // Invoke hashchange callback to navigate to content
986 // indicated by the window location hash.
987 hash();
988
989 // If there were any build errors, report them
990 reportBuildErrors();
991 }
992
Simon Hunt195cb382014-11-03 17:50:51 -0800993 // export the api and build-UI function
994 return {
Simon Hunt25248912014-11-04 11:25:48 -0800995 ui: uiApi,
Simon Hunt1a9eff92014-11-07 11:06:34 -0800996 lib: libApi,
997 //view: viewApi,
Simon Hunt25248912014-11-04 11:25:48 -0800998 nav: navApi,
Simon Huntbb282f52014-11-10 11:08:19 -0800999 buildUi: buildOnosUi,
1000 exported: exported
Simon Hunt195cb382014-11-03 17:50:51 -08001001 };
1002 };
1003
Simon Huntdb9eb072014-11-04 19:12:46 -08001004}(jQuery));