Requires

Provides

Layer.js

Adds a piece of SVG that can be drawn with widget styles

License:
Public domain (http://unlicense.org).
Authors:
Yaroslaff Fedin
  1. 25
  2. 26
  3. 27
  4. 28
  5. 29
  6. 30
  7. 31
  8. 32
  9. 33
  10. 34
  11. 35
  12. 36
  13. 37
  14. 38
  15. 39
  16. 40
  17. 41
  18. 42
  19. 43
  20. 44
  21. 45
  22. 46
  23. 47
  24. 48
  25. 49
  26. 50
  27. 51
  28. 52
  29. 53
  30. 54
  31. 55
  32. 56
  33. 57
  34. 58
  35. 59
  36. 60
  37. 61
  38. 62
  39. 63
  40. 64
  41. 65
  42. 66
  43. 67
  44. 68
  45. 69
  46. 70
  47. 71
  48. 72
  49. 73
  50. 74
  51. 75
  52. 76
  53. 77
!function() { LSD.Layer = function(name, styles, painters) { this.name = name; this.styles = styles; this.painters = painters; } LSD.Layer.prototype = { render: function(widget, commands) { var canvas = widget.getCanvas(); var shape = commands.shape; if (shape == 'none') return; if (!shape) shape = widget.getStyle('shape') || 'rectangle'; var layer = widget.shapes[this.name]; if (shape.glyph) { var glyph = ART.Glyphs[shape.glyph]; if (!glyph) return; var path = new ART.Path(glyph); var box = path.measure(); if (!layer) layer = new ART.Shape(path, box.width, box.height); if (commands.size && !Object.equals(previous ? previous.size : box, commands.size)) layer.resizeTo(commands.size.width, commands.size.height) } else if (!shape.indexOf){ for (var name in shape) { var values = shape[name]; if (!values.push) values = [values]; shape = name; } } if (!layer) { var path = ART.Shape[shape.capitalize()]; if (!path) return; var layer = new path; layer.render(commands) } else { var previous = layer.commands; if (layer.draw && layer.render) layer.render(commands) } layer.commands = commands; widget.shapes[this.name] = layer; for (command in commands) { var value = commands[command]; if (layer[command] && command != 'move') { if (!value || !previous || !Object.equals(previous[command], value)) layer[command][value && value.push ? 'apply' : 'call'](layer, value); } } var translate = commands.translate = {x: 0, y: 0} if (commands.inside) { translate.x += commands.inside.left translate.y += commands.inside.top; };

if (commands.outside) { top += commands.outside.top; left += commands.outside.left };

  1. 82
  2. 83
  3. 84
  4. 85
  5. 86
  6. 87
  7. 88
  8. 89
  9. 90
  10. 91
  11. 92
  12. 93
  13. 94
  14. 95
  15. 96
  16. 97
  17. 98
  18. 99
  19. 100
  20. 101
  21. 102
  22. 103
  23. 104
  24. 105
if (commands.move) { translate.x += commands.move.x; translate.y += commands.move.y; } if (!previous || !Object.equals(previous.translate, translate)) layer.moveTo(translate.x, translate.y) }, draw: function(widget, context, previous) { context = Object.append({size: widget.size, style: widget.style.current}, context || {}); if (context.style.cornerRadiusTopLeft !== null) { context.radius = widget.getStyle('cornerRadius') } var inherited = {}, overwritten = {}; for (var painter, i = 0; painter = this.painters[i++];) { var commands = painter.paint.apply(context, painter.keys.map(function(prop) { return widget.getStyle(prop)})); for (var name in commands) { var value = commands[name]; if (Inherit[name]) {; inherited[name] = merge(value, context[name]) } else { if (!Accumulate[name]) overwritten[name] = context[name] context[name] = (Accumulate[name] || Merge[name]) ? merge(value, context[name]) : value; } }

for (var command in value) thiscommand;

  1. 107
  2. 108
  3. 109
  4. 110
  5. 111
  6. 112
  7. 113
  8. 114
  9. 115
  10. 116
  11. 117
  12. 118
  13. 119
  14. 120
  15. 121
  16. 122
  17. 123
  18. 124
  19. 125
  20. 126
  21. 127
  22. 128
  23. 129
  24. 130
  25. 131
  26. 132
  27. 133
  28. 134
  29. 135
  30. 136
  31. 137
  32. 138
  33. 139
  34. 140
  35. 141
} this.render(widget, context); return Object.append(context, overwritten, inherited);; } } var merge = function(value, old) { if (typeof value == "object") { if (value.push) { for (var j = 0, k = value.length; j < k; j++) { var item = value[j] || 0; if (old) old[j] = (old[j] || 0) + item; else old = [item] } return old; } else if (!value.indexOf) { for (var prop in value) { var item = value[prop] || 0; if (!old) old = {} old[prop] = (old[prop] || 0) + item; } return old; } } return value; } var Accumulate = LSD.Layer.accumulated = new FastArray('translate', 'radius'); var Inherit = LSD.Layer.inherited = new FastArray('inside', 'outside') var Merge = LSD.Layer.merged = new FastArray('size') var Property = SheetParser.Property; var Styles = LSD.Styles; var Map = LSD.Layer.Map = {}; var Cache = LSD.Layer.Cache = {};

LSD.Layer.getProperty = function(property, properties)

  1. 145
  2. 146
  3. 147
  4. 148
  5. 149
  6. 150
  7. 151
  8. 152
  9. 153
  10. 154
  11. 155
  12. 156
  13. 157
  14. 158
  15. 159
  16. 160
  17. 161
  18. 162
  19. 163
  20. 164
  21. 165
  22. 166
  23. 167
  24. 168
  25. 169
  26. 170
  27. 171
  28. 172
  29. 173
  30. 174
  31. 175
  32. 176
  33. 177
  34. 178
  35. 179
  36. 180
  37. 181
  38. 182
  39. 183
  40. 184
  41. 185
  42. 186
  43. 187
  44. 188
  45. 189
  46. 190
  47. 191
  48. 192
  49. 193
  50. 194
  51. 195
  52. 196
  53. 197
  54. 198
  55. 199
  56. 200
  57. 201
  58. 202
  59. 203
  60. 204
  61. 205
  62. 206
  63. 207
  64. 208
  65. 209
  66. 210
  67. 211
  68. 212
  69. 213
  70. 214
  71. 215
LSD.Layer.generate = function(name, layers) { if (arguments.length > 2) layers = Array.prototype.splice.call(arguments, 1); var painters = []; var styles = LSD.Layer.prepare(name, layers, function(painter) { painters.push(painter) }) return new LSD.Layer(name, styles, painters); }; LSD.Layer.prepare = function(name, layers, callback) { var properties = [], styles = {}; for (var i = 0, layer; layer = layers[i++];) { var definition = LSD.Layer[layer.capitalize()]; if (!definition ) continue; var properties = definition.properties && Object.clone(definition.properties); if (!properties) continue; definition = Object.append({styles: {}, keys: []}, definition); var prefix = definition.prefix; if (prefix === false || layer == name) prefix = name; else if (!prefix) prefix = name + layer.capitalize(); var length = 0; for (var property in properties) length++ var simple = (length == 1); Object.each(properties, function(value, property) { if (property == layer) { if (simple) var style = prefix else return; } else var style = prefix + property.capitalize() if (style == 'stroke') console.error(123, '!!!!!!!!!!!!!!!!!!!!', [].concat(properties), layer, property, value, prefix) definition.styles[style] = styles[style] = Property.compile(value, properties); definition.keys.push(style); }); var shorthand = properties[layer]; if (shorthand && !simple) { var style = (layer == name) ? name : name + layer.capitalize(); if (length) { for (var j = 0, k = 0, l = 0, prop; prop = shorthand[j]; j++) { if (!prop.push) { if (properties[prop]) { shorthand[j] = prefix + prop.capitalize(); k++ } } else for (var m = 0, sub; sub = prop[m]; m++) { if (properties[sub]) { prop[m] = prefix + sub.capitalize(); l++; } } } } definition.styles[style] = styles[style] = Property.compile(((l > 0 && (k > 0 || j == 1)) ) ? [shorthand] : shorthand, styles); definition.shorthand = style; } if (definition.onCompile) definition.onCompile(name); if (callback) callback(definition); } for (var property in styles) { Styles[property] = styles[property]; Map[property] = name; } return styles; } LSD.Layer.get = function(name) { var key = name//Array.flatten(arguments).join(''); if (Cache[key]) return Cache[key]; else return (Cache[key] = LSD.Layer.generate.apply(LSD.Layer, arguments)) } }();