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