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 | |
| 128 | return drag; } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 129 | |
| 130 | function loadGlow() { |
| 131 | $log.warn('SvgUtilService: loadGlow -- To Be Implemented'); |
| 132 | } |
| 133 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 134 | // --- Ordinal scales for 7 values. |
| 135 | // TODO: tune colors for light and dark themes |
| 136 | // Note: These colors look good on the white background. Still, need to tune for dark. |
| 137 | |
| 138 | // blue brown brick red sea green purple dark teal lime |
| 139 | var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'], |
| 140 | lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'], |
| 141 | |
Simon Hunt | 27e153a | 2015-02-02 18:45:44 -0800 | [diff] [blame] | 142 | darkNorm = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 143 | darkMute = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700']; |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 144 | |
| 145 | var colors= { |
| 146 | light: { |
| 147 | norm: d3.scale.ordinal().range(lightNorm), |
| 148 | mute: d3.scale.ordinal().range(lightMute) |
| 149 | }, |
| 150 | dark: { |
| 151 | norm: d3.scale.ordinal().range(darkNorm), |
| 152 | mute: d3.scale.ordinal().range(darkMute) |
| 153 | } |
| 154 | }; |
| 155 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 156 | function cat7() { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 157 | var tcid = 'd3utilTestCard'; |
| 158 | |
| 159 | function getColor(id, muted, theme) { |
| 160 | // NOTE: since we are lazily assigning domain ids, we need to |
| 161 | // get the color from all 4 scales, to keep the domains |
| 162 | // in sync. |
| 163 | var ln = colors.light.norm(id), |
| 164 | lm = colors.light.mute(id), |
| 165 | dn = colors.dark.norm(id), |
| 166 | dm = colors.dark.mute(id); |
| 167 | if (theme === 'dark') { |
| 168 | return muted ? dm : dn; |
| 169 | } else { |
| 170 | return muted ? lm : ln; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | function testCard(svg) { |
| 175 | var g = svg.select('g#' + tcid), |
| 176 | dom = d3.range(7), |
| 177 | k, muted, theme, what; |
| 178 | |
| 179 | if (!g.empty()) { |
| 180 | g.remove(); |
| 181 | |
| 182 | } else { |
| 183 | g = svg.append('g') |
| 184 | .attr('id', tcid) |
| 185 | .attr('transform', 'scale(4)translate(20,20)'); |
| 186 | |
| 187 | for (k=0; k<4; k++) { |
| 188 | muted = k%2; |
| 189 | what = muted ? ' muted' : ' normal'; |
| 190 | theme = k < 2 ? 'light' : 'dark'; |
| 191 | dom.forEach(function (id, i) { |
| 192 | var x = i * 20, |
| 193 | y = k * 20, |
| 194 | f = get(id, muted, theme); |
| 195 | g.append('circle').attr({ |
| 196 | cx: x, |
| 197 | cy: y, |
| 198 | r: 5, |
| 199 | fill: f |
| 200 | }); |
| 201 | }); |
| 202 | g.append('rect').attr({ |
| 203 | x: 140, |
| 204 | y: k * 20 - 5, |
| 205 | width: 32, |
| 206 | height: 10, |
| 207 | rx: 2, |
| 208 | fill: '#888' |
| 209 | }); |
| 210 | g.append('text').text(theme + what) |
| 211 | .attr({ |
| 212 | x: 142, |
| 213 | y: k * 20 + 2, |
| 214 | fill: 'white' |
| 215 | }) |
| 216 | .style('font-size', '4pt'); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return { |
| 222 | testCard: testCard, |
| 223 | getColor: getColor |
| 224 | }; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 227 | function translate(x, y) { |
| 228 | if (fs.isA(x) && x.length === 2 && !y) { |
| 229 | return 'translate(' + x[0] + ',' + x[1] + ')'; |
| 230 | } |
| 231 | return 'translate(' + x + ',' + y + ')'; |
| 232 | } |
| 233 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 234 | function stripPx(s) { |
| 235 | return s.replace(/px$/,''); |
| 236 | } |
| 237 | |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 238 | function makeVisible(el, b) { |
| 239 | el.style('visibility', (b ? 'visible' : 'hidden')); |
| 240 | } |
| 241 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 242 | function safeId(s) { |
| 243 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 244 | } |
| 245 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 246 | return { |
| 247 | createDragBehavior: createDragBehavior, |
| 248 | loadGlow: loadGlow, |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 249 | cat7: cat7, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 250 | translate: translate, |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 251 | stripPx: stripPx, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 252 | makeVisible: makeVisible, |
| 253 | safeId: safeId |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 254 | }; |
| 255 | }]); |
| 256 | }()); |