Simon Hunt | a1028c4 | 2017-02-07 20:08:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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 -- Sprite Service |
| 19 | * For defining sprites and layouts (of sprites). |
| 20 | */ |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
| 24 | // injected references |
| 25 | var $log, fs, sds; |
| 26 | |
| 27 | // configuration of default options |
| 28 | var optDefaults = { |
| 29 | sprite: { |
| 30 | builder: { |
| 31 | // none for now |
| 32 | }, |
| 33 | addRect: { |
| 34 | // none for now |
| 35 | }, |
| 36 | addPath: { |
| 37 | // none for now |
| 38 | } |
| 39 | }, |
| 40 | layout: { |
| 41 | builder: { |
| 42 | grid: 10 // grid square size (in layout coord-space |
| 43 | }, |
| 44 | addSprite: { |
| 45 | anchor: 'topleft' // topleft, center |
| 46 | }, |
| 47 | addLabel: { |
| 48 | anchor: 'center', // center, left, right |
| 49 | fontStyle: 'normal' // normal, italic, bold |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | // internal state |
| 55 | var sprites, // sprite cache |
| 56 | layouts, // layout cache |
| 57 | api; |
| 58 | |
| 59 | // ---------------------------------------------------------------------- |
| 60 | // === Sprite Builder === |
| 61 | |
| 62 | // Sample usage: |
| 63 | // |
| 64 | // ss.createSprite('foo', 100, 100) |
| 65 | // .addPath('M40,40h20v20h-20z') |
| 66 | // .addRect(50, 50, 10, 20) |
| 67 | // .register(); |
| 68 | |
| 69 | function spriteBuilder(id, w, h, opts) { |
| 70 | var o = angular.extend({}, optDefaults.sprite.builder, opts), |
| 71 | builder, |
| 72 | paths = [], |
| 73 | rects = []; |
| 74 | |
| 75 | // TODO: verify id has not already been registered |
| 76 | |
| 77 | // x,y is top left corner; w,h is width and height of rectangle |
| 78 | function addRect(x, y, w, h, opts) { |
| 79 | var o = angular.extend({}, optDefaults.sprite.addRect, opts); |
| 80 | |
| 81 | rects.push({ |
| 82 | x: x, y: y, w: w, h: h |
| 83 | }); |
| 84 | return builder; |
| 85 | } |
| 86 | |
| 87 | function addPath(d, opts) { |
| 88 | var o = angular.extend({}, optDefaults.sprite.addPath, opts); |
| 89 | |
| 90 | if (fs.isS(d)) { |
| 91 | paths.push(d); |
| 92 | } else if (fs.isA(d)) { |
| 93 | paths.push(d.join('')); |
| 94 | } else { |
| 95 | $log.warn('addPath: path not a string or array', d); |
| 96 | } |
| 97 | return builder; |
| 98 | } |
| 99 | |
| 100 | function register() { |
| 101 | sprites.set(id, builder); |
| 102 | } |
| 103 | |
| 104 | // define the builder object... |
| 105 | builder = { |
| 106 | type: 'sprite', |
| 107 | data: { |
| 108 | id: id, |
| 109 | w: w, |
| 110 | h: h, |
| 111 | opts: o |
| 112 | }, |
| 113 | paths: paths, |
| 114 | rects: rects, |
| 115 | |
| 116 | // builder API |
| 117 | addRect: addRect, |
| 118 | addPath: addPath, |
| 119 | register: register |
| 120 | }; |
| 121 | |
| 122 | return builder; |
| 123 | } |
| 124 | |
| 125 | // ---------------------------------------------------------------------- |
| 126 | // === Layout Builder === |
| 127 | |
| 128 | // Sample usage: |
| 129 | // |
| 130 | // ss.createLayout('fooLayout', 400, 300) |
| 131 | // .addSprite('foo', 10, 10, 40) |
| 132 | // .addSprite('foo', 60, 10, 40) |
| 133 | // .addSprite('foo', 110, 10, 40) |
| 134 | // .register(); |
| 135 | |
| 136 | function layoutBuilder(id, w, h, opts) { |
| 137 | var o = angular.extend({}, optDefaults.layout.builder, opts), |
| 138 | builder, |
| 139 | sprs = [], |
| 140 | labs = []; |
| 141 | |
| 142 | // TODO: verify id has not already been registered |
| 143 | |
| 144 | function addSprite(id, x, y, w, opts) { |
| 145 | var o = angular.extend({}, optDefaults.layout.addSprite, opts), |
| 146 | s = sprites.get(id); |
| 147 | |
| 148 | if (!s) { |
| 149 | $log.warn('no such sprite:', id); |
| 150 | return builder; |
| 151 | } |
| 152 | |
| 153 | sprs.push({ |
| 154 | sprite: s, x: x, y: y, w: w, anchor: o.anchor |
| 155 | }); |
| 156 | return builder; |
| 157 | } |
| 158 | |
| 159 | function addLabel(text, x, y, opts) { |
| 160 | var o = angular.extend({}, optDefaults.layout.addLabel, opts); |
| 161 | |
| 162 | labs.push({ |
| 163 | text: text, x: x, y: y, anchor: o.anchor, style: o.fontStyle |
| 164 | }); |
| 165 | return builder; |
| 166 | } |
| 167 | |
| 168 | function register() { |
| 169 | layouts.set(id, builder); |
| 170 | } |
| 171 | |
| 172 | // define the builder object... |
| 173 | builder = { |
| 174 | type: 'layout', |
| 175 | data: { |
| 176 | id: id, |
| 177 | w: w, |
| 178 | h: h, |
| 179 | opts: o |
| 180 | }, |
| 181 | sprites: sprs, |
| 182 | labels: labs, |
| 183 | |
| 184 | // builder API |
| 185 | addSprite: addSprite, |
| 186 | addLabel: addLabel, |
| 187 | register: register |
| 188 | }; |
| 189 | |
| 190 | return builder; |
| 191 | } |
| 192 | |
| 193 | // ---------------------------------------------------------------------- |
| 194 | // === API functions === |
| 195 | |
| 196 | // Clears the sprite / layout caches. |
| 197 | function clear() { |
| 198 | sprites = d3.map(); |
| 199 | layouts = d3.map(); |
| 200 | } |
| 201 | |
| 202 | // Initializes the sprite / layout caches with core elements. |
| 203 | function init() { |
| 204 | sds.registerCoreSprites(api); |
| 205 | } |
| 206 | |
| 207 | // Returns a sprite "builder", which can be used to programmatically |
| 208 | // define a sprite. |
| 209 | function createSprite(id, w, h) { |
| 210 | $log.debug('createSprite:', id, w, 'x', h); |
| 211 | return spriteBuilder(id, w, h); |
| 212 | } |
| 213 | |
| 214 | // Returns a layout "builder", which can be used to programmatically |
| 215 | // define a layout. |
| 216 | function createLayout(id, w, h, grid) { |
| 217 | $log.debug('createLayout:', id, w, 'x', h, '(grid=' + grid + ')'); |
| 218 | return layoutBuilder(id, w, h, grid); |
| 219 | } |
| 220 | |
| 221 | // Registers a sprite defined by the given object (JSON structure). |
| 222 | function registerSprite(json) { |
| 223 | $log.debug('registerSprite:', json); |
| 224 | // TODO: create and register a sprite based on JSON data |
| 225 | } |
| 226 | |
| 227 | // Registers a layout defined by the given object (JSON structure). |
| 228 | function registerLayout(json) { |
| 229 | $log.debug('registerLayout:', json); |
| 230 | // TODO: create and register a layout based on JSON data |
| 231 | } |
| 232 | |
| 233 | // Returns the sprite with the given ID, or undefined otherwise. |
| 234 | function sprite(id) { |
| 235 | return sprites.get(id); |
| 236 | } |
| 237 | |
| 238 | // Returns the layout with the given ID, or undefined otherwise. |
| 239 | function layout(id) { |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame^] | 240 | return layouts.get(id); |
Simon Hunt | a1028c4 | 2017-02-07 20:08:03 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Returns a count of registered sprites and layouts. |
| 244 | function count() { |
| 245 | return { |
| 246 | sprites: sprites.size(), |
| 247 | layouts: layouts.size() |
| 248 | }; |
| 249 | } |
| 250 | |
| 251 | // Dumps the cache contents to console |
| 252 | function dump() { |
| 253 | $log.debug('Dumping Caches...'); |
| 254 | $log.debug('sprites:', sprites); |
| 255 | $log.debug('layouts:', layouts); |
| 256 | } |
| 257 | |
| 258 | // ---------------------------------------------------------------------- |
| 259 | |
| 260 | angular.module('onosSvg') |
| 261 | .factory('SpriteService', |
| 262 | ['$log', 'FnService', 'SpriteDataService', |
| 263 | |
| 264 | function (_$log_, _fs_, _sds_) { |
| 265 | $log = _$log_; |
| 266 | fs = _fs_; |
| 267 | sds = _sds_; |
| 268 | |
| 269 | api = { |
| 270 | clear: clear, |
| 271 | init: init, |
| 272 | createSprite: createSprite, |
| 273 | createLayout: createLayout, |
| 274 | registerSprite: registerSprite, |
| 275 | registerLayout: registerLayout, |
| 276 | sprite: sprite, |
| 277 | layout: layout, |
| 278 | count: count, |
| 279 | dump: dump |
| 280 | }; |
| 281 | return api; |
| 282 | }] |
| 283 | ) |
| 284 | .run(['$log', function ($log) { |
| 285 | $log.debug('Clearing sprite and layout caches'); |
| 286 | clear(); |
| 287 | }]); |
| 288 | |
| 289 | }()); |