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