/
Store.js
159 lines (129 loc) · 4.51 KB
/
Store.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2009 University of Toronto
Copyright 2011-2013 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
// Declare dependencies
/*global fluid_1_5:true, jQuery*/
// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_5 = fluid_1_5 || {};
(function ($, fluid) {
/**
* A Generic data source grade that defines an API for getting and setting
* data.
*/
fluid.defaults("fluid.uiOptions.dataSource", {
gradeNames: ["fluid.littleComponent"],
invokers: {
get: "fluid.uiOptions.dataSource.get",
set: "fluid.uiOptions.dataSource.set"
}
});
/****************
* Cookie Store *
****************/
/**
* SettingsStore Subcomponent that uses a cookie for persistence.
* @param {Object} options
*/
fluid.defaults("fluid.cookieStore", {
gradeNames: ["fluid.uiOptions.dataSource", "autoInit"],
cookie: {
name: "fluid-ui-settings",
path: "/",
expires: ""
}
});
fluid.demands("fluid.uiOptions.dataSource.get", "fluid.cookieStore", {
funcName: "fluid.cookieStore.get",
args: "{that}.options.cookie.name"
});
fluid.demands("fluid.uiOptions.dataSource.set", "fluid.cookieStore", {
funcName: "fluid.cookieStore.set",
args: ["{arguments}.0", "{that}.options.cookie"]
});
/**
* Retrieve and return the value of the cookie
*/
fluid.cookieStore.get = function (cookieName) {
var cookie = document.cookie;
if (cookie.length <= 0) {
return;
}
var cookiePrefix = cookieName + "=";
var startIndex = cookie.indexOf(cookiePrefix);
if (startIndex < 0) {
return;
}
startIndex = startIndex + cookiePrefix.length;
var endIndex = cookie.indexOf(";", startIndex);
if (endIndex < startIndex) {
endIndex = cookie.length;
}
var retObj = JSON.parse(decodeURIComponent(cookie.substring(startIndex, endIndex)));
return retObj;
};
/**
* Assembles the cookie string
* @param {Object} cookie settings
*/
fluid.cookieStore.assembleCookie = function (cookieOptions) {
var cookieStr = cookieOptions.name + "=" + cookieOptions.data;
if (cookieOptions.expires) {
cookieStr += "; expires=" + cookieOptions.expires;
}
if (cookieOptions.path) {
cookieStr += "; path=" + cookieOptions.path;
}
return cookieStr;
};
/**
* Saves the settings into a cookie
* @param {Object} settings
* @param {Object} cookieOptions
*/
fluid.cookieStore.set = function (settings, cookieOptions) {
cookieOptions.data = encodeURIComponent(JSON.stringify(settings));
document.cookie = fluid.cookieStore.assembleCookie(cookieOptions);
};
/**************
* Temp Store *
**************/
/**
* SettingsStore Subcomponent that doesn't do persistence.
* @param {Object} options
*/
fluid.defaults("fluid.tempStore", {
gradeNames: ["fluid.uiOptions.dataSource", "fluid.modelComponent", "autoInit"]
});
fluid.demands("fluid.uiOptions.dataSource.get", "fluid.tempStore", {
funcName: "fluid.identity",
args: "{that}.model"
});
fluid.demands("fluid.uiOptions.dataSource.set", "fluid.tempStore", {
funcName: "fluid.tempStore.set",
args: ["{arguments}.0", "{that}.applier"]
});
fluid.tempStore.set = function (settings, applier) {
applier.requestChange("", settings);
};
fluid.defaults("fluid.globalSettingsStore", {
gradeNames: ["autoInit", "fluid.littleComponent"],
components: {
settingsStore: {
type: "fluid.uiOptions.store"
}
}
});
fluid.globalSettingsStore.finalInit = function (that) {
fluid.staticEnvironment.settingsStore = that.settingsStore;
};
fluid.demands("fluid.uiOptions.store", ["fluid.globalSettingsStore"], {
funcName: "fluid.cookieStore"
});
})(jQuery, fluid_1_5);