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