Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [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 -- Topology Sprite Module. |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 19 | Defines behavior for loading sprites into the sprite layer. |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 26 | var $log, $http, fs, gs, sus, wss; |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 27 | |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 28 | // constants |
| 29 | var tssid = 'TopoSpriteService: ', |
| 30 | fontsize = 20; // default font size 20pt. |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 31 | |
| 32 | // internal state |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 33 | var spriteLayer, defsElement; |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 34 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 35 | function registerPathsAsGlyphs(paths) { |
| 36 | var custom = {}, |
| 37 | ids = []; |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 38 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 39 | function mkd(d) { |
| 40 | return fs.isA(d) ? d.join('') : d; |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 43 | if (paths) { |
| 44 | paths.forEach(function (path) { |
| 45 | var tag = 'spr_' + path.tag; |
| 46 | custom['_' + tag] = path.viewbox || '0 0 1000 1000'; |
| 47 | custom[tag] = mkd(path.d); |
| 48 | ids.push(tag); |
| 49 | }); |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 50 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 51 | gs.registerGlyphs(custom); |
| 52 | gs.loadDefs(defsElement, ids, true); |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 53 | } |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 56 | |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 57 | function doSprite(spr, def, pstrk) { |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 58 | var c = spr.class || 'gray1', |
| 59 | p = spr.pos || [0,0], |
| 60 | lab = spr.label, |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 61 | dim = def.dim || [40,40], |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 62 | w = dim[0], |
| 63 | h = dim[1], |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 64 | dy = def.labelyoff || 1, |
| 65 | sc = def.scale, |
| 66 | xfm = sus.translate(p), |
| 67 | useId = def.glyph || 'spr_' + def.path, |
| 68 | g, attr, use, style; |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 69 | |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 70 | if (sc) { |
| 71 | xfm += sus.scale(sc, sc); |
| 72 | } |
| 73 | |
| 74 | g = spriteLayer.append('g') |
| 75 | .classed(c, true) |
| 76 | .attr('transform', xfm); |
| 77 | |
| 78 | attr = { |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 79 | width: w, |
| 80 | height: h, |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 81 | 'xlink:href': '#' + useId |
| 82 | }; |
| 83 | |
| 84 | use = g.append('use').attr(attr); |
| 85 | |
| 86 | if (pstrk) { |
| 87 | style = {}; |
| 88 | angular.forEach(pstrk, function (value, key) { |
| 89 | style['stroke-' + key] = value; |
| 90 | }); |
| 91 | use.style(style); |
| 92 | } |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 93 | |
| 94 | if (lab) { |
| 95 | g.append('text') |
| 96 | .text(lab) |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 97 | .attr({ x: w / 2, y: h * dy }); |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 98 | } |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 101 | function doLabel(label) { |
| 102 | var c = label.class || 'gray1', |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 103 | p = label.pos || [0,0], |
| 104 | sz = label.size || 1.0, |
| 105 | g = spriteLayer.append('g') |
| 106 | .classed(c, true) |
| 107 | .attr('transform', sus.translate(p)) |
| 108 | .append('text') |
| 109 | .text(label.text) |
| 110 | .style('font-size', (fontsize * sz)+'pt'); |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 114 | // ========================== |
| 115 | // event handlers |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 116 | |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 117 | // Handles response from 'spriteListRequest' which lists all the |
| 118 | // registered sprite definitions on the server. |
| 119 | // (see onos-upload-sprites) |
| 120 | function inList(payload) { |
| 121 | $log.debug(tssid + 'Registered sprite definitions:', payload.names); |
| 122 | // Some day, we will make this list available to the user in |
| 123 | // a dropdown selection box... |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 126 | // Handles response from 'spriteDataRequest' which provides the |
| 127 | // data for the requested sprite definition. |
| 128 | function inData(payload) { |
| 129 | var data = payload.data, |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 130 | name, desc, sprites, labels, alpha, |
| 131 | pathstrokes = {}, |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 132 | defs = {}; |
| 133 | |
| 134 | if (!data) { |
| 135 | $log.warn(tssid + 'No sprite data loaded.') |
| 136 | return; |
| 137 | } |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 138 | name = data.defn_name; |
| 139 | desc = data.defn_desc; |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 140 | |
| 141 | $log.debug("Loading sprites...[" + name + "]", desc); |
| 142 | |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 143 | if (data.paths) { |
| 144 | registerPathsAsGlyphs(data.paths); |
| 145 | data.paths.forEach(function (p) { |
| 146 | pathstrokes[p.tag] = p.stroke; |
| 147 | }); |
| 148 | } |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 149 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 150 | if (data.defn) { |
| 151 | data.defn.forEach(function (d) { |
| 152 | defs[d.id] = d; |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | // pull out the sprite and label items |
| 157 | if (data.load) { |
| 158 | sprites = data.load.sprites; |
| 159 | labels = data.load.labels; |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 160 | alpha = data.load.alpha; |
| 161 | if (alpha) { |
| 162 | spriteLayer.style('opacity', alpha); |
| 163 | } |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (sprites) { |
| 167 | sprites.forEach(function (spr) { |
Simon Hunt | b2c4cc8 | 2015-04-15 17:16:28 -0700 | [diff] [blame^] | 168 | var def = defs[spr.id], |
| 169 | pstrk = def.path && pathstrokes[def.path]; |
| 170 | doSprite(spr, def, pstrk); |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 171 | }); |
| 172 | } |
| 173 | |
| 174 | if (labels) { |
| 175 | labels.forEach(doLabel); |
| 176 | } |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 180 | function loadSprites(layer, defsElem, defname) { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 181 | var name = defname || 'sprites'; |
| 182 | spriteLayer = layer; |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 183 | defsElement = defsElem; |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 184 | |
| 185 | $log.info(tssid + 'Requesting sprite definition ['+name+']...'); |
| 186 | |
| 187 | wss.sendEvent('spriteListRequest'); |
| 188 | wss.sendEvent('spriteDataRequest', {name: name}); |
| 189 | } |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 190 | |
| 191 | // === ----------------------------------------------------- |
| 192 | // === MODULE DEFINITION === |
| 193 | |
| 194 | angular.module('ovTopo') |
| 195 | .factory('TopoSpriteService', |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 196 | ['$log', '$http', 'FnService', 'GlyphService', |
| 197 | 'SvgUtilService', 'WebSocketService', |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 198 | |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 199 | function (_$log_, _$http_, _fs_, _gs_, _sus_, _wss_) { |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 200 | $log = _$log_; |
| 201 | $http = _$http_; |
| 202 | fs = _fs_; |
Simon Hunt | abf66d9 | 2015-04-15 12:57:31 -0700 | [diff] [blame] | 203 | gs = _gs_; |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 204 | sus = _sus_; |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 205 | wss = _wss_; |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 206 | |
| 207 | return { |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 208 | loadSprites: loadSprites, |
| 209 | spriteListResponse: inList, |
| 210 | spriteDataResponse: inData |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 211 | }; |
| 212 | }]); |
| 213 | |
| 214 | }()); |