mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Merge branch 'view'
This commit is contained in:
26
src/atom/file-finder.coffee
Normal file
26
src/atom/file-finder.coffee
Normal 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
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
60
src/stdlib/template/builder.coffee
Normal file
60
src/stdlib/template/builder.coffee
Normal 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 = []
|
||||
|
||||
7
src/stdlib/template/close-tag.coffee
Normal file
7
src/stdlib/template/close-tag.coffee
Normal file
@@ -0,0 +1,7 @@
|
||||
module.exports =
|
||||
class CloseTag
|
||||
constructor: (@name) ->
|
||||
|
||||
toHtml: ->
|
||||
"</#{@name}>"
|
||||
|
||||
12
src/stdlib/template/open-tag.coffee
Normal file
12
src/stdlib/template/open-tag.coffee
Normal 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
|
||||
6
src/stdlib/template/text.coffee
Normal file
6
src/stdlib/template/text.coffee
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports =
|
||||
class Text
|
||||
constructor: (@string) ->
|
||||
|
||||
toHtml: -> @string
|
||||
|
||||
Reference in New Issue
Block a user