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