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