doc.js 3.2 KB

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