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