Merge branch 'view'

This commit is contained in:
Nathan Sobo
2011-12-28 14:22:12 -06:00
13 changed files with 461 additions and 18 deletions

View File

@@ -0,0 +1,26 @@
$ = require 'jquery'
Template = require 'template'
stringScore = require 'stringscore'
module.exports =
class FileFinder extends Template
content: ->
@div class: 'file-finder', =>
@ol outlet: 'urlList'
@input outlet: 'input', keypress: 'populateUrlList'
viewProperties:
urls: null
initialize: ({@urls}) ->
populateUrlList: ->
@urlList.empty()
for url in @findMatches(@input.text())
@urlList.append $("<li>#{url}</li>")
findMatches: (query) ->
scoredUrls = ({url, score: stringScore(url, query)} for url in @urls)
sortedUrls = scoredUrls.sort (a, b) -> a.score > b.score
urlAndScore.url for urlAndScore in sortedUrls when urlAndScore.score > 0

View File

@@ -4,13 +4,13 @@ Template = require 'template'
module.exports =
class Layout extends Template
@attach: ->
view = @buildView()
view = @build()
$('body').append(view)
view
content: ->
link rel: 'stylesheet', href: 'static/atom.css'
div id: 'app-horizontal', ->
div id: 'app-vertical', ->
div id: 'main'
@link rel: 'stylesheet', href: 'static/atom.css'
@div id: 'app-horizontal', =>
@div id: 'app-vertical', =>
@div id: 'main'

View File

@@ -1,10 +1,43 @@
$ = require 'jquery'
coffeekup = require 'coffeekup'
_ = require 'underscore'
Builder = require 'template/builder'
module.exports =
class Template
@buildView: (attributes) ->
(new this).buildView(attributes)
@events: 'blur change click dblclick error focus keydown
keypress keyup load mousedown mousemove mouseout mouseover
mouseup resize scroll select submit unload'.split /\s+/
@buildTagMethod: (name) ->
this.prototype[name] = (args...) -> @builder.tag(name, args...)
@buildTagMethod(name) for name in Builder.elements.normal
@buildTagMethod(name) for name in Builder.elements.void
@build: (attributes) ->
(new this).build(attributes)
build: (attributes) ->
@builder = new Builder
@content(attributes)
view = @builder.toFragment()
@wireOutlets(view)
@bindEvents(view)
if @viewProperties
$.extend(view, @viewProperties)
view.initialize?(attributes)
view
wireOutlets: (view) ->
view.find('[outlet]').each ->
elt = $(this)
outletName = elt.attr('outlet')
view[outletName] = elt
bindEvents: (view) ->
for eventName in this.constructor.events
view.find("[#{eventName}]").each ->
elt = $(this)
methodName = elt.attr(eventName)
elt[eventName]((event) -> view[methodName](event, elt))
buildView: (attributes) ->
$(coffeekup.render(@content, attributes))

View File

@@ -0,0 +1,60 @@
_ = require 'underscore'
$ = require 'jquery'
OpenTag = require 'template/open-tag'
CloseTag = require 'template/close-tag'
Text = require 'template/text'
module.exports =
class Builder
@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+/
constructor: ->
@reset()
toHtml: ->
_.map(@document, (x) -> x.toHtml()).join('')
toFragment: ->
$(@toHtml())
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 #{tag} cannot have text or content")
else
options.content?()
@text(options.text) if options.text
@document.push(new CloseTag(name))
elementIsVoid: (name) ->
_.contains(this.constructor.elements.void, name)
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)
options
text: (string) ->
@document.push(new Text(string))
reset: ->
@document = []

View File

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

View File

@@ -0,0 +1,12 @@
_ = 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

@@ -0,0 +1,6 @@
module.exports =
class Text
constructor: (@string) ->
toHtml: -> @string