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 DIV based view. |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
| 26 | var list, |
| 27 | data = [ 'foo', 'bar', 'baz' ]; |
| 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 |
Simon Hunt | a2994cc | 2014-12-02 14:19:15 -0800 | [diff] [blame] | 31 | function init(view, ctx, flags) { |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 32 | // NOTE: view.$div is a D3 selection of the view's div |
| 33 | list = view.$div.append('ul'); |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 34 | // ... further code to initialize the SVG view ... |
| 35 | |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 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) { |
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) { |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 48 | list.selectAll('li') |
| 49 | .data(data) |
| 50 | .enter() |
| 51 | .append('li') |
| 52 | .text(function (d) { return d; }) |
| 53 | } |
| 54 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 55 | // invoked when the view is unloaded |
| 56 | // - used to clean up data that should be removed, |
| 57 | // when the view is hidden |
| 58 | function unload(view, ctx, flags) { |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 59 | |
| 60 | } |
| 61 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 62 | // invoked when the view is resized |
| 63 | // - used to reconfigure elements to the new view size |
| 64 | function resize(view, ctx, flags) { |
| 65 | var w = view.width(), |
| 66 | h = view.height(); |
| 67 | |
| 68 | } |
| 69 | |
| 70 | // invoked when the framework needs to alert the view of an error |
| 71 | // - (EXPERIMENTAL -- not currently used) |
| 72 | function error(view, ctx, flags) { |
| 73 | |
| 74 | } |
| 75 | |
| 76 | // ================================================================ |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 77 | // == register the view here, with links to lifecycle callbacks |
| 78 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 79 | // A typical setup that initializes the view once, then reacts to |
| 80 | // load and resize events would look like this: |
| 81 | |
| 82 | onos.ui.addView('myDivViewId', { |
Simon Hunt | a2994cc | 2014-12-02 14:19:15 -0800 | [diff] [blame] | 83 | init: init, |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 84 | load: load, |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 85 | resize: resize |
| 86 | }); |
| 87 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 88 | // A minimum setup that builds the view every time it is loaded |
| 89 | // would look like this: |
| 90 | // |
| 91 | // onos.ui.addView('myViewId', { |
| 92 | // reset: true, // clear view contents on reset |
| 93 | // load: load |
| 94 | // }); |
| 95 | |
| 96 | // The complete gamut of callbacks would look like this: |
| 97 | // |
| 98 | // onos.ui.addView('myViewId', { |
Simon Hunt | a2994cc | 2014-12-02 14:19:15 -0800 | [diff] [blame] | 99 | // init: init, |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 100 | // reset: reset, |
| 101 | // load: load, |
| 102 | // unload: unload, |
| 103 | // resize: resize, |
| 104 | // error: error |
| 105 | // }); |
| 106 | |
Simon Hunt | 8914d8b | 2014-11-04 17:06:00 -0800 | [diff] [blame] | 107 | }(ONOS)); |