neft
Version:
JavaScript. Everywhere.
166 lines (131 loc) • 4.3 kB
text/coffeescript
utils = require 'src/utils'
assert = require 'src/assert'
log = require 'src/log'
signal = require 'src/signal'
Dict = require 'src/dict'
List = require 'src/list'
Binding = require 'src/binding'
assert = assert.scope 'View.Input'
log = log.scope 'View', 'Input'
class DocumentBinding extends Binding
= (binding, ctx, target) ->
target ?= new DocumentBinding binding, ctx
Binding.New binding, ctx, target
constructor: (binding, ctx) ->
super binding, ctx
= ctx.file.inputArgs
`//<development>`
= false
= false
`//</development>`
getItemById: (item) ->
if item is 'this'
else if item is 'ids'
[0]
else if item is 'props'
[1]
else if item is 'state'
[2]
`//<development>`
failCheckQueue = []
failCheckQueuePending = false
checkFails = ->
while binding = failCheckQueue.pop()
err = failCheckQueue.pop()
if binding.failed
log.error "Error in '#{binding.ctx.text}', file '#{binding.ctx.file.path}':\n#{err}"
binding.failCheckPending = false
failCheckQueuePending = false
return
onError: (err) ->
= true
unless
= true
failCheckQueue.push err, @
unless failCheckQueuePending
failCheckQueuePending = true
setImmediate checkFails
return
update: ->
= false
super()
`//</development>`
getValue: ->
.getValue()
setValue: (val) ->
.setValue val
module.exports = (File) -> class Input extends signal.Emitter
{Element} = File
{Tag} = Element
= 'Input'
= 'File.Input'
JSON_CTOR_ID = = File.JSON_CTORS.push(Input) - 1
i = 1
JSON_NODE = = i++
JSON_TEXT = = i++
JSON_BINDING = = i++
JSON_ARGS_LENGTH = = i
= (file, arr, obj) ->
unless obj
node = file.node.getChildByAccessPath arr[JSON_NODE]
obj = new Input file, node, arr[JSON_TEXT], arr[JSON_BINDING]
obj
RE = = new RegExp '([^$]*)\\${([^}]*)}([^$]*)', 'gm'
= (str) ->
RE.lastIndex = 0
RE.test str
if utils.isServer
= require('./input/parser').parse
createFunction = (func) ->
new Function 'ids', 'props', 'state', func
constructor: (, , , ) ->
assert.instanceOf , File
assert.instanceOf , File.Element
assert.isString
assert.isObject
super()
= null
= null
.func ?= createFunction .body
`//<development>`
if is Input
Object.seal @
`//</development>`
signal.Emitter.createSignal @, 'onTargetChange'
signal.Emitter.createSignal @, 'onRootChange'
registerBinding: do ->
cache = Object.create null
->
{binding} = @
arr = cache[binding.body] ?= [binding.func, binding.connections]
docBinding = DocumentBinding.New arr, this
if binding.updateOnCreate
docBinding.update()
return
render: ->
oldTarget =
oldRoot =
= .context
= .root
if oldTarget isnt
.emit()
if oldRoot isnt
.emit()
return
revert: ->
return
clone: (original, file) ->
node = original.node.getCopiedElement , file.node
new Input 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_TEXT] =
arr[JSON_BINDING] =
arr
= require('./input/text.coffee') File, @
= require('./input/attr.coffee') File, @