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