Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [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 -- SVG -- Util Service |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | The SVG Util Service provides a miscellany of utility functions. |
| 23 | */ |
| 24 | |
| 25 | (function () { |
| 26 | 'use strict'; |
| 27 | |
| 28 | // injected references |
| 29 | var $log, fs; |
| 30 | |
| 31 | angular.module('onosSvg') |
| 32 | .factory('SvgUtilService', ['$log', 'FnService', |
| 33 | function (_$log_, _fs_) { |
| 34 | $log = _$log_; |
| 35 | fs = _fs_; |
| 36 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 37 | // TODO: change 'force' ref to be 'force.alpha' ref. |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 38 | function createDragBehavior(force, selectCb, atDragEnd, |
| 39 | dragEnabled, clickEnabled) { |
| 40 | var draggedThreshold = d3.scale.linear() |
| 41 | .domain([0, 0.1]) |
| 42 | .range([5, 20]) |
| 43 | .clamp(true), |
| 44 | drag, |
| 45 | fSel = fs.isF(selectCb), |
| 46 | fEnd = fs.isF(atDragEnd), |
| 47 | fDEn = fs.isF(dragEnabled), |
| 48 | fCEn = fs.isF(clickEnabled), |
| 49 | bad = []; |
| 50 | |
| 51 | function naf(what) { |
| 52 | return 'SvgUtilService: createDragBehavior(): ' + what + |
| 53 | ' is not a function'; |
| 54 | } |
| 55 | |
| 56 | if (!fSel) { |
| 57 | bad.push(naf('selectCb')); |
| 58 | } |
| 59 | if (!fEnd) { |
| 60 | bad.push(naf('atDragEnd')); |
| 61 | } |
| 62 | if (!fDEn) { |
| 63 | bad.push(naf('dragEnabled')); |
| 64 | } |
| 65 | if (!fCEn) { |
| 66 | bad.push(naf('clickEnabled')); |
| 67 | } |
| 68 | |
| 69 | if (bad.length) { |
| 70 | $log.error(bad.join('\n')); |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | function dragged(d) { |
| 76 | var threshold = draggedThreshold(force.alpha()), |
| 77 | dx = d.oldX - d.px, |
| 78 | dy = d.oldY - d.py; |
| 79 | if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) { |
| 80 | d.dragged = true; |
| 81 | } |
| 82 | return d.dragged; |
| 83 | } |
| 84 | |
| 85 | drag = d3.behavior.drag() |
| 86 | .origin(function(d) { return d; }) |
| 87 | .on('dragstart', function(d) { |
| 88 | if (clickEnabled() || dragEnabled()) { |
| 89 | d3.event.sourceEvent.stopPropagation(); |
| 90 | |
| 91 | d.oldX = d.x; |
| 92 | d.oldY = d.y; |
| 93 | d.dragged = false; |
| 94 | d.fixed |= 2; |
| 95 | d.dragStarted = true; |
| 96 | } |
| 97 | }) |
| 98 | .on('drag', function(d) { |
| 99 | if (dragEnabled()) { |
| 100 | d.px = d3.event.x; |
| 101 | d.py = d3.event.y; |
| 102 | if (dragged(d)) { |
| 103 | if (!force.alpha()) { |
| 104 | force.alpha(.025); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | }) |
| 109 | .on('dragend', function(d) { |
| 110 | if (d.dragStarted) { |
| 111 | d.dragStarted = false; |
| 112 | if (!dragged(d)) { |
| 113 | // consider this the same as a 'click' |
| 114 | // (selection of a node) |
| 115 | if (clickEnabled()) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 116 | selectCb.call(this, d); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | d.fixed &= ~6; |
| 120 | |
| 121 | // hook at the end of a drag gesture |
| 122 | if (dragEnabled()) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 123 | atDragEnd.call(this, d); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | }); |
| 127 | |
Simon Hunt | 0ee2868 | 2015-02-12 20:48:11 -0800 | [diff] [blame] | 128 | return drag; |
| 129 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 130 | |
Simon Hunt | 0ee2868 | 2015-02-12 20:48:11 -0800 | [diff] [blame] | 131 | |
| 132 | function loadGlow(defs, r, g, b, id) { |
| 133 | var glow = defs.append('filter') |
| 134 | .attr('x', '-50%') |
| 135 | .attr('y', '-50%') |
| 136 | .attr('width', '200%') |
| 137 | .attr('height', '200%') |
| 138 | .attr('id', id); |
| 139 | |
| 140 | glow.append('feColorMatrix') |
| 141 | .attr('type', 'matrix') |
| 142 | .attr('values', |
| 143 | '0 0 0 0 ' + r + ' ' + |
| 144 | '0 0 0 0 ' + g + ' ' + |
| 145 | '0 0 0 0 ' + b + ' ' + |
| 146 | '0 0 0 1 0 '); |
| 147 | |
| 148 | glow.append('feGaussianBlur') |
| 149 | .attr('stdDeviation', 3) |
| 150 | .attr('result', 'coloredBlur'); |
| 151 | |
| 152 | glow.append('feMerge').selectAll('feMergeNode') |
| 153 | .data(['coloredBlur', 'SourceGraphic']) |
| 154 | .enter().append('feMergeNode') |
| 155 | .attr('in', String); |
| 156 | } |
| 157 | |
| 158 | function loadGlowDefs(defs) { |
| 159 | loadGlow(defs, 0.0, 0.0, 0.7, 'blue-glow'); |
| 160 | loadGlow(defs, 1.0, 1.0, 0.3, 'yellow-glow'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 163 | // --- Ordinal scales for 7 values. |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 164 | |
| 165 | // blue brown brick red sea green purple dark teal lime |
| 166 | var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'], |
| 167 | lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'], |
| 168 | |
Simon Hunt | 27e153a | 2015-02-02 18:45:44 -0800 | [diff] [blame] | 169 | darkNorm = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 170 | darkMute = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700']; |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 171 | |
| 172 | var colors= { |
| 173 | light: { |
| 174 | norm: d3.scale.ordinal().range(lightNorm), |
| 175 | mute: d3.scale.ordinal().range(lightMute) |
| 176 | }, |
| 177 | dark: { |
| 178 | norm: d3.scale.ordinal().range(darkNorm), |
| 179 | mute: d3.scale.ordinal().range(darkMute) |
| 180 | } |
| 181 | }; |
| 182 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 183 | function cat7() { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 184 | var tcid = 'd3utilTestCard'; |
| 185 | |
| 186 | function getColor(id, muted, theme) { |
| 187 | // NOTE: since we are lazily assigning domain ids, we need to |
| 188 | // get the color from all 4 scales, to keep the domains |
| 189 | // in sync. |
| 190 | var ln = colors.light.norm(id), |
| 191 | lm = colors.light.mute(id), |
| 192 | dn = colors.dark.norm(id), |
| 193 | dm = colors.dark.mute(id); |
| 194 | if (theme === 'dark') { |
| 195 | return muted ? dm : dn; |
| 196 | } else { |
| 197 | return muted ? lm : ln; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | function testCard(svg) { |
| 202 | var g = svg.select('g#' + tcid), |
| 203 | dom = d3.range(7), |
| 204 | k, muted, theme, what; |
| 205 | |
| 206 | if (!g.empty()) { |
| 207 | g.remove(); |
| 208 | |
| 209 | } else { |
| 210 | g = svg.append('g') |
| 211 | .attr('id', tcid) |
| 212 | .attr('transform', 'scale(4)translate(20,20)'); |
| 213 | |
| 214 | for (k=0; k<4; k++) { |
| 215 | muted = k%2; |
| 216 | what = muted ? ' muted' : ' normal'; |
| 217 | theme = k < 2 ? 'light' : 'dark'; |
| 218 | dom.forEach(function (id, i) { |
| 219 | var x = i * 20, |
| 220 | y = k * 20, |
| 221 | f = get(id, muted, theme); |
| 222 | g.append('circle').attr({ |
| 223 | cx: x, |
| 224 | cy: y, |
| 225 | r: 5, |
| 226 | fill: f |
| 227 | }); |
| 228 | }); |
| 229 | g.append('rect').attr({ |
| 230 | x: 140, |
| 231 | y: k * 20 - 5, |
| 232 | width: 32, |
| 233 | height: 10, |
| 234 | rx: 2, |
| 235 | fill: '#888' |
| 236 | }); |
| 237 | g.append('text').text(theme + what) |
| 238 | .attr({ |
| 239 | x: 142, |
| 240 | y: k * 20 + 2, |
| 241 | fill: 'white' |
| 242 | }) |
| 243 | .style('font-size', '4pt'); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return { |
| 249 | testCard: testCard, |
| 250 | getColor: getColor |
| 251 | }; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 254 | function translate(x, y) { |
| 255 | if (fs.isA(x) && x.length === 2 && !y) { |
| 256 | return 'translate(' + x[0] + ',' + x[1] + ')'; |
| 257 | } |
| 258 | return 'translate(' + x + ',' + y + ')'; |
| 259 | } |
| 260 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 261 | function stripPx(s) { |
| 262 | return s.replace(/px$/,''); |
| 263 | } |
| 264 | |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 265 | function safeId(s) { |
| 266 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 267 | } |
| 268 | |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 269 | function makeVisible(el, b) { |
| 270 | el.style('visibility', (b ? 'visible' : 'hidden')); |
| 271 | } |
| 272 | |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 273 | function isVisible(el) { |
| 274 | return el.style('visibility') === 'visible'; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 277 | return { |
| 278 | createDragBehavior: createDragBehavior, |
Simon Hunt | 0ee2868 | 2015-02-12 20:48:11 -0800 | [diff] [blame] | 279 | loadGlowDefs: loadGlowDefs, |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 280 | cat7: cat7, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 281 | translate: translate, |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 282 | stripPx: stripPx, |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 283 | safeId: safeId, |
| 284 | visible: function (el, x) { |
| 285 | if (x === undefined) { |
| 286 | return isVisible(el); |
| 287 | } else { |
| 288 | makeVisible(el, x); |
| 289 | } |
| 290 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 291 | }; |
| 292 | }]); |
| 293 | }()); |