blob: 792973e09993327bf4c938a52b2b51ad219ca1b2 [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -08001/*
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 Hunta11b4eb2015-01-28 16:20:50 -080037 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()) {
115 selectCb(d, this);
116 // TODO: set 'this' context instead of param
117 }
118 }
119 d.fixed &= ~6;
120
121 // hook at the end of a drag gesture
122 if (dragEnabled()) {
123 atDragEnd(d, this);
124 // TODO: set 'this' context instead of param
125 }
126 }
127 });
128
129 return drag; }
Simon Hunt737c89f2015-01-28 12:23:19 -0800130
131 function loadGlow() {
132 $log.warn('SvgUtilService: loadGlow -- To Be Implemented');
133 }
134
Simon Hunt48e61672015-01-30 14:48:25 -0800135 // --- Ordinal scales for 7 values.
136 // TODO: tune colors for light and dark themes
137 // Note: These colors look good on the white background. Still, need to tune for dark.
138
139 // blue brown brick red sea green purple dark teal lime
140 var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'],
141 lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'],
142
Simon Hunta57d6812015-02-02 18:34:04 -0800143 darkNorm = ['#304860', '#664631', '#a8391b', '#00754b', '#77206d', '#005959', '#428700'],
144 darkMute = ['#16203A', '#281810', '#4F1206', '#00331C', '#3D063A', '#002D2D', '#1B4400'];
Simon Hunt48e61672015-01-30 14:48:25 -0800145
146 var colors= {
147 light: {
148 norm: d3.scale.ordinal().range(lightNorm),
149 mute: d3.scale.ordinal().range(lightMute)
150 },
151 dark: {
152 norm: d3.scale.ordinal().range(darkNorm),
153 mute: d3.scale.ordinal().range(darkMute)
154 }
155 };
156
Simon Hunt737c89f2015-01-28 12:23:19 -0800157 function cat7() {
Simon Hunt48e61672015-01-30 14:48:25 -0800158 var tcid = 'd3utilTestCard';
159
160 function getColor(id, muted, theme) {
161 // NOTE: since we are lazily assigning domain ids, we need to
162 // get the color from all 4 scales, to keep the domains
163 // in sync.
164 var ln = colors.light.norm(id),
165 lm = colors.light.mute(id),
166 dn = colors.dark.norm(id),
167 dm = colors.dark.mute(id);
168 if (theme === 'dark') {
169 return muted ? dm : dn;
170 } else {
171 return muted ? lm : ln;
172 }
173 }
174
175 function testCard(svg) {
176 var g = svg.select('g#' + tcid),
177 dom = d3.range(7),
178 k, muted, theme, what;
179
180 if (!g.empty()) {
181 g.remove();
182
183 } else {
184 g = svg.append('g')
185 .attr('id', tcid)
186 .attr('transform', 'scale(4)translate(20,20)');
187
188 for (k=0; k<4; k++) {
189 muted = k%2;
190 what = muted ? ' muted' : ' normal';
191 theme = k < 2 ? 'light' : 'dark';
192 dom.forEach(function (id, i) {
193 var x = i * 20,
194 y = k * 20,
195 f = get(id, muted, theme);
196 g.append('circle').attr({
197 cx: x,
198 cy: y,
199 r: 5,
200 fill: f
201 });
202 });
203 g.append('rect').attr({
204 x: 140,
205 y: k * 20 - 5,
206 width: 32,
207 height: 10,
208 rx: 2,
209 fill: '#888'
210 });
211 g.append('text').text(theme + what)
212 .attr({
213 x: 142,
214 y: k * 20 + 2,
215 fill: 'white'
216 })
217 .style('font-size', '4pt');
218 }
219 }
220 }
221
222 return {
223 testCard: testCard,
224 getColor: getColor
225 };
Simon Hunt737c89f2015-01-28 12:23:19 -0800226 }
227
Simon Huntc9b73162015-01-29 14:02:15 -0800228 function translate(x, y) {
229 if (fs.isA(x) && x.length === 2 && !y) {
230 return 'translate(' + x[0] + ',' + x[1] + ')';
231 }
232 return 'translate(' + x + ',' + y + ')';
233 }
234
Simon Hunt4b668592015-01-29 17:33:53 -0800235 function stripPx(s) {
236 return s.replace(/px$/,'');
237 }
238
Simon Hunt737c89f2015-01-28 12:23:19 -0800239 return {
240 createDragBehavior: createDragBehavior,
241 loadGlow: loadGlow,
Simon Huntc9b73162015-01-29 14:02:15 -0800242 cat7: cat7,
Simon Hunt4b668592015-01-29 17:33:53 -0800243 translate: translate,
244 stripPx: stripPx
Simon Hunt737c89f2015-01-28 12:23:19 -0800245 };
246 }]);
247}());