doc.coffee 2.7 KB

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