Steven Burrows | f50a177 | 2017-09-14 11:58:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | (function () { |
| 18 | |
| 19 | var ps, fs, mast, wss, is, EditableTextComponent; |
| 20 | |
| 21 | var panel, |
| 22 | pStartY, |
| 23 | wSize, |
| 24 | wssHandlers = {}, |
| 25 | options; |
| 26 | |
| 27 | // Constants |
| 28 | var topPdg = 28, |
| 29 | defaultLabelWidth = 110, |
| 30 | defaultValueWidth = 80; |
| 31 | |
| 32 | // Elements |
| 33 | var container, |
| 34 | top, |
| 35 | bottom; |
| 36 | |
| 37 | function createDetailsPanel(name, _options) { |
| 38 | options = _options; |
| 39 | scope = options.scope; |
| 40 | |
| 41 | panel = ps.createPanel(name, options); |
| 42 | |
| 43 | calculatePositions(); |
| 44 | |
| 45 | panel.el().style({ |
| 46 | position: 'absolute', |
| 47 | top: pStartY + 'px', |
| 48 | }); |
| 49 | |
| 50 | hide(); |
| 51 | |
| 52 | return panel; |
| 53 | } |
| 54 | |
| 55 | function calculatePositions() { |
| 56 | pStartY = fs.noPxStyle(d3.select('.tabular-header'), 'height') |
| 57 | + mast.mastHeight() + topPdg; |
| 58 | wSize = fs.windowSize(pStartY); |
| 59 | pHeight = wSize.height; |
| 60 | } |
| 61 | |
| 62 | function hide() { |
| 63 | panel.hide(); |
| 64 | } |
| 65 | |
| 66 | function setResponse(name, callback) { |
| 67 | var additionalHandler = {}; |
| 68 | additionalHandler[name] = callback; |
| 69 | |
| 70 | wss.bindHandlers(additionalHandler); |
| 71 | wssHandlers = _.extend({}, wssHandlers, additionalHandler); |
| 72 | } |
| 73 | |
| 74 | function addContainers() { |
| 75 | container = panel.append('div').classed('container', true); |
| 76 | top = container.append('div').classed('top', true); |
| 77 | bottom = container.append('div').classed('bottom', true); |
| 78 | } |
| 79 | |
| 80 | function addCloseButton(onClose) { |
| 81 | var closeBtn = top.append('div').classed('close-btn', true); |
| 82 | |
| 83 | is.loadEmbeddedIcon(closeBtn, 'close', 20); |
| 84 | closeBtn.on('click', onClose || function () {}); |
| 85 | } |
| 86 | |
| 87 | function addHeading(icon) { |
| 88 | top.append('div').classed('iconDiv ' + icon, true); |
| 89 | new EditableTextComponent(top.append('h2'), { |
| 90 | scope: options.scope, |
| 91 | nameChangeRequest: options.nameChangeRequest, |
| 92 | keyBindings: options.keyBindings, |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | function addTable(parent, className) { |
| 97 | return parent.append('div').classed(className, true).append('table'); |
| 98 | } |
| 99 | |
| 100 | function addProp(tbody, key, value) { |
| 101 | console.log(tbody); |
| 102 | var tr = tbody.append('tr'); |
| 103 | |
| 104 | function addCell(cls, txt, width) { |
| 105 | tr.append('td').attr('class', cls).attr('width', width).text(txt); |
| 106 | } |
| 107 | |
| 108 | addCell('label', key + ' :', defaultLabelWidth); |
| 109 | addCell('value', value, defaultValueWidth); |
| 110 | } |
| 111 | |
| 112 | function addPropsList(el, props) { |
| 113 | var tblDiv = el.append('div').classed('top-tables', true); |
| 114 | var left = addTable(tblDiv, 'left').append('tbody'); |
| 115 | var right = addTable(tblDiv, 'right').append('tbody'); |
| 116 | |
| 117 | var keys = _.keys(props); |
| 118 | |
| 119 | _.each(props, function (value, key) { |
| 120 | var index = keys.indexOf(key); |
| 121 | |
| 122 | if (index < keys.length / 2) { |
| 123 | addProp(left, key, value); |
| 124 | } else { |
| 125 | addProp(right, key, value); |
| 126 | } |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | function empty() { |
| 131 | panel.empty(); |
| 132 | } |
| 133 | |
| 134 | function select(id) { |
| 135 | return panel.el().select(id); |
| 136 | } |
| 137 | |
| 138 | function destroy() { |
| 139 | wss.unbindHandlers(wssHandlers); |
| 140 | } |
| 141 | |
| 142 | angular.module('onosLayer') |
| 143 | .factory('DetailsPanelService', [ |
| 144 | |
| 145 | 'PanelService', 'FnService', 'MastService', 'WebSocketService', |
| 146 | 'IconService', 'EditableTextComponent', |
| 147 | |
| 148 | function (_ps_, _fs_, _mast_, _wss_, _is_, _etc_) { |
| 149 | |
| 150 | ps = _ps_; |
| 151 | fs = _fs_; |
| 152 | mast = _mast_; |
| 153 | wss = _wss_; |
| 154 | is = _is_; |
| 155 | EditableTextComponent = _etc_; |
| 156 | |
| 157 | return { |
| 158 | create: createDetailsPanel, |
| 159 | setResponse: setResponse, |
| 160 | |
| 161 | addContainers: addContainers, |
| 162 | addCloseButton: addCloseButton, |
| 163 | addHeading: addHeading, |
| 164 | addPropsList: addPropsList, |
| 165 | |
| 166 | // Elements |
| 167 | container: function () { return container; }, |
| 168 | top: function () { return top; }, |
| 169 | bottom: function () { return bottom; }, |
| 170 | select: select, |
| 171 | |
| 172 | empty: empty, |
| 173 | hide: hide, |
| 174 | destroy: destroy, |
| 175 | }; |
| 176 | }, |
| 177 | ]); |
| 178 | })(); |