blob: cc595a97b849b84af57a872eecdad156391cbd47 [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';
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800100 default:
101 if ((code >= 48 && code <= 57) ||
102 (code >= 65 && code <= 90)) {
103 return String.fromCharCode(code);
104 } else if (code >= 112 && code <= 123) {
105 return 'F' + (code - 111);
106 }
107 return '.';
108 }
109 }
110
111
Simon Hunt195cb382014-11-03 17:50:51 -0800112 // ..........................................................
113 // Internal functions
114
115 // throw an error
116 function throwError(msg) {
117 // separate function, as we might add tracing here too, later
118 throw new Error(msg);
119 }
120
121 function doError(msg) {
Simon Hunt25248912014-11-04 11:25:48 -0800122 console.error(msg);
Simon Hunt56d51852014-11-09 13:03:35 -0800123 doAlert(msg);
Simon Hunt25248912014-11-04 11:25:48 -0800124 }
125
126 function trace(msg) {
127 if (settings.trace) {
128 console.log(msg);
129 }
130 }
131
132 function traceFn(fn, params) {
133 if (settings.trace) {
134 console.log('*FN* ' + fn + '(...): ' + params);
135 }
Simon Hunt195cb382014-11-03 17:50:51 -0800136 }
137
138 // hash navigation
139 function hash() {
140 var hash = window.location.hash,
141 redo = false,
142 view,
143 t;
144
Simon Hunt25248912014-11-04 11:25:48 -0800145 traceFn('hash', hash);
146
Simon Hunt195cb382014-11-03 17:50:51 -0800147 if (!hash) {
Simon Hunt142d0032014-11-04 20:13:09 -0800148 hash = settings.startVid;
Simon Hunt195cb382014-11-03 17:50:51 -0800149 redo = true;
150 }
151
152 t = parseHash(hash);
153 if (!t || !t.vid) {
Simon Hunt56d51852014-11-09 13:03:35 -0800154 doError('Unable to parse target hash: "' + hash + '"');
Simon Hunt195cb382014-11-03 17:50:51 -0800155 }
156
157 view = views[t.vid];
158 if (!view) {
159 doError('No view defined with id: ' + t.vid);
160 }
161
162 if (redo) {
163 window.location.hash = makeHash(t);
164 // the above will result in a hashchange event, invoking
165 // this function again
166 } else {
167 // hash was not modified... navigate to where we need to be
168 navigate(hash, view, t);
169 }
Simon Hunt195cb382014-11-03 17:50:51 -0800170 }
171
172 function parseHash(s) {
173 // extract navigation coordinates from the supplied string
Simon Hunt56d51852014-11-09 13:03:35 -0800174 // "vid,ctx?flag1,flag2" --> { vid:vid, ctx:ctx, flags:{...} }
Simon Hunt25248912014-11-04 11:25:48 -0800175 traceFn('parseHash', s);
Simon Hunt195cb382014-11-03 17:50:51 -0800176
Simon Hunt56d51852014-11-09 13:03:35 -0800177 // look for use of flags, first
178 var vidctx,
179 vid,
180 ctx,
181 flags,
182 flagMap,
183 m;
184
185 // RE that includes flags ('?flag1,flag2')
186 m = /^[#]{0,1}(.+)\?(.+)$/.exec(s);
Simon Hunt195cb382014-11-03 17:50:51 -0800187 if (m) {
Simon Hunt56d51852014-11-09 13:03:35 -0800188 vidctx = m[1];
189 flags = m[2];
190 flagMap = {};
191 } else {
192 // no flags
193 m = /^[#]{0,1}((.+)(,.+)*)$/.exec(s);
194 if (m) {
195 vidctx = m[1];
196 } else {
197 // bad hash
198 return null;
199 }
Simon Hunt195cb382014-11-03 17:50:51 -0800200 }
201
Simon Hunt56d51852014-11-09 13:03:35 -0800202 vidctx = vidctx.split(',');
203 vid = vidctx[0];
204 ctx = vidctx[1];
205 if (flags) {
206 flags.split(',').forEach(function (f) {
207 flagMap[f.trim()] = true;
208 });
209 }
210
211 return {
212 vid: vid.trim(),
213 ctx: ctx ? ctx.trim() : '',
214 flags: flagMap
215 };
216
Simon Hunt195cb382014-11-03 17:50:51 -0800217 }
218
Simon Hunt56d51852014-11-09 13:03:35 -0800219 function makeHash(t, ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800220 traceFn('makeHash');
Simon Hunt56d51852014-11-09 13:03:35 -0800221 // make a hash string from the given navigation coordinates,
222 // and optional flags map.
Simon Hunt195cb382014-11-03 17:50:51 -0800223 // if t is not an object, then it is a vid
224 var h = t,
Simon Hunt56d51852014-11-09 13:03:35 -0800225 c = ctx || '',
226 f = $.isPlainObject(flags) ? flags : null;
Simon Hunt195cb382014-11-03 17:50:51 -0800227
228 if ($.isPlainObject(t)) {
229 h = t.vid;
230 c = t.ctx || '';
Simon Hunt56d51852014-11-09 13:03:35 -0800231 f = t.flags || null;
Simon Hunt195cb382014-11-03 17:50:51 -0800232 }
233
234 if (c) {
235 h += ',' + c;
236 }
Simon Hunt56d51852014-11-09 13:03:35 -0800237 if (f) {
238 h += '?' + d3.map(f).keys().join(',');
239 }
Simon Hunt25248912014-11-04 11:25:48 -0800240 trace('hash = "' + h + '"');
Simon Hunt195cb382014-11-03 17:50:51 -0800241 return h;
242 }
243
244 function navigate(hash, view, t) {
Simon Hunt25248912014-11-04 11:25:48 -0800245 traceFn('navigate', view.vid);
Simon Hunt195cb382014-11-03 17:50:51 -0800246 // closePanes() // flyouts etc.
Simon Hunt25248912014-11-04 11:25:48 -0800247 // updateNav() // accordion / selected nav item etc.
Simon Hunt195cb382014-11-03 17:50:51 -0800248 createView(view);
249 setView(view, hash, t);
250 }
251
Simon Hunt61d04042014-11-11 17:27:16 -0800252 function buildError(msg) {
253 buildErrors.push(msg);
254 }
255
Simon Hunt195cb382014-11-03 17:50:51 -0800256 function reportBuildErrors() {
Simon Hunt25248912014-11-04 11:25:48 -0800257 traceFn('reportBuildErrors');
Simon Hunt61d04042014-11-11 17:27:16 -0800258 var nerr = buildErrors.length,
259 errmsg;
260 if (!nerr) {
261 console.log('(no build errors)');
262 } else {
263 errmsg = 'Build errors: ' + nerr + ' found...\n\n' +
264 buildErrors.join('\n');
265 doAlert(errmsg);
266 console.error(errmsg);
267 }
Simon Hunt195cb382014-11-03 17:50:51 -0800268 }
269
Simon Hunt25248912014-11-04 11:25:48 -0800270 // returns the reference if it is a function, null otherwise
271 function isF(f) {
272 return $.isFunction(f) ? f : null;
273 }
274
Simon Hunt988c6fc2014-11-20 17:43:03 -0800275 // returns the reference if it is an array, null otherwise
276 function isA(a) {
277 return $.isArray(a) ? a : null;
278 }
279
Simon Hunt195cb382014-11-03 17:50:51 -0800280 // ..........................................................
281 // View life-cycle functions
282
Simon Hunt25248912014-11-04 11:25:48 -0800283 function setViewDimensions(sel) {
284 var w = window.innerWidth,
285 h = window.innerHeight - mastHeight;
286 sel.each(function () {
287 $(this)
288 .css('width', w + 'px')
289 .css('height', h + 'px')
290 });
291 }
292
Simon Hunt195cb382014-11-03 17:50:51 -0800293 function createView(view) {
294 var $d;
Simon Hunt25248912014-11-04 11:25:48 -0800295
Simon Hunt195cb382014-11-03 17:50:51 -0800296 // lazy initialization of the view
297 if (view && !view.$div) {
Simon Hunt25248912014-11-04 11:25:48 -0800298 trace('creating view for ' + view.vid);
Simon Hunt195cb382014-11-03 17:50:51 -0800299 $d = $view.append('div')
300 .attr({
Simon Hunt25248912014-11-04 11:25:48 -0800301 id: view.vid,
302 class: 'onosView'
Simon Hunt195cb382014-11-03 17:50:51 -0800303 });
Simon Hunt25248912014-11-04 11:25:48 -0800304 setViewDimensions($d);
305 view.$div = $d; // cache a reference to the D3 selection
Simon Hunt195cb382014-11-03 17:50:51 -0800306 }
307 }
308
309 function setView(view, hash, t) {
Simon Hunt25248912014-11-04 11:25:48 -0800310 traceFn('setView', view.vid);
Simon Hunt195cb382014-11-03 17:50:51 -0800311 // set the specified view as current, while invoking the
312 // appropriate life-cycle callbacks
313
Simon Hunt56d51852014-11-09 13:03:35 -0800314 // first, we'll start by closing the alerts pane, if open
315 closeAlerts();
316
Simon Hunt195cb382014-11-03 17:50:51 -0800317 // if there is a current view, and it is not the same as
318 // the incoming view, then unload it...
Simon Hunt25248912014-11-04 11:25:48 -0800319 if (current.view && (current.view.vid !== view.vid)) {
Simon Hunt195cb382014-11-03 17:50:51 -0800320 current.view.unload();
Simon Huntdb9eb072014-11-04 19:12:46 -0800321
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800322 // detach radio buttons, key handlers, etc.
323 $('#mastRadio').children().detach();
Thomas Vachuska65368e32014-11-08 16:10:20 -0800324 keyHandler.viewKeys = {};
325 keyHandler.viewFn = null;
Simon Hunt195cb382014-11-03 17:50:51 -0800326 }
327
328 // cache new view and context
329 current.view = view;
330 current.ctx = t.ctx || '';
Simon Hunt56d51852014-11-09 13:03:35 -0800331 current.flags = t.flags || {};
Simon Hunt195cb382014-11-03 17:50:51 -0800332
Simon Hunt195cb382014-11-03 17:50:51 -0800333 // preload is called only once, after the view is in the DOM
334 if (!view.preloaded) {
Simon Hunt56d51852014-11-09 13:03:35 -0800335 view.preload(current.ctx, current.flags);
Simon Hunt25248912014-11-04 11:25:48 -0800336 view.preloaded = true;
Simon Hunt195cb382014-11-03 17:50:51 -0800337 }
338
339 // clear the view of stale data
340 view.reset();
341
342 // load the view
Simon Hunt56d51852014-11-09 13:03:35 -0800343 view.load(current.ctx, current.flags);
Simon Hunt195cb382014-11-03 17:50:51 -0800344 }
345
Simon Huntdb9eb072014-11-04 19:12:46 -0800346 // generate 'unique' id by prefixing view id
Simon Hunt934c3ce2014-11-05 11:45:07 -0800347 function makeUid(view, id) {
Simon Huntdb9eb072014-11-04 19:12:46 -0800348 return view.vid + '-' + id;
349 }
350
351 // restore id by removing view id prefix
Simon Hunt934c3ce2014-11-05 11:45:07 -0800352 function unmakeUid(view, uid) {
Simon Huntdb9eb072014-11-04 19:12:46 -0800353 var re = new RegExp('^' + view.vid + '-');
354 return uid.replace(re, '');
355 }
356
Simon Hunt934c3ce2014-11-05 11:45:07 -0800357 function setRadioButtons(vid, btnSet) {
Simon Huntdb9eb072014-11-04 19:12:46 -0800358 var view = views[vid],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800359 btnG,
360 api = {};
Simon Huntdb9eb072014-11-04 19:12:46 -0800361
362 // lazily create the buttons...
363 if (!(btnG = view.radioButtons)) {
364 btnG = d3.select(document.createElement('div'));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800365 btnG.buttonDef = {};
Simon Huntdb9eb072014-11-04 19:12:46 -0800366
367 btnSet.forEach(function (btn, i) {
368 var bid = btn.id || 'b' + i,
369 txt = btn.text || 'Button #' + i,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800370 uid = makeUid(view, bid),
371 button = btnG.append('span')
Simon Huntdb9eb072014-11-04 19:12:46 -0800372 .attr({
Simon Hunt934c3ce2014-11-05 11:45:07 -0800373 id: uid,
Simon Huntdb9eb072014-11-04 19:12:46 -0800374 class: 'radio'
375 })
376 .text(txt);
Simon Hunt934c3ce2014-11-05 11:45:07 -0800377
Simon Hunt9462e8c2014-11-14 17:28:09 -0800378 btn.id = bid;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800379 btnG.buttonDef[uid] = btn;
380
Simon Huntdb9eb072014-11-04 19:12:46 -0800381 if (i === 0) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800382 button.classed('active', true);
Simon Hunt9462e8c2014-11-14 17:28:09 -0800383 btnG.selected = bid;
Simon Huntdb9eb072014-11-04 19:12:46 -0800384 }
385 });
386
387 btnG.selectAll('span')
388 .on('click', function (d) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800389 var button = d3.select(this),
390 uid = button.attr('id'),
391 btn = btnG.buttonDef[uid],
392 act = button.classed('active');
Simon Huntdb9eb072014-11-04 19:12:46 -0800393
394 if (!act) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800395 btnG.selectAll('span').classed('active', false);
396 button.classed('active', true);
Simon Hunt9462e8c2014-11-14 17:28:09 -0800397 btnG.selected = btn.id;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800398 if (isF(btn.cb)) {
399 btn.cb(view.token(), btn);
400 }
Simon Huntdb9eb072014-11-04 19:12:46 -0800401 }
402 });
403
404 view.radioButtons = btnG;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800405
406 api.selected = function () {
407 return btnG.selected;
408 }
Simon Huntdb9eb072014-11-04 19:12:46 -0800409 }
410
411 // attach the buttons to the masthead
412 $mastRadio.node().appendChild(btnG.node());
Simon Hunt9462e8c2014-11-14 17:28:09 -0800413 // return an api for interacting with the button set
414 return api;
Simon Huntdb9eb072014-11-04 19:12:46 -0800415 }
416
Thomas Vachuska65368e32014-11-08 16:10:20 -0800417 function setupGlobalKeys() {
418 keyHandler.globalKeys = {
Simon Hunt5cef9062014-11-24 15:24:35 -0800419 slash: [quickHelp, 'Show / hide Quick Help'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800420 esc: [escapeKey, 'Dismiss dialog or cancel selections'],
421 T: [toggleTheme, "Toggle theme"]
Thomas Vachuska65368e32014-11-08 16:10:20 -0800422 };
423 // Masked keys are global key handlers that always return true.
424 // That is, the view will never see the event for that key.
425 keyHandler.maskedKeys = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800426 slash: true,
Thomas Vachuska65368e32014-11-08 16:10:20 -0800427 T: true
428 };
429 }
430
Simon Hunt5cef9062014-11-24 15:24:35 -0800431 function quickHelp(view, key, code, ev) {
432 libApi.quickHelp.show(keyHandler);
Simon Hunt988c6fc2014-11-20 17:43:03 -0800433 return true;
434 }
435
Thomas Vachuska65368e32014-11-08 16:10:20 -0800436 function escapeKey(view, key, code, ev) {
437 if (alerts.open) {
438 closeAlerts();
439 return true;
440 }
Simon Hunt5cef9062014-11-24 15:24:35 -0800441 if (libApi.quickHelp.hide()) {
442 return true;
443 }
Thomas Vachuska65368e32014-11-08 16:10:20 -0800444 return false;
445 }
446
447 function toggleTheme(view, key, code, ev) {
448 var body = d3.select('body');
449 current.theme = (current.theme === 'light') ? 'dark' : 'light';
450 body.classed('light dark', false);
451 body.classed(current.theme, true);
Simon Hunt8f40cce2014-11-23 15:57:30 -0800452 theme(view);
Thomas Vachuska65368e32014-11-08 16:10:20 -0800453 return true;
454 }
455
Simon Hunt87514342014-11-24 16:41:27 -0800456 function setGestureNotes(g) {
457 keyHandler.viewGestures = isA(g) || [];
458 }
459
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800460 function setKeyBindings(keyArg) {
Thomas Vachuska65368e32014-11-08 16:10:20 -0800461 var viewKeys,
462 masked = [];
463
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800464 if ($.isFunction(keyArg)) {
465 // set general key handler callback
Thomas Vachuska65368e32014-11-08 16:10:20 -0800466 keyHandler.viewFn = keyArg;
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800467 } else {
468 // set specific key filter map
Thomas Vachuska65368e32014-11-08 16:10:20 -0800469 viewKeys = d3.map(keyArg).keys();
470 viewKeys.forEach(function (key) {
471 if (keyHandler.maskedKeys[key]) {
472 masked.push(' Key "' + key + '" is reserved');
473 }
474 });
475
476 if (masked.length) {
477 doAlert('WARNING...\n\nsetKeys():\n' + masked.join('\n'));
478 }
479 keyHandler.viewKeys = keyArg;
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800480 }
481 }
482
Thomas Vachuska65368e32014-11-08 16:10:20 -0800483 function keyIn() {
484 var event = d3.event,
485 keyCode = event.keyCode,
486 key = whatKey(keyCode),
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800487 gk = keyHandler.globalKeys[key],
488 gcb = isF(gk) || (isA(gk) && isF(gk[0])),
Simon Hunt988c6fc2014-11-20 17:43:03 -0800489 vk = keyHandler.viewKeys[key],
490 vcb = isF(vk) || (isA(vk) && isF(vk[0])) || isF(keyHandler.viewFn);
Thomas Vachuska65368e32014-11-08 16:10:20 -0800491
492 // global callback?
493 if (gcb && gcb(current.view.token(), key, keyCode, event)) {
494 // if the event was 'handled', we are done
495 return;
496 }
497 // otherwise, let the view callback have a shot
498 if (vcb) {
499 vcb(current.view.token(), key, keyCode, event);
500 }
501 }
Simon Hunt1a9eff92014-11-07 11:06:34 -0800502
503 function createAlerts() {
Simon Hunt61d04042014-11-11 17:27:16 -0800504 $alerts.style('display', 'block');
505 $alerts.append('span')
Simon Hunt1a9eff92014-11-07 11:06:34 -0800506 .attr('class', 'close')
507 .text('X')
508 .on('click', closeAlerts);
Simon Hunt61d04042014-11-11 17:27:16 -0800509 $alerts.append('pre');
510 $alerts.append('p').attr('class', 'footnote')
Thomas Vachuska65368e32014-11-08 16:10:20 -0800511 .text('Press ESCAPE to close');
Simon Hunt1a9eff92014-11-07 11:06:34 -0800512 alerts.open = true;
513 alerts.count = 0;
514 }
515
516 function closeAlerts() {
Simon Hunt61d04042014-11-11 17:27:16 -0800517 $alerts.style('display', 'none')
Thomas Vachuska65368e32014-11-08 16:10:20 -0800518 .html('');
Simon Hunt1a9eff92014-11-07 11:06:34 -0800519 alerts.open = false;
520 }
521
522 function addAlert(msg) {
523 var lines,
524 oldContent;
525
526 if (alerts.count) {
Simon Hunt61d04042014-11-11 17:27:16 -0800527 oldContent = $alerts.select('pre').html();
Simon Hunt1a9eff92014-11-07 11:06:34 -0800528 }
529
530 lines = msg.split('\n');
531 lines[0] += ' '; // spacing so we don't crowd 'X'
532 lines = lines.join('\n');
533
534 if (oldContent) {
535 lines += '\n----\n' + oldContent;
536 }
537
Simon Hunt61d04042014-11-11 17:27:16 -0800538 $alerts.select('pre').html(lines);
Simon Hunt1a9eff92014-11-07 11:06:34 -0800539 alerts.count++;
540 }
541
542 function doAlert(msg) {
543 if (!alerts.open) {
544 createAlerts();
545 }
546 addAlert(msg);
547 }
548
Simon Hunt25248912014-11-04 11:25:48 -0800549 function resize(e) {
550 d3.selectAll('.onosView').call(setViewDimensions);
551 // allow current view to react to resize event...
Simon Hunt195cb382014-11-03 17:50:51 -0800552 if (current.view) {
Simon Hunt56d51852014-11-09 13:03:35 -0800553 current.view.resize(current.ctx, current.flags);
Simon Hunt195cb382014-11-03 17:50:51 -0800554 }
555 }
556
Simon Hunt8f40cce2014-11-23 15:57:30 -0800557 function theme() {
558 // allow current view to react to theme event...
559 if (current.view) {
560 current.view.theme(current.ctx, current.flags);
561 }
562 }
563
Simon Hunt195cb382014-11-03 17:50:51 -0800564 // ..........................................................
565 // View class
566 // Captures state information about a view.
567
568 // Constructor
569 // vid : view id
570 // nid : id of associated nav-item (optional)
Simon Hunt25248912014-11-04 11:25:48 -0800571 // cb : callbacks (preload, reset, load, unload, resize, error)
Simon Hunt195cb382014-11-03 17:50:51 -0800572 function View(vid) {
573 var av = 'addView(): ',
574 args = Array.prototype.slice.call(arguments),
575 nid,
Simon Hunt25248912014-11-04 11:25:48 -0800576 cb;
Simon Hunt195cb382014-11-03 17:50:51 -0800577
578 args.shift(); // first arg is always vid
579 if (typeof args[0] === 'string') { // nid specified
580 nid = args.shift();
581 }
582 cb = args.shift();
Simon Hunt195cb382014-11-03 17:50:51 -0800583
584 this.vid = vid;
585
586 if (validateViewArgs(vid)) {
587 this.nid = nid; // explicit navitem id (can be null)
588 this.cb = $.isPlainObject(cb) ? cb : {}; // callbacks
Simon Huntdb9eb072014-11-04 19:12:46 -0800589 this.$div = null; // view not yet added to DOM
590 this.radioButtons = null; // no radio buttons yet
591 this.ok = true; // valid view
Simon Hunt195cb382014-11-03 17:50:51 -0800592 }
Simon Hunt195cb382014-11-03 17:50:51 -0800593 }
594
595 function validateViewArgs(vid) {
Simon Hunt25248912014-11-04 11:25:48 -0800596 var av = "ui.addView(...): ",
597 ok = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800598 if (typeof vid !== 'string' || !vid) {
599 doError(av + 'vid required');
600 } else if (views[vid]) {
601 doError(av + 'View ID "' + vid + '" already exists');
602 } else {
603 ok = true;
604 }
605 return ok;
606 }
607
608 var viewInstanceMethods = {
Simon Hunt25248912014-11-04 11:25:48 -0800609 token: function () {
Simon Hunt195cb382014-11-03 17:50:51 -0800610 return {
Simon Hunt25248912014-11-04 11:25:48 -0800611 // attributes
Simon Hunt195cb382014-11-03 17:50:51 -0800612 vid: this.vid,
613 nid: this.nid,
Simon Hunt25248912014-11-04 11:25:48 -0800614 $div: this.$div,
615
616 // functions
617 width: this.width,
Simon Huntdb9eb072014-11-04 19:12:46 -0800618 height: this.height,
Simon Hunt142d0032014-11-04 20:13:09 -0800619 uid: this.uid,
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800620 setRadio: this.setRadio,
Simon Huntc7ee0662014-11-05 16:44:37 -0800621 setKeys: this.setKeys,
Simon Hunt87514342014-11-24 16:41:27 -0800622 setGestures: this.setGestures,
Simon Hunt1a9eff92014-11-07 11:06:34 -0800623 dataLoadError: this.dataLoadError,
Simon Hunt625dc402014-11-18 10:57:18 -0800624 alert: this.alert,
Simon Hunta3dd9572014-11-20 15:22:41 -0800625 flash: this.flash,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800626 getTheme: this.getTheme
Simon Hunt195cb382014-11-03 17:50:51 -0800627 }
Simon Hunt25248912014-11-04 11:25:48 -0800628 },
629
Simon Huntf67722a2014-11-10 09:32:06 -0800630 // == Life-cycle functions
631 // TODO: factor common code out of life-cycle
Simon Hunt56d51852014-11-09 13:03:35 -0800632 preload: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800633 var c = ctx || '',
634 fn = isF(this.cb.preload);
635 traceFn('View.preload', this.vid + ', ' + c);
636 if (fn) {
637 trace('PRELOAD cb for ' + this.vid);
Simon Hunt56d51852014-11-09 13:03:35 -0800638 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800639 }
640 },
641
Simon Huntf67722a2014-11-10 09:32:06 -0800642 reset: function (ctx, flags) {
643 var c = ctx || '',
644 fn = isF(this.cb.reset);
Simon Hunt25248912014-11-04 11:25:48 -0800645 traceFn('View.reset', this.vid);
646 if (fn) {
647 trace('RESET cb for ' + this.vid);
Simon Huntf67722a2014-11-10 09:32:06 -0800648 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800649 } else if (this.cb.reset === true) {
650 // boolean true signifies "clear view"
651 trace(' [true] cleaing view...');
652 viewApi.empty();
653 }
654 },
655
Simon Hunt56d51852014-11-09 13:03:35 -0800656 load: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800657 var c = ctx || '',
658 fn = isF(this.cb.load);
659 traceFn('View.load', this.vid + ', ' + c);
660 this.$div.classed('currentView', true);
Simon Hunt25248912014-11-04 11:25:48 -0800661 if (fn) {
662 trace('LOAD cb for ' + this.vid);
Simon Hunt56d51852014-11-09 13:03:35 -0800663 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800664 }
665 },
666
Simon Huntf67722a2014-11-10 09:32:06 -0800667 unload: function (ctx, flags) {
668 var c = ctx | '',
669 fn = isF(this.cb.unload);
Simon Hunt25248912014-11-04 11:25:48 -0800670 traceFn('View.unload', this.vid);
671 this.$div.classed('currentView', false);
Simon Hunt25248912014-11-04 11:25:48 -0800672 if (fn) {
673 trace('UNLOAD cb for ' + this.vid);
Simon Huntf67722a2014-11-10 09:32:06 -0800674 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800675 }
676 },
677
Simon Hunt56d51852014-11-09 13:03:35 -0800678 resize: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800679 var c = ctx || '',
680 fn = isF(this.cb.resize),
681 w = this.width(),
682 h = this.height();
683 traceFn('View.resize', this.vid + '/' + c +
684 ' [' + w + 'x' + h + ']');
685 if (fn) {
686 trace('RESIZE cb for ' + this.vid);
Simon Hunt56d51852014-11-09 13:03:35 -0800687 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800688 }
689 },
690
Simon Hunt8f40cce2014-11-23 15:57:30 -0800691 theme: function (ctx, flags) {
692 var c = ctx | '',
693 fn = isF(this.cb.theme);
694 traceFn('View.theme', this.vid);
695 if (fn) {
696 trace('THEME cb for ' + this.vid);
697 fn(this.token(), c, flags);
698 }
699 },
700
Simon Huntf67722a2014-11-10 09:32:06 -0800701 error: function (ctx, flags) {
Simon Hunt25248912014-11-04 11:25:48 -0800702 var c = ctx || '',
703 fn = isF(this.cb.error);
704 traceFn('View.error', this.vid + ', ' + c);
705 if (fn) {
706 trace('ERROR cb for ' + this.vid);
Simon Huntf67722a2014-11-10 09:32:06 -0800707 fn(this.token(), c, flags);
Simon Hunt25248912014-11-04 11:25:48 -0800708 }
709 },
710
Simon Huntf67722a2014-11-10 09:32:06 -0800711 // == Token API functions
Simon Hunt25248912014-11-04 11:25:48 -0800712 width: function () {
713 return $(this.$div.node()).width();
714 },
715
716 height: function () {
717 return $(this.$div.node()).height();
Simon Huntdb9eb072014-11-04 19:12:46 -0800718 },
Simon Hunt25248912014-11-04 11:25:48 -0800719
Simon Hunt934c3ce2014-11-05 11:45:07 -0800720 setRadio: function (btnSet) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800721 return setRadioButtons(this.vid, btnSet);
Simon Hunt142d0032014-11-04 20:13:09 -0800722 },
723
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800724 setKeys: function (keyArg) {
725 setKeyBindings(keyArg);
726 },
727
Simon Hunt87514342014-11-24 16:41:27 -0800728 setGestures: function (g) {
729 setGestureNotes(g);
730 },
731
Simon Hunt8f40cce2014-11-23 15:57:30 -0800732 getTheme: function () {
Simon Hunt625dc402014-11-18 10:57:18 -0800733 return current.theme;
734 },
735
Simon Hunt142d0032014-11-04 20:13:09 -0800736 uid: function (id) {
Simon Hunt934c3ce2014-11-05 11:45:07 -0800737 return makeUid(this, id);
Simon Huntc7ee0662014-11-05 16:44:37 -0800738 },
739
Simon Huntbb282f52014-11-10 11:08:19 -0800740 // TODO : add exportApi and importApi methods
Simon Hunt1a9eff92014-11-07 11:06:34 -0800741 // TODO : implement custom dialogs
742
743 // Consider enhancing alert mechanism to handle multiples
744 // as individually closable.
745 alert: function (msg) {
746 doAlert(msg);
747 },
Simon Huntc7ee0662014-11-05 16:44:37 -0800748
Simon Hunta3dd9572014-11-20 15:22:41 -0800749 flash: function (msg) {
750 libApi.feedback.flash(msg);
751 },
752
Simon Huntc7ee0662014-11-05 16:44:37 -0800753 dataLoadError: function (err, url) {
754 var msg = 'Data Load Error\n\n' +
755 err.status + ' -- ' + err.statusText + '\n\n' +
756 'relative-url: "' + url + '"\n\n' +
757 'complete-url: "' + err.responseURL + '"';
Simon Hunt1a9eff92014-11-07 11:06:34 -0800758 this.alert(msg);
Simon Huntdb9eb072014-11-04 19:12:46 -0800759 }
Simon Hunt25248912014-11-04 11:25:48 -0800760
761 // TODO: consider schedule, clearTimer, etc.
Simon Hunt195cb382014-11-03 17:50:51 -0800762 };
763
764 // attach instance methods to the view prototype
765 $.extend(View.prototype, viewInstanceMethods);
766
767 // ..........................................................
Simon Hunt25248912014-11-04 11:25:48 -0800768 // UI API
Simon Hunt195cb382014-11-03 17:50:51 -0800769
Simon Hunta5e89142014-11-14 07:00:33 -0800770 var fpConfig = {
771 TR: {
772 side: 'right'
773
774 },
775 TL: {
776 side: 'left'
777 }
778 };
779
Simon Hunt25248912014-11-04 11:25:48 -0800780 uiApi = {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800781 addLib: function (libName, api) {
782 // TODO: validation of args
783 libApi[libName] = api;
784 },
785
Simon Hunt61d04042014-11-11 17:27:16 -0800786 // TODO: implement floating panel as a class
787 // TODO: parameterize position (currently hard-coded to TopRight)
788 /*
789 * Creates div in floating panels block, with the given id.
790 * Returns panel token used to interact with the panel
791 */
792 addFloatingPanel: function (id, position) {
793 var pos = position || 'TR',
Simon Hunta5e89142014-11-14 07:00:33 -0800794 cfg = fpConfig[pos],
Simon Hunt61d04042014-11-11 17:27:16 -0800795 el,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800796 fp,
797 on = false;
Simon Hunt61d04042014-11-11 17:27:16 -0800798
799 if (fpanels[id]) {
800 buildError('Float panel with id "' + id + '" already exists.');
801 return null;
802 }
803
804 el = $floatPanels.append('div')
805 .attr('id', id)
Simon Hunta5e89142014-11-14 07:00:33 -0800806 .attr('class', 'fpanel')
807 .style('opacity', 0);
808
809 // has to be called after el is set.
810 el.style(cfg.side, pxHide());
811
812 function pxShow() {
813 return '20px';
814 }
815 function pxHide() {
816 return (-20 - widthVal()) + 'px';
817 }
Simon Hunt7b403bc2014-11-22 19:01:00 -0800818 function noPx(what) {
819 return el.style(what).replace(/px$/, '');
820 }
Simon Hunta5e89142014-11-14 07:00:33 -0800821 function widthVal() {
Simon Hunt7b403bc2014-11-22 19:01:00 -0800822 return noPx('width');
823 }
824 function heightVal() {
825 return noPx('height');
Simon Hunta5e89142014-11-14 07:00:33 -0800826 }
Simon Hunt61d04042014-11-11 17:27:16 -0800827
828 fp = {
829 id: id,
830 el: el,
831 pos: pos,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800832 isVisible: function () {
833 return on;
834 },
Simon Hunta5e89142014-11-14 07:00:33 -0800835
Simon Hunt61d04042014-11-11 17:27:16 -0800836 show: function () {
837 console.log('show pane: ' + id);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800838 on = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800839 el.transition().duration(750)
Simon Hunta5e89142014-11-14 07:00:33 -0800840 .style(cfg.side, pxShow())
Simon Hunt61d04042014-11-11 17:27:16 -0800841 .style('opacity', 1);
842 },
843 hide: function () {
844 console.log('hide pane: ' + id);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800845 on = false;
Simon Hunt61d04042014-11-11 17:27:16 -0800846 el.transition().duration(750)
Simon Hunta5e89142014-11-14 07:00:33 -0800847 .style(cfg.side, pxHide())
Simon Hunt61d04042014-11-11 17:27:16 -0800848 .style('opacity', 0);
849 },
850 empty: function () {
851 return el.html('');
852 },
853 append: function (what) {
854 return el.append(what);
Simon Hunta5e89142014-11-14 07:00:33 -0800855 },
856 width: function (w) {
857 if (w === undefined) {
858 return widthVal();
859 }
Simon Huntb82f6902014-11-22 11:53:15 -0800860 el.style('width', w + 'px');
Simon Hunt7b403bc2014-11-22 19:01:00 -0800861 },
862 height: function (h) {
863 if (h === undefined) {
864 return heightVal();
865 }
866 el.style('height', h + 'px');
Simon Hunt61d04042014-11-11 17:27:16 -0800867 }
868 };
869 fpanels[id] = fp;
870 return fp;
871 },
872
Simon Hunt1a9eff92014-11-07 11:06:34 -0800873 // TODO: it remains to be seen whether we keep this style of docs
Simon Hunt25248912014-11-04 11:25:48 -0800874 /** @api ui addView( vid, nid, cb )
875 * Adds a view to the UI.
876 * <p>
877 * Views are loaded/unloaded into the view content pane at
878 * appropriate times, by the navigation framework. This method
879 * adds a view to the UI and returns a token object representing
880 * the view. A view's token is always passed as the first
881 * argument to each of the view's life-cycle callback functions.
882 * <p>
883 * Note that if the view is directly referenced by a nav-item,
884 * or in a group of views with one of those views referenced by
885 * a nav-item, then the <i>nid</i> argument can be omitted as
886 * the framework can infer it.
887 * <p>
888 * <i>cb</i> is a plain object containing callback functions:
889 * "preload", "reset", "load", "unload", "resize", "error".
890 * <pre>
891 * function myLoad(view, ctx) { ... }
892 * ...
893 * // short form...
894 * onos.ui.addView('viewId', {
895 * load: myLoad
896 * });
897 * </pre>
898 *
899 * @param vid (string) [*] view ID (a unique DOM element id)
900 * @param nid (string) nav-item ID (a unique DOM element id)
901 * @param cb (object) [*] callbacks object
902 * @return the view token
903 */
904 addView: function (vid, nid, cb) {
905 traceFn('addView', vid);
906 var view = new View(vid, nid, cb),
Simon Hunt195cb382014-11-03 17:50:51 -0800907 token;
908 if (view.ok) {
909 views[vid] = view;
910 token = view.token();
911 } else {
912 token = { vid: view.vid, bad: true };
913 }
914 return token;
915 }
916 };
917
Simon Hunt25248912014-11-04 11:25:48 -0800918 // ..........................................................
919 // View API
920
Simon Huntbb282f52014-11-10 11:08:19 -0800921 // TODO: deprecated
Simon Hunt25248912014-11-04 11:25:48 -0800922 viewApi = {
923 /** @api view empty( )
924 * Empties the current view.
925 * <p>
926 * More specifically, removes all DOM elements from the
927 * current view's display div.
928 */
929 empty: function () {
930 if (!current.view) {
931 return;
932 }
933 current.view.$div.html('');
934 }
935 };
936
937 // ..........................................................
938 // Nav API
939 navApi = {
940
941 };
942
943 // ..........................................................
Simon Hunt1a9eff92014-11-07 11:06:34 -0800944 // Library API
945 libApi = {
946
947 };
948
949 // ..........................................................
Simon Hunt25248912014-11-04 11:25:48 -0800950 // Exported API
951
Simon Hunt195cb382014-11-03 17:50:51 -0800952 // function to be called from index.html to build the ONOS UI
953 function buildOnosUi() {
954 tsB = new Date().getTime();
955 tsI = tsB - tsI; // initialization duration
956
957 console.log('ONOS UI initialized in ' + tsI + 'ms');
958
959 if (built) {
960 throwError("ONOS UI already built!");
961 }
962 built = true;
963
Simon Huntdb9eb072014-11-04 19:12:46 -0800964 $mastRadio = d3.select('#mastRadio');
Simon Hunt195cb382014-11-03 17:50:51 -0800965
966 $(window).on('hashchange', hash);
Simon Hunt25248912014-11-04 11:25:48 -0800967 $(window).on('resize', resize);
Simon Hunt195cb382014-11-03 17:50:51 -0800968
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800969 d3.select('body').on('keydown', keyIn);
Thomas Vachuska65368e32014-11-08 16:10:20 -0800970 setupGlobalKeys();
Simon Hunt0df1b1d2014-11-04 22:58:29 -0800971
Simon Hunt195cb382014-11-03 17:50:51 -0800972 // Invoke hashchange callback to navigate to content
973 // indicated by the window location hash.
974 hash();
975
976 // If there were any build errors, report them
977 reportBuildErrors();
978 }
979
Simon Hunt195cb382014-11-03 17:50:51 -0800980 // export the api and build-UI function
981 return {
Simon Hunt25248912014-11-04 11:25:48 -0800982 ui: uiApi,
Simon Hunt1a9eff92014-11-07 11:06:34 -0800983 lib: libApi,
984 //view: viewApi,
Simon Hunt25248912014-11-04 11:25:48 -0800985 nav: navApi,
Simon Huntbb282f52014-11-10 11:08:19 -0800986 buildUi: buildOnosUi,
987 exported: exported
Simon Hunt195cb382014-11-03 17:50:51 -0800988 };
989 };
990
Simon Huntdb9eb072014-11-04 19:12:46 -0800991}(jQuery));