Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -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 | Module template file for SVG based view. |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 26 | var svg, |
| 27 | data = [ 60 ]; |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 28 | |
| 29 | // invoked only the first time the view is loaded |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 30 | // - used to initialize the view contents |
| 31 | function preload(view, ctx, flags) { |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 32 | svg = view.$div.append('svg'); |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 33 | resize(view); |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 34 | // ... further code to initialize the SVG view ... |
| 35 | |
| 36 | } |
| 37 | |
| 38 | // invoked just prior to loading the view |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 39 | // - used to clear the view of stale data |
| 40 | function reset(view, ctx, flags) { |
| 41 | // e.g. clear svg of all objects... |
| 42 | // svg.html(''); |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 43 | |
| 44 | } |
| 45 | |
| 46 | // invoked when the view is loaded |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 47 | // - used to load data into the view, |
| 48 | // when the view is shown |
| 49 | function load(view, ctx, flags) { |
| 50 | var w = view.width(), |
| 51 | h = view.height(); |
| 52 | |
| 53 | // as an example... |
| 54 | svg.selectAll('circle') |
| 55 | .data(data) |
| 56 | .enter() |
| 57 | .append('circle') |
| 58 | .attr({ |
| 59 | cx: w / 2, |
| 60 | cy: h / 2, |
| 61 | r: function (d) { return d; } |
| 62 | }) |
| 63 | .style({ |
| 64 | fill: 'goldenrod', |
| 65 | stroke: 'black', |
| 66 | 'stroke-width': 3.5, |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | // invoked when the view is unloaded |
| 71 | // - used to clean up data that should be removed, |
| 72 | // when the view is hidden |
| 73 | function unload(view, ctx, flags) { |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 74 | |
| 75 | } |
| 76 | |
| 77 | // invoked when the view is resized |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 78 | // - used to reconfigure elements to the new size of the view |
| 79 | function resize(view, ctx, flags) { |
| 80 | var w = view.width(), |
| 81 | h = view.height(); |
| 82 | |
| 83 | // resize svg layer to match new size of view |
| 84 | svg.attr({ |
| 85 | width: w, |
| 86 | height: h |
| 87 | }); |
| 88 | |
| 89 | // as an example... |
| 90 | svg.selectAll('circle') |
| 91 | .attr({ |
| 92 | cx: w / 2, |
| 93 | cy: h / 2 |
| 94 | }); |
| 95 | |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 96 | // ... further code to handle resize of view ... |
| 97 | |
| 98 | } |
| 99 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 100 | // invoked when the framework needs to alert the view of an error |
| 101 | // - (EXPERIMENTAL -- not currently used) |
| 102 | function error(view, ctx, flags) { |
| 103 | |
| 104 | } |
| 105 | |
| 106 | // ================================================================ |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 107 | // == register the view here, with links to lifecycle callbacks |
| 108 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 109 | // A typical setup that initializes the view once, then reacts to |
| 110 | // load and resize events would look like this: |
| 111 | |
| 112 | onos.ui.addView('mySvgViewId', { |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 113 | preload: preload, |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 114 | load: load, |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 115 | resize: resize |
| 116 | }); |
| 117 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 118 | // A minimum setup that builds the view every time it is loaded |
| 119 | // would look like this: |
| 120 | // |
| 121 | // onos.ui.addView('myViewId', { |
| 122 | // reset: true, // clear view contents on reset |
| 123 | // load: load |
| 124 | // }); |
| 125 | |
| 126 | // The complete gamut of callbacks would look like this: |
| 127 | // |
| 128 | // onos.ui.addView('myViewId', { |
| 129 | // preload: preload, |
| 130 | // reset: reset, |
| 131 | // load: load, |
| 132 | // unload: unload, |
| 133 | // resize: resize, |
| 134 | // error: error |
| 135 | // }); |
| 136 | |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 137 | }(ONOS)); |