Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 -- Topology Dialog Module. |
| 19 | Defines functions for manipulating a dialog box. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
| 26 | var $log, $window, $rootScope, fs, ps, bns; |
| 27 | |
| 28 | // constants |
Simon Hunt | 75c2568 | 2015-11-02 16:49:56 -0800 | [diff] [blame] | 29 | var pCls = 'topo-p dialog', |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 30 | idDialog = 'topo-p-dialog', |
| 31 | panelOpts = { |
Simon Hunt | 75c2568 | 2015-11-02 16:49:56 -0800 | [diff] [blame] | 32 | width: 300, |
| 33 | edge: 'left' |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | // internal state |
| 37 | var pApi, panel, dApi; |
| 38 | |
| 39 | // TODO: ESC key invokes Cancel callback |
| 40 | // TODO: Enter invokes OK callback |
| 41 | |
| 42 | // create the dialog; return its API |
| 43 | function createDialog() { |
| 44 | var header, body, footer, |
| 45 | p = ps.createPanel(idDialog, panelOpts); |
| 46 | p.classed(pCls, true); |
| 47 | panel = p; |
| 48 | |
| 49 | function reset() { |
| 50 | p.empty(); |
| 51 | p.append('div').classed('header', true); |
| 52 | p.append('div').classed('body', true); |
| 53 | p.append('div').classed('footer', true); |
| 54 | |
| 55 | header = p.el().select('.header'); |
| 56 | body = p.el().select('.body'); |
| 57 | footer = p.el().select('.footer'); |
| 58 | } |
| 59 | |
| 60 | function hAppend(x) { |
| 61 | if (typeof x === 'string') { |
| 62 | return header.append(x); |
| 63 | } |
| 64 | header.node().appendChild(x.node()); |
| 65 | return header; |
| 66 | } |
| 67 | |
| 68 | function bAppend(x) { |
| 69 | if (typeof x === 'string') { |
| 70 | return body.append(x); |
| 71 | } |
| 72 | body.node().appendChild(x.node()); |
| 73 | return body; |
| 74 | } |
| 75 | |
| 76 | function fAppend(x) { |
| 77 | if (typeof x === 'string') { |
| 78 | return footer.append(x); |
| 79 | } |
| 80 | footer.node().appendChild(x.node()); |
| 81 | return footer; |
| 82 | } |
| 83 | |
| 84 | function destroy() { |
| 85 | ps.destroyPanel(idDialog); |
| 86 | } |
| 87 | |
| 88 | return { |
| 89 | reset: reset, |
| 90 | appendHeader: hAppend, |
| 91 | appendBody: bAppend, |
| 92 | appendFooter: fAppend, |
| 93 | destroy: destroy |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | function makeButton(text, callback) { |
| 98 | var cb = fs.isF(callback); |
| 99 | |
| 100 | function invoke() { |
| 101 | cb && cb(); |
| 102 | panel.hide(); |
| 103 | } |
| 104 | return createDiv('dialog-button') |
| 105 | .text(text) |
| 106 | .on('click', invoke); |
| 107 | } |
| 108 | |
Simon Hunt | a211f7f | 2015-11-09 12:48:23 -0800 | [diff] [blame] | 109 | function setTitle(title) { |
| 110 | if (pApi) { |
| 111 | pApi.appendHeader('h2').text(title); |
| 112 | } |
| 113 | return dApi; |
| 114 | } |
| 115 | |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 116 | function addContent(content) { |
| 117 | if (pApi) { |
| 118 | pApi.appendBody(content); |
| 119 | } |
| 120 | return dApi; |
| 121 | } |
| 122 | |
| 123 | function addButton(text, cb) { |
| 124 | if (pApi) { |
| 125 | pApi.appendFooter(makeButton(text, cb)); |
| 126 | } |
| 127 | return dApi; |
| 128 | } |
| 129 | |
| 130 | // opens the dialog (creates if necessary) |
| 131 | function openDialog() { |
| 132 | $log.debug('Open DIALOG'); |
| 133 | if (!pApi) { |
| 134 | pApi = createDialog(); |
| 135 | } |
| 136 | pApi.reset(); |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 137 | panel.show(); |
| 138 | |
| 139 | // return the dialog object API |
| 140 | dApi = { |
Simon Hunt | a211f7f | 2015-11-09 12:48:23 -0800 | [diff] [blame] | 141 | setTitle: setTitle, |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 142 | addContent: addContent, |
| 143 | addButton: addButton |
| 144 | }; |
| 145 | return dApi; |
| 146 | } |
| 147 | |
| 148 | // closes the dialog (destroying panel) |
| 149 | function closeDialog() { |
| 150 | $log.debug('Close DIALOG'); |
| 151 | if (pApi) { |
| 152 | panel.hide(); |
| 153 | pApi.destroy(); |
| 154 | pApi = null; |
| 155 | dApi = null; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // creates a detached div, returning D3 selection |
| 160 | // optional CSS class may be provided |
| 161 | function createDiv(cls) { |
| 162 | var div = d3.select(document.createElement('div')); |
| 163 | if (cls) { |
| 164 | div.classed(cls, true); |
| 165 | } |
| 166 | return div; |
| 167 | } |
| 168 | |
| 169 | // ========================== |
| 170 | |
| 171 | angular.module('ovTopo') |
| 172 | .factory('TopoDialogService', |
| 173 | ['$log', '$window', '$rootScope', 'FnService', 'PanelService', 'ButtonService', |
| 174 | |
| 175 | function (_$log_, _$window_, _$rootScope_, |
| 176 | _fs_, _ps_, _bns_) { |
| 177 | $log = _$log_; |
| 178 | $window = _$window_; |
| 179 | $rootScope = _$rootScope_; |
| 180 | fs = _fs_; |
| 181 | ps = _ps_; |
| 182 | bns = _bns_; |
| 183 | |
| 184 | return { |
| 185 | openDialog: openDialog, |
| 186 | closeDialog: closeDialog, |
| 187 | createDiv: createDiv |
| 188 | }; |
| 189 | }]); |
| 190 | }()); |