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