Update image list to fetch images from GitHub

This commit is contained in:
Andrew Miner
2015-06-07 14:13:30 -07:00
parent dd760018df
commit 2227e81b8b
10 changed files with 118 additions and 19 deletions
+35 -6
View File
@@ -6,26 +6,39 @@ All rights reserved.
###
BaseModel = require './base_model'
_ = require 'underscore'
{Event} = require '../constants'
########################################################################################################################
module.exports = class MarkdownImage extends BaseModel
@MIMETYPES = [
{patern:/\.png$/i, text:'image/png'}
{patern:/\.gif$/i, text:'image/gif'}
{patern:/\.jpg$/i, text:'image/jpeg'}
{patern:/\.jpeg$/i, text:'image/jpeg'}
@MimeTypes = MimeTypes = [
{pattern:/\.png$/i, text:'image/png'}
{pattern:/\.gif$/i, text:'image/gif'}
{pattern:/\.jpg$/i, text:'image/jpeg'}
{pattern:/\.jpeg$/i, text:'image/jpeg'}
]
@Status = Status = {
'unknown': 'unknown'
'checking': 'checking'
'empty': 'empty'
'creatable': 'creatable'
'unchanged': 'unchanged'
'updateable': 'updatable'
}
constructor: (attributes={}, options={})->
attributes.encodedData ?= null
attributes.fileName ?= ''
attributes.path ?= ''
attributes.sha ?= null
attributes.status ?= Status.unknown
super
@client = options.client
@on Event.change + ':fileName', => @_mimeType = null
# Property Methods #############################################################################
@@ -35,7 +48,7 @@ module.exports = class MarkdownImage extends BaseModel
getMimeType: ->
if not @_mimeType?
for mimeType in MarkdownImage.MIMETYPES
for mimeType in MimeTypes
if mimeType.pattern.test @fileName
@_mimeType = mimeType.text
break
@@ -44,3 +57,19 @@ module.exports = class MarkdownImage extends BaseModel
Object.defineProperties @prototype,
fullPath: {get:@prototype.getFullPath}
mimeType: {get:@prototype.getMimeType}
# BaseModel Overrides ##########################################################################
fetch: ->
if not @client? then throw new Error 'MarkdownImage must be given a client to fetch with'
@status = Status.checking
@client.fetchFile path:@fullPath
.then (response)=>
data = response.json.data
if data.sha?
@set encodedData:data.content, sha:data.sha, status:Status.unchanged
else
@status = Status.empty
+14 -1
View File
@@ -14,14 +14,27 @@ MarkdownImage = require './markdown_image'
module.exports = class MarkdownImageList extends BaseModel
constructor: (attributes={}, options={})->
attributes.imageBase ?= ''
attributes.markdownText ?= null
super attributes, options
@client = options.client
@_images = {}
@on Event.change + ':imageBase', =>
for fileName, image of @_images
image.path = @imageBase
@on Event.change + ':markdownText', => @_analyzeMarkdownText()
@_analyzeMarkdownText()
# Public Methods ###############################################################################
loadImages: ->
for fileName, image of @_images
if image.status is MarkdownImage.Status.unknown
image.fetch()
# Property Methods #############################################################################
getAll: ->
@@ -50,7 +63,7 @@ module.exports = class MarkdownImageList extends BaseModel
fileName = match[2]
image = @_images[fileName]
if not image?
image = new MarkdownImage fileName:fileName
image = new MarkdownImage {fileName:fileName, path:@imageBase}, {client:@client}
@trigger Event.add, this, image
changed = true