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 |
| 28 | defaultHash = '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, |
| 35 | navApi; |
| 36 | |
| 37 | var defaultOptions = { |
| 38 | trace: false |
| 39 | }; |
| 40 | |
| 41 | // compute runtime settings |
| 42 | var settings = $.extend({}, defaultOptions, options); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 43 | |
| 44 | // internal state |
| 45 | var views = {}, |
| 46 | current = { |
| 47 | view: null, |
| 48 | ctx: '' |
| 49 | }, |
| 50 | built = false, |
| 51 | errorCount = 0; |
| 52 | |
| 53 | // DOM elements etc. |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 54 | var $view, |
| 55 | $mastRadio; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 56 | |
| 57 | |
| 58 | // .......................................................... |
| 59 | // Internal functions |
| 60 | |
| 61 | // throw an error |
| 62 | function throwError(msg) { |
| 63 | // separate function, as we might add tracing here too, later |
| 64 | throw new Error(msg); |
| 65 | } |
| 66 | |
| 67 | function doError(msg) { |
| 68 | errorCount++; |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 69 | console.error(msg); |
| 70 | } |
| 71 | |
| 72 | function trace(msg) { |
| 73 | if (settings.trace) { |
| 74 | console.log(msg); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function traceFn(fn, params) { |
| 79 | if (settings.trace) { |
| 80 | console.log('*FN* ' + fn + '(...): ' + params); |
| 81 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // hash navigation |
| 85 | function hash() { |
| 86 | var hash = window.location.hash, |
| 87 | redo = false, |
| 88 | view, |
| 89 | t; |
| 90 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 91 | traceFn('hash', hash); |
| 92 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 93 | if (!hash) { |
| 94 | hash = defaultHash; |
| 95 | redo = true; |
| 96 | } |
| 97 | |
| 98 | t = parseHash(hash); |
| 99 | if (!t || !t.vid) { |
| 100 | doError('Unable to parse target hash: ' + hash); |
| 101 | } |
| 102 | |
| 103 | view = views[t.vid]; |
| 104 | if (!view) { |
| 105 | doError('No view defined with id: ' + t.vid); |
| 106 | } |
| 107 | |
| 108 | if (redo) { |
| 109 | window.location.hash = makeHash(t); |
| 110 | // the above will result in a hashchange event, invoking |
| 111 | // this function again |
| 112 | } else { |
| 113 | // hash was not modified... navigate to where we need to be |
| 114 | navigate(hash, view, t); |
| 115 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | function parseHash(s) { |
| 119 | // extract navigation coordinates from the supplied string |
| 120 | // "vid,ctx" --> { vid:vid, ctx:ctx } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 121 | traceFn('parseHash', s); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 122 | |
| 123 | var m = /^[#]{0,1}(\S+),(\S*)$/.exec(s); |
| 124 | if (m) { |
| 125 | return { vid: m[1], ctx: m[2] }; |
| 126 | } |
| 127 | |
| 128 | m = /^[#]{0,1}(\S+)$/.exec(s); |
| 129 | return m ? { vid: m[1] } : null; |
| 130 | } |
| 131 | |
| 132 | function makeHash(t, ctx) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 133 | traceFn('makeHash'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 134 | // make a hash string from the given navigation coordinates. |
| 135 | // if t is not an object, then it is a vid |
| 136 | var h = t, |
| 137 | c = ctx || ''; |
| 138 | |
| 139 | if ($.isPlainObject(t)) { |
| 140 | h = t.vid; |
| 141 | c = t.ctx || ''; |
| 142 | } |
| 143 | |
| 144 | if (c) { |
| 145 | h += ',' + c; |
| 146 | } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 147 | trace('hash = "' + h + '"'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 148 | return h; |
| 149 | } |
| 150 | |
| 151 | function navigate(hash, view, t) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 152 | traceFn('navigate', view.vid); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 153 | // closePanes() // flyouts etc. |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 154 | // updateNav() // accordion / selected nav item etc. |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 155 | createView(view); |
| 156 | setView(view, hash, t); |
| 157 | } |
| 158 | |
| 159 | function reportBuildErrors() { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 160 | traceFn('reportBuildErrors'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 161 | // TODO: validate registered views / nav-item linkage etc. |
| 162 | console.log('(no build errors)'); |
| 163 | } |
| 164 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 165 | // returns the reference if it is a function, null otherwise |
| 166 | function isF(f) { |
| 167 | return $.isFunction(f) ? f : null; |
| 168 | } |
| 169 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 170 | // .......................................................... |
| 171 | // View life-cycle functions |
| 172 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 173 | function setViewDimensions(sel) { |
| 174 | var w = window.innerWidth, |
| 175 | h = window.innerHeight - mastHeight; |
| 176 | sel.each(function () { |
| 177 | $(this) |
| 178 | .css('width', w + 'px') |
| 179 | .css('height', h + 'px') |
| 180 | }); |
| 181 | } |
| 182 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 183 | function createView(view) { |
| 184 | var $d; |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 185 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 186 | // lazy initialization of the view |
| 187 | if (view && !view.$div) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 188 | trace('creating view for ' + view.vid); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 189 | $d = $view.append('div') |
| 190 | .attr({ |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 191 | id: view.vid, |
| 192 | class: 'onosView' |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 193 | }); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 194 | setViewDimensions($d); |
| 195 | view.$div = $d; // cache a reference to the D3 selection |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | function setView(view, hash, t) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 200 | traceFn('setView', view.vid); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 201 | // set the specified view as current, while invoking the |
| 202 | // appropriate life-cycle callbacks |
| 203 | |
| 204 | // if there is a current view, and it is not the same as |
| 205 | // the incoming view, then unload it... |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 206 | if (current.view && (current.view.vid !== view.vid)) { |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 207 | current.view.unload(); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 208 | // detach radio buttons, if they were there.. |
| 209 | $('#mastRadio').children().detach(); |
| 210 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // cache new view and context |
| 214 | current.view = view; |
| 215 | current.ctx = t.ctx || ''; |
| 216 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 217 | // preload is called only once, after the view is in the DOM |
| 218 | if (!view.preloaded) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 219 | view.preload(current.ctx); |
| 220 | view.preloaded = true; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // clear the view of stale data |
| 224 | view.reset(); |
| 225 | |
| 226 | // load the view |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 227 | view.load(current.ctx); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 230 | // generate 'unique' id by prefixing view id |
| 231 | function uid(view, id) { |
| 232 | return view.vid + '-' + id; |
| 233 | } |
| 234 | |
| 235 | // restore id by removing view id prefix |
| 236 | function unUid(view, uid) { |
| 237 | var re = new RegExp('^' + view.vid + '-'); |
| 238 | return uid.replace(re, ''); |
| 239 | } |
| 240 | |
| 241 | function setRadioButtons(vid, btnSet, callback) { |
| 242 | var view = views[vid], |
| 243 | btnG; |
| 244 | |
| 245 | // lazily create the buttons... |
| 246 | if (!(btnG = view.radioButtons)) { |
| 247 | btnG = d3.select(document.createElement('div')); |
| 248 | |
| 249 | btnSet.forEach(function (btn, i) { |
| 250 | var bid = btn.id || 'b' + i, |
| 251 | txt = btn.text || 'Button #' + i, |
| 252 | b = btnG.append('span') |
| 253 | .attr({ |
| 254 | id: uid(view, bid), |
| 255 | class: 'radio' |
| 256 | }) |
| 257 | .text(txt); |
| 258 | if (i === 0) { |
| 259 | b.classed('active', true); |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | btnG.selectAll('span') |
| 264 | .on('click', function (d) { |
| 265 | var btn = d3.select(this), |
| 266 | bid = btn.attr('id'), |
| 267 | act = btn.classed('active'); |
| 268 | |
| 269 | if (!act) { |
| 270 | $mastRadio.selectAll('span') |
| 271 | .classed('active', false); |
| 272 | btn.classed('active', true); |
| 273 | |
| 274 | callback(view.token(), unUid(view, bid)); |
| 275 | } |
| 276 | }); |
| 277 | |
| 278 | view.radioButtons = btnG; |
| 279 | } |
| 280 | |
| 281 | // attach the buttons to the masthead |
| 282 | $mastRadio.node().appendChild(btnG.node()); |
| 283 | } |
| 284 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 285 | function resize(e) { |
| 286 | d3.selectAll('.onosView').call(setViewDimensions); |
| 287 | // allow current view to react to resize event... |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 288 | if (current.view) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 289 | current.view.resize(current.ctx); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | // .......................................................... |
| 294 | // View class |
| 295 | // Captures state information about a view. |
| 296 | |
| 297 | // Constructor |
| 298 | // vid : view id |
| 299 | // nid : id of associated nav-item (optional) |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 300 | // cb : callbacks (preload, reset, load, unload, resize, error) |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 301 | function View(vid) { |
| 302 | var av = 'addView(): ', |
| 303 | args = Array.prototype.slice.call(arguments), |
| 304 | nid, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 305 | cb; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 306 | |
| 307 | args.shift(); // first arg is always vid |
| 308 | if (typeof args[0] === 'string') { // nid specified |
| 309 | nid = args.shift(); |
| 310 | } |
| 311 | cb = args.shift(); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 312 | |
| 313 | this.vid = vid; |
| 314 | |
| 315 | if (validateViewArgs(vid)) { |
| 316 | this.nid = nid; // explicit navitem id (can be null) |
| 317 | this.cb = $.isPlainObject(cb) ? cb : {}; // callbacks |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 318 | this.$div = null; // view not yet added to DOM |
| 319 | this.radioButtons = null; // no radio buttons yet |
| 320 | this.ok = true; // valid view |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | function validateViewArgs(vid) { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 326 | var av = "ui.addView(...): ", |
| 327 | ok = false; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 328 | if (typeof vid !== 'string' || !vid) { |
| 329 | doError(av + 'vid required'); |
| 330 | } else if (views[vid]) { |
| 331 | doError(av + 'View ID "' + vid + '" already exists'); |
| 332 | } else { |
| 333 | ok = true; |
| 334 | } |
| 335 | return ok; |
| 336 | } |
| 337 | |
| 338 | var viewInstanceMethods = { |
| 339 | toString: function () { |
| 340 | return '[View: id="' + this.vid + '"]'; |
| 341 | }, |
| 342 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 343 | token: function () { |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 344 | return { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 345 | // attributes |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 346 | vid: this.vid, |
| 347 | nid: this.nid, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 348 | $div: this.$div, |
| 349 | |
| 350 | // functions |
| 351 | width: this.width, |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 352 | height: this.height, |
| 353 | setRadio: this.setRadio |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 354 | } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 355 | }, |
| 356 | |
| 357 | preload: function (ctx) { |
| 358 | var c = ctx || '', |
| 359 | fn = isF(this.cb.preload); |
| 360 | traceFn('View.preload', this.vid + ', ' + c); |
| 361 | if (fn) { |
| 362 | trace('PRELOAD cb for ' + this.vid); |
| 363 | fn(this.token(), c); |
| 364 | } |
| 365 | }, |
| 366 | |
| 367 | reset: function () { |
| 368 | var fn = isF(this.cb.reset); |
| 369 | traceFn('View.reset', this.vid); |
| 370 | if (fn) { |
| 371 | trace('RESET cb for ' + this.vid); |
| 372 | fn(this.token()); |
| 373 | } else if (this.cb.reset === true) { |
| 374 | // boolean true signifies "clear view" |
| 375 | trace(' [true] cleaing view...'); |
| 376 | viewApi.empty(); |
| 377 | } |
| 378 | }, |
| 379 | |
| 380 | load: function (ctx) { |
| 381 | var c = ctx || '', |
| 382 | fn = isF(this.cb.load); |
| 383 | traceFn('View.load', this.vid + ', ' + c); |
| 384 | this.$div.classed('currentView', true); |
| 385 | // TODO: add radio button set, if needed |
| 386 | if (fn) { |
| 387 | trace('LOAD cb for ' + this.vid); |
| 388 | fn(this.token(), c); |
| 389 | } |
| 390 | }, |
| 391 | |
| 392 | unload: function () { |
| 393 | var fn = isF(this.cb.unload); |
| 394 | traceFn('View.unload', this.vid); |
| 395 | this.$div.classed('currentView', false); |
| 396 | // TODO: remove radio button set, if needed |
| 397 | if (fn) { |
| 398 | trace('UNLOAD cb for ' + this.vid); |
| 399 | fn(this.token()); |
| 400 | } |
| 401 | }, |
| 402 | |
| 403 | resize: function (ctx) { |
| 404 | var c = ctx || '', |
| 405 | fn = isF(this.cb.resize), |
| 406 | w = this.width(), |
| 407 | h = this.height(); |
| 408 | traceFn('View.resize', this.vid + '/' + c + |
| 409 | ' [' + w + 'x' + h + ']'); |
| 410 | if (fn) { |
| 411 | trace('RESIZE cb for ' + this.vid); |
| 412 | fn(this.token(), c); |
| 413 | } |
| 414 | }, |
| 415 | |
| 416 | error: function (ctx) { |
| 417 | var c = ctx || '', |
| 418 | fn = isF(this.cb.error); |
| 419 | traceFn('View.error', this.vid + ', ' + c); |
| 420 | if (fn) { |
| 421 | trace('ERROR cb for ' + this.vid); |
| 422 | fn(this.token(), c); |
| 423 | } |
| 424 | }, |
| 425 | |
| 426 | width: function () { |
| 427 | return $(this.$div.node()).width(); |
| 428 | }, |
| 429 | |
| 430 | height: function () { |
| 431 | return $(this.$div.node()).height(); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 432 | }, |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 433 | |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 434 | setRadio: function (btnSet, cb) { |
| 435 | setRadioButtons(this.vid, btnSet, cb); |
| 436 | } |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 437 | |
| 438 | // TODO: consider schedule, clearTimer, etc. |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | // attach instance methods to the view prototype |
| 442 | $.extend(View.prototype, viewInstanceMethods); |
| 443 | |
| 444 | // .......................................................... |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 445 | // UI API |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 446 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 447 | uiApi = { |
| 448 | /** @api ui addView( vid, nid, cb ) |
| 449 | * Adds a view to the UI. |
| 450 | * <p> |
| 451 | * Views are loaded/unloaded into the view content pane at |
| 452 | * appropriate times, by the navigation framework. This method |
| 453 | * adds a view to the UI and returns a token object representing |
| 454 | * the view. A view's token is always passed as the first |
| 455 | * argument to each of the view's life-cycle callback functions. |
| 456 | * <p> |
| 457 | * Note that if the view is directly referenced by a nav-item, |
| 458 | * or in a group of views with one of those views referenced by |
| 459 | * a nav-item, then the <i>nid</i> argument can be omitted as |
| 460 | * the framework can infer it. |
| 461 | * <p> |
| 462 | * <i>cb</i> is a plain object containing callback functions: |
| 463 | * "preload", "reset", "load", "unload", "resize", "error". |
| 464 | * <pre> |
| 465 | * function myLoad(view, ctx) { ... } |
| 466 | * ... |
| 467 | * // short form... |
| 468 | * onos.ui.addView('viewId', { |
| 469 | * load: myLoad |
| 470 | * }); |
| 471 | * </pre> |
| 472 | * |
| 473 | * @param vid (string) [*] view ID (a unique DOM element id) |
| 474 | * @param nid (string) nav-item ID (a unique DOM element id) |
| 475 | * @param cb (object) [*] callbacks object |
| 476 | * @return the view token |
| 477 | */ |
| 478 | addView: function (vid, nid, cb) { |
| 479 | traceFn('addView', vid); |
| 480 | var view = new View(vid, nid, cb), |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 481 | token; |
| 482 | if (view.ok) { |
| 483 | views[vid] = view; |
| 484 | token = view.token(); |
| 485 | } else { |
| 486 | token = { vid: view.vid, bad: true }; |
| 487 | } |
| 488 | return token; |
| 489 | } |
| 490 | }; |
| 491 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 492 | // .......................................................... |
| 493 | // View API |
| 494 | |
| 495 | viewApi = { |
| 496 | /** @api view empty( ) |
| 497 | * Empties the current view. |
| 498 | * <p> |
| 499 | * More specifically, removes all DOM elements from the |
| 500 | * current view's display div. |
| 501 | */ |
| 502 | empty: function () { |
| 503 | if (!current.view) { |
| 504 | return; |
| 505 | } |
| 506 | current.view.$div.html(''); |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | // .......................................................... |
| 511 | // Nav API |
| 512 | navApi = { |
| 513 | |
| 514 | }; |
| 515 | |
| 516 | // .......................................................... |
| 517 | // Exported API |
| 518 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 519 | // function to be called from index.html to build the ONOS UI |
| 520 | function buildOnosUi() { |
| 521 | tsB = new Date().getTime(); |
| 522 | tsI = tsB - tsI; // initialization duration |
| 523 | |
| 524 | console.log('ONOS UI initialized in ' + tsI + 'ms'); |
| 525 | |
| 526 | if (built) { |
| 527 | throwError("ONOS UI already built!"); |
| 528 | } |
| 529 | built = true; |
| 530 | |
| 531 | $view = d3.select('#view'); |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 532 | $mastRadio = d3.select('#mastRadio'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 533 | |
| 534 | $(window).on('hashchange', hash); |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 535 | $(window).on('resize', resize); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 536 | |
| 537 | // Invoke hashchange callback to navigate to content |
| 538 | // indicated by the window location hash. |
| 539 | hash(); |
| 540 | |
| 541 | // If there were any build errors, report them |
| 542 | reportBuildErrors(); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | // export the api and build-UI function |
| 547 | return { |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 548 | ui: uiApi, |
| 549 | view: viewApi, |
| 550 | nav: navApi, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 551 | buildUi: buildOnosUi |
| 552 | }; |
| 553 | }; |
| 554 | |
Simon Hunt | db9eb07 | 2014-11-04 19:12:46 -0800 | [diff] [blame^] | 555 | }(jQuery)); |