Use $$ -> to render ad-hoc document fragments

Also eliminate stdlib/template directory which held code related to
SpacePen's precursor framework.
This commit is contained in:
Nathan Sobo
2012-02-06 16:19:35 -07:00
parent c712f598e5
commit bb640dd342
9 changed files with 48 additions and 238 deletions

View File

@@ -1,4 +1,4 @@
{View} = require 'space-pen'
{View, $$} = require 'space-pen'
Buffer = require 'buffer'
Point = require 'point'
Cursor = require 'cursor'
@@ -7,7 +7,6 @@ Highlighter = require 'highlighter'
Range = require 'range'
$ = require 'jquery'
$$ = require 'template/builder'
_ = require 'underscore'
module.exports =
@@ -108,13 +107,14 @@ class Editor extends View
buildLineElement: (row) ->
tokens = @highlighter.tokensForRow(row)
$$.pre class: 'line', ->
if tokens.length
for token in tokens
classes = token.type.split('.').map((c) -> "ace_#{c}").join(' ')
@span { class: token.type.replace('.', ' ') }, token.value
else
@raw ' '
$$ ->
@pre class: 'line', =>
if tokens.length
for token in tokens
classes = token.type.split('.').map((c) -> "ace_#{c}").join(' ')
@span { class: token.type.replace('.', ' ') }, token.value
else
@raw ' '
setBuffer: (@buffer) ->
@highlighter = new Highlighter(@buffer)

View File

@@ -1,7 +1,6 @@
Cursor = require 'cursor'
Range = require 'range'
{View} = require 'space-pen'
$$ = require 'template/builder'
{View, $$} = require 'space-pen'
module.exports =
class Selection extends View
@@ -52,7 +51,7 @@ class Selection extends View
else
css.right = 0
region = $$.div(class: 'selection').css(css)
region = ($$ -> @div class: 'selection').css(css)
@append(region)
@regions.push(region)

View File

@@ -1,105 +0,0 @@
_ = require 'underscore'
$ = require 'jquery'
OpenTag = require 'template/open-tag'
CloseTag = require 'template/close-tag'
Text = require 'template/text'
module.exports =
class Builder
@render: (fn) ->
builder = new this
fn.call(builder)
builder.toFragment()
@elements:
normal: 'a abbr address article aside audio b bdi bdo blockquote body button
canvas caption cite code colgroup datalist dd del details dfn div dl dt em
fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup
html i iframe ins kbd label legend li map mark menu meter nav noscript object
ol optgroup option output p pre progress q rp rt ruby s samp script section
select small span strong style sub summary sup table tbody td textarea tfoot
th thead time title tr u ul video'.split /\s+/
void: 'area base br col command embed hr img input keygen link meta param
source track wbr'.split /\s+/
@allElements: ->
@elements.normal.concat(@elements.void)
@buildTagClassMethod: (tagName) ->
this[tagName] = (args...) ->
@render ->
argsWithBoundFunctions = args.map (arg) =>
if _.isFunction(arg)
_.bind(arg, this)
else
arg
@tag(tagName, argsWithBoundFunctions...)
@buildTagInstanceMethod: (tagName) ->
@prototype[tagName] = (args...) -> @tag(tagName, args...)
@allElements().forEach (tagName) => @buildTagClassMethod(tagName)
@allElements().forEach (tagName) => @buildTagInstanceMethod(tagName)
constructor: ->
@reset()
toHtml: ->
_.map(@document, (x) -> x.toHtml()).join('')
toFragment: ->
fragment = $(@toHtml())
@wireOutlets fragment
fragment.find('*').andSelf().data('view', fragment)
fn(fragment) for fn in @postProcessingFns
fragment
tag: (name, args...) ->
options = @extractOptions(args)
@document.push(new OpenTag(name, options.attributes))
if @elementIsVoid(name)
if (options.text? or options.content?)
throw new Error("Self-closing tag #{name} cannot have text or content")
else
options.content?()
@text(options.text) if options.text
@document.push(new CloseTag(name))
subview: (outletName, subview) ->
subviewId = _.uniqueId('subview')
@tag 'div', id: subviewId
@postProcessingFns.push (view) ->
view[outletName] = subview
subview.parentView = view
view.find("div##{subviewId}").replaceWith(subview)
elementIsVoid: (name) ->
name in @constructor.elements.void
extractOptions: (args) ->
options = {}
for arg in args
options.content = arg if _.isFunction(arg)
options.text = arg if _.isString(arg)
options.text = arg.toString() if _.isNumber(arg)
options.attributes = arg if _.isObject(arg) and not _.isFunction(arg)
options
text: (string) ->
@document.push(new Text(string))
raw: (string) ->
@document.push(new Text(string, true))
wireOutlets: (view) ->
view.find('[outlet]').each ->
elt = $(this)
outletName = elt.attr('outlet')
view[outletName] = elt
reset: ->
@document = []
@postProcessingFns = []

View File

@@ -1,7 +0,0 @@
module.exports =
class CloseTag
constructor: (@name) ->
toHtml: ->
"</#{@name}>"

View File

@@ -1,12 +0,0 @@
_ = require 'underscore'
module.exports =
class OpenTag
constructor: (@name, @attributes) ->
toHtml: ->
"<#{@name}#{@attributesHtml()}>"
attributesHtml: ->
s = _.map(@attributes, (value, key) -> "#{key}=\"#{value}\"").join(' ')
if s == "" then "" else " " + s

View File

@@ -1,15 +0,0 @@
module.exports =
class Text
constructor: (@string, @raw=false) ->
toHtml: ->
if @raw
@string
else
@string
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')