doc.coffee 2.9 KB

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