-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgiftwrap.coffee
More file actions
127 lines (112 loc) · 4.37 KB
/
Copy pathgiftwrap.coffee
File metadata and controls
127 lines (112 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
window.GW = {}
GW.fileThreads = {}
GW.fileQueue = {}
GW.Thread = (filename) ->
wait = false
setInterval ->
if !wait
fnObj = GW.fileQueue[filename].shift()
if fnObj?
wait = true
fnCallback = fnObj.params[fnObj.cbIndex]
fnObj.params[fnObj.cbIndex] = ->
wait = false
fnCallback.apply @, arguments
fnObj.fn.apply fnObj.context, fnObj.params
, 0
null
class GW.Present
###
# Creates a new Present, accepting an options hash.
#
# Parameters:
# size: the number of bytes to request via the FileSystem API. Defaults to
# 5 MiB
# fileName: The name of the file to write to. Defaults to object.json
# onReady: The function to call when the FileSystem API has granted the
# app permission for use.
# persistent: Whether or not to request persistent data from the API.
# Defaults to persistent.
# errorCallback: The callback to use on errors. Defaults to a console-
# logging callback
###
constructor: (options) ->
options = options || {}
options.size = if options.size? then options.size else 5 * 1024 * 1024
options.fileName = if options.fileName? then options.fileName else "object.json"
options.onReady = if options.onReady? then options.onReady else ->
options.persistent = if options.persistent then "PERSISTENT" else "TEMPORARY"
options.errorCallback = if options.errorCallback? then options.errorCallback else @_on_error
unless GW.fileThreads[options.fileName]?
GW.Thread options.fileName
GW.fileThreads[options.fileName] = true
unless GW.fileQueue[options.fileName]?
GW.fileQueue[options.fileName] = []
@_jsonObject = {}
@options = options
@retryRequest()
retryRequest: ->
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem
onQuotaReady = (grantedBytes) =>
window.requestFileSystem window[@options.persistent], grantedBytes, ((fs) =>
@_onGranted(fs)
), @options.errorCallback
if @options.persistent == "PERSISTENT"
window.webkitStorageInfo.requestQuota window.PERSISTENT, @options.size, onQuotaReady, @options.errorCallback
else
onQuotaReady(@options.size)
getObject: ->
@_jsonObject || {}
writeObject: (object, successCallback) ->
GW.fileQueue[@options.fileName].push
params: [object, successCallback]
cbIndex: 1
context: @
fn: @_writeObject
readObject: (successCallback) ->
GW.fileQueue[@options.fileName].push
params: [successCallback]
cbIndex: 0
context: @
fn: @_readObject
_writeObject: (object, successCallback) ->
if @fs?
@_jsonObject = if object? then object else @_jsonObject
successCallback = if successCallback? then successCallback else ->
fileWriterHandler = (fileWriter) =>
json_string = JSON.stringify(@_jsonObject)
blob = new Blob([json_string])
fileWriter.write blob
successCallback()
fileEntryHandler = (fileEntry) =>
fileEntry.createWriter fileWriterHandler, @_onError
@fs.root.getFile @options.fileName, {create: true}, (fe) =>
fe.remove =>
@fs.root.getFile @options.fileName, {create: true}, fileEntryHandler, @_onError
, @_onError
, @_onError
else
console.log "No filesystem space has been obtained for this object."
_readObject: (successCallback) ->
result = {}
fileHandler = (file) =>
reader = new FileReader()
reader.onloadend = (e) =>
if JSON then @_jsonObject = JSON.parse(e.target.result) else console.log "JSON parsing not supported"
successCallback(e)
reader.readAsText(file)
fileEntryHandler = (fileEntry) =>
fileEntry.file fileHandler, @_onError
@fs.root.getFile @options.fileName, {create: true}, fileEntryHandler, @onError
_onGranted: (fs) ->
@fs = fs
@options.onReady @
_onError: (e) ->
switch e.code
when FileError.QUOTA_EXCEEDED_ERR then msg = 'Local storage quota exceeded'
when FileError.NOT_FOUND_ERR then msg = 'Local storage file not found'
when FileError.SECURITY_ERR then msg = 'A security error occurred'
when FileError.INVALID_MODIFICATION_ERR then msg = 'You do not have permission to save results'
when FileError.INVALID_STATE_ERR then msg = 'Invalid state was encountered'
else msg = 'Unknown Error'
console.log "Error: #{msg}"