-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js.coffee
More file actions
99 lines (81 loc) · 2.51 KB
/
store.js.coffee
File metadata and controls
99 lines (81 loc) · 2.51 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
class SM.Store
# manages locally stored items through store.js
# https://github.com/marcuswestin/store.js/
constructor: () ->
@this = this
@prefix = "SM://"
@numListViewsToStore = 1
@numObjectsToStore = 10
@bind()
@attemptMade = false
@lastListViewModel
@lastListViewData
bind: =>
$(document).ready =>
@storeObject(SM.currentObject) if SM.currentObject
$(document.body).on SM.events.click, '#user-menu-clear-cache', (e) =>
e.preventDefault()
SM.message.showConfirm("Clear locally stored data?", @clearConfirmed, @clearCancelled)
clearConfirmed: =>
store.clear()
SM.menu.doClose()
clearCancelled: =>
SM.menu.doClose()
set: (name, value) =>
store.set(@prefix + name, value)
get: (name) =>
store.get(@prefix + name)
attemptingListView: (model,data) =>
@lastListViewModel = model
@lastListViewData = data
@attemptMade = true
successfulListView: (results) =>
if @attemptMade
if @lastListViewData.split("&search=")[1]!=''
@storeListView(@lastListViewModel,@lastListViewData,results)
SM.savedViews.viewChanged()
@attemptMade = false
return true
storeListView: (model,data,results) =>
key = "listview/#{model}"
val = {model:model,data:data,results:results,dt:new Date()}
arr = @get(key)
arr = [] if (!arr)
arr = @updateArray(arr, val, @numListViewsToStore)
@set(key,arr)
return
getListViews: (model) =>
key = "listview/#{model}"
return @get(key)
storeObject: (data) =>
return if (SM.currentAccount.settings.no_recents_on_ios && SM.ios)
standardised = SM.standardise(Object.keys(data)[0])
pluralised = SM.pluralise("",standardised)
key = "object/#{pluralised}"
val = {data:data,dt:new Date()}
arr = @get(key)
arr = [] if (!arr)
arr = @updateArray(arr, val, @numObjectsToStore)
@set(key,arr)
return
getObjects: (model) =>
key = "object/#{model}"
return @get(key)
clearObjects: (model) =>
key = "object/#{model}"
@set(key,"")
updateArray: (arr, val, size) =>
for i, index in arr
if typeof(val.data) is "object" && typeof(i.data) is "object"
if i.data[Object.keys(i.data)[0]].id is val.data[Object.keys(val.data)[0]].id
i.data = $.extend(true, {}, val.data);
arr.splice(index,1)
break
else
if i.data == val.data
i.data = val.data
arr.splice(index,1)
break
arr.unshift(val)
arr = arr.slice(0,size)
return arr