1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/examples/documents.coffee

73 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2009-12-13 17:07:16 -05:00
# Document Model
dc.model.Document: dc.Model.extend({
2009-12-30 18:28:16 -05:00
constructor: attributes => this.base(attributes)
2009-12-13 20:29:44 -05:00
# For display, show either the highlighted search results, or the summary,
# if no highlights are available.
# The import process will take care of this in the future, but the inline
# version of the summary has all runs of whitespace squeezed out.
displaySummary: =>
2009-12-14 10:00:31 -05:00
text: this.get('highlight') or this.get('summary') or ''
2009-12-30 18:28:16 -05:00
text and text.replace(/\s+/g, ' ')
2009-12-13 20:29:44 -05:00
# Return a list of the document's metadata. Think about caching this on the
# document by binding to Metadata, instead of on-the-fly.
metadata: =>
docId: this.id
2009-12-30 18:28:16 -05:00
_.select(Metadata.models(), (meta =>
_.any(meta.get('instances'), instance =>
instance.document_id is docId)))
2009-12-13 20:29:44 -05:00
bookmark: pageNumber =>
bookmark: new dc.model.Bookmark({title: this.get('title'), page_number: pageNumber, document_id: this.id})
2009-12-30 18:28:16 -05:00
Bookmarks.create(bookmark)
2009-12-13 20:29:44 -05:00
# Inspect.
2009-12-30 18:28:16 -05:00
toString: => 'Document ' + this.id + ' "' + this.get('title') + '"'
2009-12-13 20:29:44 -05:00
})
# Document Set
dc.model.DocumentSet: dc.model.RESTfulSet.extend({
resource: 'documents'
SELECTION_CHANGED: 'documents:selection_changed'
constructor: options =>
this.base(options)
2009-12-30 18:28:16 -05:00
_.bindAll(this, 'downloadSelectedViewers', 'downloadSelectedPDF', 'downloadSelectedFullText')
2009-12-13 20:29:44 -05:00
2009-12-30 18:28:16 -05:00
selected: => _.select(this.models(), m => m.get('selected'))
2009-12-13 20:29:44 -05:00
2009-12-30 18:28:16 -05:00
selectedIds: => _.pluck(this.selected(), 'id')
2009-12-13 20:29:44 -05:00
2009-12-30 18:28:16 -05:00
countSelected: => this.selected().length
2009-12-13 20:29:44 -05:00
downloadSelectedViewers: =>
2009-12-30 18:28:16 -05:00
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_viewer.zip')
2009-12-13 20:29:44 -05:00
downloadSelectedPDF: =>
2009-12-30 18:28:16 -05:00
if this.countSelected() <= 1 then return window.open(this.selected()[0].get('pdf_url'))
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_pdfs.zip')
2009-12-13 20:29:44 -05:00
downloadSelectedFullText: =>
2009-12-30 18:28:16 -05:00
if this.countSelected() <= 1 then return window.open(this.selected()[0].get('full_text_url'))
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_text.zip')
2009-12-13 20:29:44 -05:00
# We override "_onModelEvent" to fire selection changed events when documents
# change their selected state.
_onModelEvent: e, model =>
this.base(e, model)
fire: e == dc.Model.CHANGED and model.hasChanged('selected')
2009-12-30 18:28:16 -05:00
if fire then _.defer(_(this.fire).bind(this, this.SELECTION_CHANGED, this))
2009-12-13 17:07:16 -05:00
})
2009-12-13 20:29:44 -05:00
# The main set of Documents, used by the search tab.
window.Documents: new dc.model.DocumentSet()
# The set of documents that is used to look at a particular label.
dc.app.LabeledDocuments: new dc.model.DocumentSet()