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