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