doc.coffee 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. class app.models.Doc extends app.Model
  2. # Attributes: name, slug, type, version, index_path, db_path, db_size, mtime
  3. constructor: ->
  4. super
  5. @reset @
  6. reset: (data) ->
  7. @resetEntries data.entries
  8. @resetTypes data.types
  9. return
  10. resetEntries: (entries) ->
  11. @entries = new app.collections.Entries(entries)
  12. @entries.each (entry) => entry.doc = @
  13. return
  14. resetTypes: (types) ->
  15. @types = new app.collections.Types(types)
  16. @types.each (type) => type.doc = @
  17. return
  18. fullPath: (path = '') ->
  19. path = "/#{path}" unless path[0] is '/'
  20. "/#{@slug}#{path}"
  21. fileUrl: (path) ->
  22. "#{app.config.docs_host}#{@fullPath(path)}"
  23. dbUrl: ->
  24. "#{app.config.docs_host}/#{@db_path}?#{@mtime}"
  25. indexUrl: ->
  26. "#{app.indexHost()}/#{@index_path}?#{@mtime}"
  27. toEntry: ->
  28. new app.models.Entry
  29. doc: @
  30. name: @name
  31. path: 'index'
  32. findEntryByPathAndHash: (path, hash) ->
  33. if hash and entry = @entries.findBy 'path', "#{path}##{hash}"
  34. entry
  35. else if path is 'index'
  36. @toEntry()
  37. else
  38. @entries.findBy 'path', path
  39. load: (onSuccess, onError, options = {}) ->
  40. return if options.readCache and @_loadFromCache(onSuccess)
  41. callback = (data) =>
  42. @reset data
  43. onSuccess()
  44. @_setCache data if options.writeCache
  45. return
  46. ajax
  47. url: @indexUrl()
  48. success: callback
  49. error: onError
  50. clearCache: ->
  51. app.store.del @slug
  52. return
  53. _loadFromCache: (onSuccess) ->
  54. return unless data = @_getCache()
  55. callback = =>
  56. @reset data
  57. onSuccess()
  58. return
  59. setTimeout callback, 0
  60. true
  61. _getCache: ->
  62. return unless data = app.store.get @slug
  63. if data[0] is @mtime
  64. return data[1]
  65. else
  66. @clearCache()
  67. return
  68. _setCache: (data) ->
  69. app.store.set @slug, [@mtime, data]
  70. return
  71. install: (onSuccess, onError) ->
  72. return if @installing
  73. @installing = true
  74. error = =>
  75. @installing = null
  76. onError()
  77. return
  78. success = (data) =>
  79. @installing = null
  80. app.db.store @, data, onSuccess, error
  81. return
  82. ajax
  83. url: @dbUrl()
  84. success: success
  85. error: error
  86. return
  87. uninstall: (onSuccess, onError) ->
  88. return if @installing
  89. @installing = true
  90. success = =>
  91. @installing = null
  92. onSuccess()
  93. return
  94. error = =>
  95. @installing = null
  96. onError()
  97. return
  98. app.db.unstore @, success, error
  99. return
  100. getInstallStatus: (callback) ->
  101. app.db.version @, (value) ->
  102. callback installed: !!value, mtime: value
  103. return