neft
Version:
JavaScript. Everywhere.
68 lines (53 loc) • 1.85 kB
text/coffeescript
'use strict'
utils = require 'src/utils'
assert = require 'src/assert'
log = require 'src/log'
module.exports = (File) -> class AttrsToSet
= 'AttrsToSet'
= 'File.AttrsToSet'
JSON_CTOR_ID = = File.JSON_CTORS.push(AttrsToSet) - 1
i = 1
JSON_NODE = i++
JSON_ATTRS = i++
JSON_ARGS_LENGTH = = i
= (file, arr, obj) ->
unless obj
node = file.node.getChildByAccessPath arr[JSON_NODE]
obj = new AttrsToSet file, node, arr[JSON_ATTRS]
obj
constructor: (, , ) ->
assert.instanceOf , File
assert.instanceOf , File.Element
assert.isPlainObject
# set current attributes
for attr of
if .attrs[attr]?
this.setAttribute attr, null
# listen on changes
.onAttrsChange , this
`//<development>`
if is AttrsToSet
Object.preventExtensions @
`//</development>`
setAttribute: (attr, oldValue) ->
unless [attr]
return
val = .attrs[attr]
if typeof [attr] is 'function' and [attr].connect
if typeof oldValue is 'function'
[attr].disconnect oldValue
if typeof val is 'function'
[attr] val
else
[attr] = val
return
clone: (original, file) ->
node = original.node.getCopiedElement , file.node
new AttrsToSet file, node,
toJSON: (key, arr) ->
unless arr
arr = new Array JSON_ARGS_LENGTH
arr[0] = JSON_CTOR_ID
arr[JSON_NODE] = .getAccessPath .node
arr[JSON_ATTRS] =
arr