Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 27 | mastHeight = 36, // see mast2.css |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 28 | defaultVid = 'sample'; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 29 | |
| 30 | |
| 31 | // attach our main function to the jQuery object |
| 32 | $.onos = function (options) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 33 | var uiApi, |
| 34 | viewApi, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 35 | navApi, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 36 | libApi, |
| 37 | exported = {}; |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 38 | |
| 39 | var defaultOptions = { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 40 | trace: false, |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 41 | theme: 'light', |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 42 | startVid: defaultVid |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | // compute runtime settings |
| 46 | var settings = $.extend({}, defaultOptions, options); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 47 | |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 48 | // set the selected theme |
| 49 | d3.select('body').classed(settings.theme, true); |
| 50 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 51 | // internal state |
| 52 | var views = {}, |
| 53 | current = { |
| 54 | view: null, |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 55 | ctx: '', |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 56 | flags: {}, |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 57 | theme: settings.theme |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 58 | }, |
| 59 | built = false, |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 60 | errorCount = 0, |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 61 | keyHandler = { |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 62 | globalKeys: {}, |
| 63 | maskedKeys: {}, |
| 64 | viewKeys: {}, |
| 65 | viewFn: null |
| 66 | }, |
| 67 | alerts = { |
| 68 | open: false, |
| 69 | count: 0 |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 70 | }; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 71 | |
| 72 | // DOM elements etc. |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 73 | var $view, |
| 74 | $mastRadio; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 75 | |
| 76 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 77 | function whatKey(code) { |
| 78 | switch (code) { |
| 79 | case 13: return 'enter'; |
| 80 | case 16: return 'shift'; |
| 81 | case 17: return 'ctrl'; |
| 82 | case 18: return 'alt'; |
| 83 | case 27: return 'esc'; |
| 84 | case 32: return 'space'; |
| 85 | case 37: return 'leftArrow'; |
| 86 | case 38: return 'upArrow'; |
| 87 | case 39: return 'rightArrow'; |
| 88 | case 40: return 'downArrow'; |
| 89 | case 91: return 'cmdLeft'; |
| 90 | case 93: return 'cmdRight'; |
| 91 | default: |
| 92 | if ((code >= 48 && code <= 57) || |
| 93 | (code >= 65 && code <= 90)) { |
| 94 | return String.fromCharCode(code); |
| 95 | } else if (code >= 112 && code <= 123) { |
| 96 | return 'F' + (code - 111); |
| 97 | } |
| 98 | return '.'; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 103 | // .......................................................... |
| 104 | // Internal functions |
| 105 | |
| 106 | // throw an error |
| 107 | function throwError(msg) { |
| 108 | // separate function, as we might add tracing here too, later |
| 109 | throw new Error(msg); |
| 110 | } |
| 111 | |
| 112 | function doError(msg) { |
| 113 | errorCount++; |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 114 | console.error(msg); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 115 | doAlert(msg); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | function trace(msg) { |
| 119 | if (settings.trace) { |
| 120 | console.log(msg); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | function traceFn(fn, params) { |
| 125 | if (settings.trace) { |
| 126 | console.log('*FN* ' + fn + '(...): ' + params); |
| 127 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // hash navigation |
| 131 | function hash() { |
| 132 | var hash = window.location.hash, |
| 133 | redo = false, |
| 134 | view, |
| 135 | t; |
| 136 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 137 | traceFn('hash', hash); |
| 138 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 139 | if (!hash) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 140 | hash = settings.startVid; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 141 | redo = true; |
| 142 | } |
| 143 | |
| 144 | t = parseHash(hash); |
| 145 | if (!t || !t.vid) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 146 | doError('Unable to parse target hash: "' + hash + '"'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | view = views[t.vid]; |
| 150 | if (!view) { |
| 151 | doError('No view defined with id: ' + t.vid); |
| 152 | } |
| 153 | |
| 154 | if (redo) { |
| 155 | window.location.hash = makeHash(t); |
| 156 | // the above will result in a hashchange event, invoking |
| 157 | // this function again |
| 158 | } else { |
| 159 | // hash was not modified... navigate to where we need to be |
| 160 | navigate(hash, view, t); |
| 161 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | function parseHash(s) { |
| 165 | // extract navigation coordinates from the supplied string |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 166 | // "vid,ctx?flag1,flag2" --> { vid:vid, ctx:ctx, flags:{...} } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 167 | traceFn('parseHash', s); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 168 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 169 | // look for use of flags, first |
| 170 | var vidctx, |
| 171 | vid, |
| 172 | ctx, |
| 173 | flags, |
| 174 | flagMap, |
| 175 | m; |
| 176 | |
| 177 | // RE that includes flags ('?flag1,flag2') |
| 178 | m = /^[#]{0,1}(.+)\?(.+)$/.exec(s); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 179 | if (m) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 180 | vidctx = m[1]; |
| 181 | flags = m[2]; |
| 182 | flagMap = {}; |
| 183 | } else { |
| 184 | // no flags |
| 185 | m = /^[#]{0,1}((.+)(,.+)*)$/.exec(s); |
| 186 | if (m) { |
| 187 | vidctx = m[1]; |
| 188 | } else { |
| 189 | // bad hash |
| 190 | return null; |
| 191 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 194 | vidctx = vidctx.split(','); |
| 195 | vid = vidctx[0]; |
| 196 | ctx = vidctx[1]; |
| 197 | if (flags) { |
| 198 | flags.split(',').forEach(function (f) { |
| 199 | flagMap[f.trim()] = true; |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | return { |
| 204 | vid: vid.trim(), |
| 205 | ctx: ctx ? ctx.trim() : '', |
| 206 | flags: flagMap |
| 207 | }; |
| 208 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 211 | function makeHash(t, ctx, flags) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 212 | traceFn('makeHash'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 213 | // make a hash string from the given navigation coordinates, |
| 214 | // and optional flags map. |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 215 | // if t is not an object, then it is a vid |
| 216 | var h = t, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 217 | c = ctx || '', |
| 218 | f = $.isPlainObject(flags) ? flags : null; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 219 | |
| 220 | if ($.isPlainObject(t)) { |
| 221 | h = t.vid; |
| 222 | c = t.ctx || ''; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 223 | f = t.flags || null; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (c) { |
| 227 | h += ',' + c; |
| 228 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 229 | if (f) { |
| 230 | h += '?' + d3.map(f).keys().join(','); |
| 231 | } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 232 | trace('hash = "' + h + '"'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 233 | return h; |
| 234 | } |
| 235 | |
| 236 | function navigate(hash, view, t) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 237 | traceFn('navigate', view.vid); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 238 | // closePanes() // flyouts etc. |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 239 | // updateNav() // accordion / selected nav item etc. |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 240 | createView(view); |
| 241 | setView(view, hash, t); |
| 242 | } |
| 243 | |
| 244 | function reportBuildErrors() { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 245 | traceFn('reportBuildErrors'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 246 | // TODO: validate registered views / nav-item linkage etc. |
| 247 | console.log('(no build errors)'); |
| 248 | } |
| 249 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 250 | // returns the reference if it is a function, null otherwise |
| 251 | function isF(f) { |
| 252 | return $.isFunction(f) ? f : null; |
| 253 | } |
| 254 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 255 | // .......................................................... |
| 256 | // View life-cycle functions |
| 257 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 258 | function setViewDimensions(sel) { |
| 259 | var w = window.innerWidth, |
| 260 | h = window.innerHeight - mastHeight; |
| 261 | sel.each(function () { |
| 262 | $(this) |
| 263 | .css('width', w + 'px') |
| 264 | .css('height', h + 'px') |
| 265 | }); |
| 266 | } |
| 267 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 268 | function createView(view) { |
| 269 | var $d; |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 270 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 271 | // lazy initialization of the view |
| 272 | if (view && !view.$div) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 273 | trace('creating view for ' + view.vid); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 274 | $d = $view.append('div') |
| 275 | .attr({ |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 276 | id: view.vid, |
| 277 | class: 'onosView' |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 278 | }); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 279 | setViewDimensions($d); |
| 280 | view.$div = $d; // cache a reference to the D3 selection |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | function setView(view, hash, t) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 285 | traceFn('setView', view.vid); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 286 | // set the specified view as current, while invoking the |
| 287 | // appropriate life-cycle callbacks |
| 288 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 289 | // first, we'll start by closing the alerts pane, if open |
| 290 | closeAlerts(); |
| 291 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 292 | // if there is a current view, and it is not the same as |
| 293 | // the incoming view, then unload it... |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 294 | if (current.view && (current.view.vid !== view.vid)) { |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 295 | current.view.unload(); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 296 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 297 | // detach radio buttons, key handlers, etc. |
| 298 | $('#mastRadio').children().detach(); |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 299 | keyHandler.viewKeys = {}; |
| 300 | keyHandler.viewFn = null; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // cache new view and context |
| 304 | current.view = view; |
| 305 | current.ctx = t.ctx || ''; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 306 | current.flags = t.flags || {}; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 307 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 308 | // preload is called only once, after the view is in the DOM |
| 309 | if (!view.preloaded) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 310 | view.preload(current.ctx, current.flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 311 | view.preloaded = true; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // clear the view of stale data |
| 315 | view.reset(); |
| 316 | |
| 317 | // load the view |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 318 | view.load(current.ctx, current.flags); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 321 | // generate 'unique' id by prefixing view id |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 322 | function makeUid(view, id) { |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 323 | return view.vid + '-' + id; |
| 324 | } |
| 325 | |
| 326 | // restore id by removing view id prefix |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 327 | function unmakeUid(view, uid) { |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 328 | var re = new RegExp('^' + view.vid + '-'); |
| 329 | return uid.replace(re, ''); |
| 330 | } |
| 331 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 332 | function setRadioButtons(vid, btnSet) { |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 333 | var view = views[vid], |
| 334 | btnG; |
| 335 | |
| 336 | // lazily create the buttons... |
| 337 | if (!(btnG = view.radioButtons)) { |
| 338 | btnG = d3.select(document.createElement('div')); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 339 | btnG.buttonDef = {}; |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 340 | |
| 341 | btnSet.forEach(function (btn, i) { |
| 342 | var bid = btn.id || 'b' + i, |
| 343 | txt = btn.text || 'Button #' + i, |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 344 | uid = makeUid(view, bid), |
| 345 | button = btnG.append('span') |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 346 | .attr({ |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 347 | id: uid, |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 348 | class: 'radio' |
| 349 | }) |
| 350 | .text(txt); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 351 | |
| 352 | btnG.buttonDef[uid] = btn; |
| 353 | |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 354 | if (i === 0) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 355 | button.classed('active', true); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 356 | } |
| 357 | }); |
| 358 | |
| 359 | btnG.selectAll('span') |
| 360 | .on('click', function (d) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 361 | var button = d3.select(this), |
| 362 | uid = button.attr('id'), |
| 363 | btn = btnG.buttonDef[uid], |
| 364 | act = button.classed('active'); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 365 | |
| 366 | if (!act) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 367 | btnG.selectAll('span').classed('active', false); |
| 368 | button.classed('active', true); |
| 369 | if (isF(btn.cb)) { |
| 370 | btn.cb(view.token(), btn); |
| 371 | } |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 372 | } |
| 373 | }); |
| 374 | |
| 375 | view.radioButtons = btnG; |
| 376 | } |
| 377 | |
| 378 | // attach the buttons to the masthead |
| 379 | $mastRadio.node().appendChild(btnG.node()); |
| 380 | } |
| 381 | |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 382 | function setupGlobalKeys() { |
| 383 | keyHandler.globalKeys = { |
| 384 | esc: escapeKey, |
| 385 | T: toggleTheme |
| 386 | }; |
| 387 | // Masked keys are global key handlers that always return true. |
| 388 | // That is, the view will never see the event for that key. |
| 389 | keyHandler.maskedKeys = { |
| 390 | T: true |
| 391 | }; |
| 392 | } |
| 393 | |
| 394 | function escapeKey(view, key, code, ev) { |
| 395 | if (alerts.open) { |
| 396 | closeAlerts(); |
| 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | function toggleTheme(view, key, code, ev) { |
| 403 | var body = d3.select('body'); |
| 404 | current.theme = (current.theme === 'light') ? 'dark' : 'light'; |
| 405 | body.classed('light dark', false); |
| 406 | body.classed(current.theme, true); |
| 407 | return true; |
| 408 | } |
| 409 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 410 | function setKeyBindings(keyArg) { |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 411 | var viewKeys, |
| 412 | masked = []; |
| 413 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 414 | if ($.isFunction(keyArg)) { |
| 415 | // set general key handler callback |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 416 | keyHandler.viewFn = keyArg; |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 417 | } else { |
| 418 | // set specific key filter map |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 419 | viewKeys = d3.map(keyArg).keys(); |
| 420 | viewKeys.forEach(function (key) { |
| 421 | if (keyHandler.maskedKeys[key]) { |
| 422 | masked.push(' Key "' + key + '" is reserved'); |
| 423 | } |
| 424 | }); |
| 425 | |
| 426 | if (masked.length) { |
| 427 | doAlert('WARNING...\n\nsetKeys():\n' + masked.join('\n')); |
| 428 | } |
| 429 | keyHandler.viewKeys = keyArg; |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 433 | function keyIn() { |
| 434 | var event = d3.event, |
| 435 | keyCode = event.keyCode, |
| 436 | key = whatKey(keyCode), |
| 437 | gcb = isF(keyHandler.globalKeys[key]), |
| 438 | vcb = isF(keyHandler.viewKeys[key]) || isF(keyHandler.viewFn); |
| 439 | |
| 440 | // global callback? |
| 441 | if (gcb && gcb(current.view.token(), key, keyCode, event)) { |
| 442 | // if the event was 'handled', we are done |
| 443 | return; |
| 444 | } |
| 445 | // otherwise, let the view callback have a shot |
| 446 | if (vcb) { |
| 447 | vcb(current.view.token(), key, keyCode, event); |
| 448 | } |
| 449 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 450 | |
| 451 | function createAlerts() { |
| 452 | var al = d3.select('#alerts') |
| 453 | .style('display', 'block'); |
| 454 | al.append('span') |
| 455 | .attr('class', 'close') |
| 456 | .text('X') |
| 457 | .on('click', closeAlerts); |
| 458 | al.append('pre'); |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 459 | al.append('p').attr('class', 'footnote') |
| 460 | .text('Press ESCAPE to close'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 461 | alerts.open = true; |
| 462 | alerts.count = 0; |
| 463 | } |
| 464 | |
| 465 | function closeAlerts() { |
| 466 | d3.select('#alerts') |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 467 | .style('display', 'none') |
| 468 | .html(''); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 469 | alerts.open = false; |
| 470 | } |
| 471 | |
| 472 | function addAlert(msg) { |
| 473 | var lines, |
| 474 | oldContent; |
| 475 | |
| 476 | if (alerts.count) { |
| 477 | oldContent = d3.select('#alerts pre').html(); |
| 478 | } |
| 479 | |
| 480 | lines = msg.split('\n'); |
| 481 | lines[0] += ' '; // spacing so we don't crowd 'X' |
| 482 | lines = lines.join('\n'); |
| 483 | |
| 484 | if (oldContent) { |
| 485 | lines += '\n----\n' + oldContent; |
| 486 | } |
| 487 | |
| 488 | d3.select('#alerts pre').html(lines); |
| 489 | alerts.count++; |
| 490 | } |
| 491 | |
| 492 | function doAlert(msg) { |
| 493 | if (!alerts.open) { |
| 494 | createAlerts(); |
| 495 | } |
| 496 | addAlert(msg); |
| 497 | } |
| 498 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 499 | function resize(e) { |
| 500 | d3.selectAll('.onosView').call(setViewDimensions); |
| 501 | // allow current view to react to resize event... |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 502 | if (current.view) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 503 | current.view.resize(current.ctx, current.flags); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | // .......................................................... |
| 508 | // View class |
| 509 | // Captures state information about a view. |
| 510 | |
| 511 | // Constructor |
| 512 | // vid : view id |
| 513 | // nid : id of associated nav-item (optional) |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 514 | // cb : callbacks (preload, reset, load, unload, resize, error) |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 515 | function View(vid) { |
| 516 | var av = 'addView(): ', |
| 517 | args = Array.prototype.slice.call(arguments), |
| 518 | nid, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 519 | cb; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 520 | |
| 521 | args.shift(); // first arg is always vid |
| 522 | if (typeof args[0] === 'string') { // nid specified |
| 523 | nid = args.shift(); |
| 524 | } |
| 525 | cb = args.shift(); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 526 | |
| 527 | this.vid = vid; |
| 528 | |
| 529 | if (validateViewArgs(vid)) { |
| 530 | this.nid = nid; // explicit navitem id (can be null) |
| 531 | this.cb = $.isPlainObject(cb) ? cb : {}; // callbacks |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 532 | this.$div = null; // view not yet added to DOM |
| 533 | this.radioButtons = null; // no radio buttons yet |
| 534 | this.ok = true; // valid view |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 535 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | function validateViewArgs(vid) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 539 | var av = "ui.addView(...): ", |
| 540 | ok = false; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 541 | if (typeof vid !== 'string' || !vid) { |
| 542 | doError(av + 'vid required'); |
| 543 | } else if (views[vid]) { |
| 544 | doError(av + 'View ID "' + vid + '" already exists'); |
| 545 | } else { |
| 546 | ok = true; |
| 547 | } |
| 548 | return ok; |
| 549 | } |
| 550 | |
| 551 | var viewInstanceMethods = { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 552 | token: function () { |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 553 | return { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 554 | // attributes |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 555 | vid: this.vid, |
| 556 | nid: this.nid, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 557 | $div: this.$div, |
| 558 | |
| 559 | // functions |
| 560 | width: this.width, |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 561 | height: this.height, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 562 | uid: this.uid, |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 563 | setRadio: this.setRadio, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 564 | setKeys: this.setKeys, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 565 | dataLoadError: this.dataLoadError, |
| 566 | alert: this.alert |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 567 | } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 568 | }, |
| 569 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 570 | // == Life-cycle functions |
| 571 | // TODO: factor common code out of life-cycle |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 572 | preload: function (ctx, flags) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 573 | var c = ctx || '', |
| 574 | fn = isF(this.cb.preload); |
| 575 | traceFn('View.preload', this.vid + ', ' + c); |
| 576 | if (fn) { |
| 577 | trace('PRELOAD cb for ' + this.vid); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 578 | fn(this.token(), c, flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 579 | } |
| 580 | }, |
| 581 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 582 | reset: function (ctx, flags) { |
| 583 | var c = ctx || '', |
| 584 | fn = isF(this.cb.reset); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 585 | traceFn('View.reset', this.vid); |
| 586 | if (fn) { |
| 587 | trace('RESET cb for ' + this.vid); |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 588 | fn(this.token(), c, flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 589 | } else if (this.cb.reset === true) { |
| 590 | // boolean true signifies "clear view" |
| 591 | trace(' [true] cleaing view...'); |
| 592 | viewApi.empty(); |
| 593 | } |
| 594 | }, |
| 595 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 596 | load: function (ctx, flags) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 597 | var c = ctx || '', |
| 598 | fn = isF(this.cb.load); |
| 599 | traceFn('View.load', this.vid + ', ' + c); |
| 600 | this.$div.classed('currentView', true); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 601 | if (fn) { |
| 602 | trace('LOAD cb for ' + this.vid); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 603 | fn(this.token(), c, flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 604 | } |
| 605 | }, |
| 606 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 607 | unload: function (ctx, flags) { |
| 608 | var c = ctx | '', |
| 609 | fn = isF(this.cb.unload); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 610 | traceFn('View.unload', this.vid); |
| 611 | this.$div.classed('currentView', false); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 612 | if (fn) { |
| 613 | trace('UNLOAD cb for ' + this.vid); |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 614 | fn(this.token(), c, flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 615 | } |
| 616 | }, |
| 617 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 618 | resize: function (ctx, flags) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 619 | var c = ctx || '', |
| 620 | fn = isF(this.cb.resize), |
| 621 | w = this.width(), |
| 622 | h = this.height(); |
| 623 | traceFn('View.resize', this.vid + '/' + c + |
| 624 | ' [' + w + 'x' + h + ']'); |
| 625 | if (fn) { |
| 626 | trace('RESIZE cb for ' + this.vid); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 627 | fn(this.token(), c, flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 628 | } |
| 629 | }, |
| 630 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 631 | error: function (ctx, flags) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 632 | var c = ctx || '', |
| 633 | fn = isF(this.cb.error); |
| 634 | traceFn('View.error', this.vid + ', ' + c); |
| 635 | if (fn) { |
| 636 | trace('ERROR cb for ' + this.vid); |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 637 | fn(this.token(), c, flags); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 638 | } |
| 639 | }, |
| 640 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 641 | // == Token API functions |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 642 | width: function () { |
| 643 | return $(this.$div.node()).width(); |
| 644 | }, |
| 645 | |
| 646 | height: function () { |
| 647 | return $(this.$div.node()).height(); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 648 | }, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 649 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 650 | setRadio: function (btnSet) { |
| 651 | setRadioButtons(this.vid, btnSet); |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 652 | }, |
| 653 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 654 | setKeys: function (keyArg) { |
| 655 | setKeyBindings(keyArg); |
| 656 | }, |
| 657 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 658 | uid: function (id) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 659 | return makeUid(this, id); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 660 | }, |
| 661 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 662 | // TODO : add exportApi and importApi methods |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 663 | // TODO : implement custom dialogs |
| 664 | |
| 665 | // Consider enhancing alert mechanism to handle multiples |
| 666 | // as individually closable. |
| 667 | alert: function (msg) { |
| 668 | doAlert(msg); |
| 669 | }, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 670 | |
| 671 | dataLoadError: function (err, url) { |
| 672 | var msg = 'Data Load Error\n\n' + |
| 673 | err.status + ' -- ' + err.statusText + '\n\n' + |
| 674 | 'relative-url: "' + url + '"\n\n' + |
| 675 | 'complete-url: "' + err.responseURL + '"'; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 676 | this.alert(msg); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 677 | } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 678 | |
| 679 | // TODO: consider schedule, clearTimer, etc. |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | // attach instance methods to the view prototype |
| 683 | $.extend(View.prototype, viewInstanceMethods); |
| 684 | |
| 685 | // .......................................................... |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 686 | // UI API |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 687 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 688 | uiApi = { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 689 | addLib: function (libName, api) { |
| 690 | // TODO: validation of args |
| 691 | libApi[libName] = api; |
| 692 | }, |
| 693 | |
| 694 | // TODO: it remains to be seen whether we keep this style of docs |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 695 | /** @api ui addView( vid, nid, cb ) |
| 696 | * Adds a view to the UI. |
| 697 | * <p> |
| 698 | * Views are loaded/unloaded into the view content pane at |
| 699 | * appropriate times, by the navigation framework. This method |
| 700 | * adds a view to the UI and returns a token object representing |
| 701 | * the view. A view's token is always passed as the first |
| 702 | * argument to each of the view's life-cycle callback functions. |
| 703 | * <p> |
| 704 | * Note that if the view is directly referenced by a nav-item, |
| 705 | * or in a group of views with one of those views referenced by |
| 706 | * a nav-item, then the <i>nid</i> argument can be omitted as |
| 707 | * the framework can infer it. |
| 708 | * <p> |
| 709 | * <i>cb</i> is a plain object containing callback functions: |
| 710 | * "preload", "reset", "load", "unload", "resize", "error". |
| 711 | * <pre> |
| 712 | * function myLoad(view, ctx) { ... } |
| 713 | * ... |
| 714 | * // short form... |
| 715 | * onos.ui.addView('viewId', { |
| 716 | * load: myLoad |
| 717 | * }); |
| 718 | * </pre> |
| 719 | * |
| 720 | * @param vid (string) [*] view ID (a unique DOM element id) |
| 721 | * @param nid (string) nav-item ID (a unique DOM element id) |
| 722 | * @param cb (object) [*] callbacks object |
| 723 | * @return the view token |
| 724 | */ |
| 725 | addView: function (vid, nid, cb) { |
| 726 | traceFn('addView', vid); |
| 727 | var view = new View(vid, nid, cb), |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 728 | token; |
| 729 | if (view.ok) { |
| 730 | views[vid] = view; |
| 731 | token = view.token(); |
| 732 | } else { |
| 733 | token = { vid: view.vid, bad: true }; |
| 734 | } |
| 735 | return token; |
| 736 | } |
| 737 | }; |
| 738 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 739 | // .......................................................... |
| 740 | // View API |
| 741 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 742 | // TODO: deprecated |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 743 | viewApi = { |
| 744 | /** @api view empty( ) |
| 745 | * Empties the current view. |
| 746 | * <p> |
| 747 | * More specifically, removes all DOM elements from the |
| 748 | * current view's display div. |
| 749 | */ |
| 750 | empty: function () { |
| 751 | if (!current.view) { |
| 752 | return; |
| 753 | } |
| 754 | current.view.$div.html(''); |
| 755 | } |
| 756 | }; |
| 757 | |
| 758 | // .......................................................... |
| 759 | // Nav API |
| 760 | navApi = { |
| 761 | |
| 762 | }; |
| 763 | |
| 764 | // .......................................................... |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 765 | // Library API |
| 766 | libApi = { |
| 767 | |
| 768 | }; |
| 769 | |
| 770 | // .......................................................... |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 771 | // Exported API |
| 772 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 773 | // function to be called from index.html to build the ONOS UI |
| 774 | function buildOnosUi() { |
| 775 | tsB = new Date().getTime(); |
| 776 | tsI = tsB - tsI; // initialization duration |
| 777 | |
| 778 | console.log('ONOS UI initialized in ' + tsI + 'ms'); |
| 779 | |
| 780 | if (built) { |
| 781 | throwError("ONOS UI already built!"); |
| 782 | } |
| 783 | built = true; |
| 784 | |
| 785 | $view = d3.select('#view'); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 786 | $mastRadio = d3.select('#mastRadio'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 787 | |
| 788 | $(window).on('hashchange', hash); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 789 | $(window).on('resize', resize); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 790 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 791 | d3.select('body').on('keydown', keyIn); |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 792 | setupGlobalKeys(); |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 793 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 794 | // Invoke hashchange callback to navigate to content |
| 795 | // indicated by the window location hash. |
| 796 | hash(); |
| 797 | |
| 798 | // If there were any build errors, report them |
| 799 | reportBuildErrors(); |
| 800 | } |
| 801 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 802 | // export the api and build-UI function |
| 803 | return { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 804 | ui: uiApi, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 805 | lib: libApi, |
| 806 | //view: viewApi, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 807 | nav: navApi, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 808 | buildUi: buildOnosUi, |
| 809 | exported: exported |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 810 | }; |
| 811 | }; |
| 812 | |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame] | 813 | }(jQuery)); |