Skip to content
This repository was archived by the owner on Jun 8, 2020. It is now read-only.

Commit bb2f35a

Browse files
author
Samy Pessé
committed
Add settings sync with server and update HappyRhino version
1 parent 02f8e50 commit bb2f35a

27 files changed

+417
-254
lines changed

client/collections/reports.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
define([
2+
"hr/hr",
3+
"models/report"
4+
], function(hr, Report) {
5+
var Reports = hr.Collection.extend({
6+
model: Report,
7+
});
8+
9+
return Reports;
10+
});

client/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ require([
5252

5353
this.user = User.current;
5454
this.user.models = new EventModels();
55-
this.user.on("change", this.render, this);
55+
this.user.on("change:token", this.render, this);
5656

5757
return this;
5858
},
@@ -62,7 +62,7 @@ require([
6262
*/
6363
templateContext: function() {
6464
return {
65-
user: this.user
65+
'user': this.user
6666
}
6767
},
6868

client/models/report.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
define([
2+
"hr/hr",
3+
"Underscore"
4+
], function(hr, _) {
5+
var Report = hr.Model.extend({
6+
defaults: {
7+
'id': null,
8+
'event': null,
9+
'namespace': null,
10+
'settings': {}
11+
},
12+
13+
/*
14+
* Constructor
15+
*/
16+
initialize: function() {
17+
Report.__super__.initialize.apply(this, arguments);
18+
19+
this.set("id", this.get("id") || _.uniqueId("report_"));
20+
return this;
21+
}
22+
});
23+
24+
return Report;
25+
});

client/models/user.js

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
define([
22
"hr/hr",
33
"api",
4-
"notifications"
5-
], function(hr, api, notifications) {
4+
"notifications",
5+
"collections/reports"
6+
], function(hr, api, notifications, Reports) {
67
var User = hr.Model.extend({
78
defaults: {
89
'email': hr.Storage.get("email", ""),
9-
'token': hr.Storage.get("token", "")
10+
'token': hr.Storage.get("token", ""),
11+
'settings': hr.Storage.get("settings") || {}
1012
},
1113

1214
/*
@@ -15,14 +17,30 @@ define([
1517
initialize: function() {
1618
User.__super__.initialize.apply(this, arguments);
1719

18-
// User change
19-
this.on("change", function() {
20-
hr.Storage.set("email", this.get("email", ""));
21-
hr.Storage.set("token", this.get("token", ""));
20+
// Reports list
21+
this.reports = new Reports();
22+
this.reports.on("add remove set", function() {
23+
var settings = _.extend({}, this.get("settings", {}), {
24+
'reports': this.reports.toJSON()
25+
});
26+
27+
this.set("settings", settings);
28+
}, this);
2229

30+
// User change
31+
this.on("change:token", function() {
2332
this.connectNotifications();
2433
}, this);
34+
35+
this.on("set", _.throttle(this.syncSettings, 1000), this);
2536

37+
if (this.isAuth()) {
38+
console.log("user is logged");
39+
this.syncSettings({
40+
'updateReports': true
41+
});
42+
}
43+
2644
this.connectNotifications();
2745
return this;
2846
},
@@ -57,6 +75,8 @@ define([
5775
'password': password
5876
}).done(function(data) {
5977
that.set(data);
78+
this.syncLocal();
79+
this.reports.reset(this.get("settings.reports", []));
6080
});
6181
},
6282

@@ -79,6 +99,7 @@ define([
7999
* Log out the user
80100
*/
81101
logout: function() {
102+
if (!this.isAuth()) return this;
82103
hr.Storage.clear();
83104
this.set({
84105
'email': null,
@@ -87,73 +108,42 @@ define([
87108
return this;
88109
},
89110

90-
/*
91-
* Key/Value storage for user settings
92-
*
93-
* TODO: add sync with server
94-
*/
95-
96-
getSettings: function(key) {
97-
return hr.Storage.get(this.get("email")+"/"+key);
98-
},
99-
setSettings: function(key, value) {
100-
hr.Storage.set(this.get("email")+"/"+key, value);
101-
this.trigger("settings.change."+key, key);
102-
this.syncSettings();
103-
return this;
104-
},
105111

106112
/*
107113
* Sync settings
108114
*/
109-
syncSettings: function() {
110-
111-
},
112-
113-
114-
/*
115-
* Get list reports
116-
*/
117-
reports: function() {
118-
var reports = this.getSettings("reports") || [];
119-
return _.reduce(reports, function(memo, report) {
120-
if (_.isString(report)) {
121-
memo.push({
122-
'id': _.uniqueId('report_'),
123-
'report': report
124-
});
125-
} else if (_.isObject(report)) {
126-
memo.push(report);
115+
syncSettings: function(options) {
116+
var that = this;
117+
if (!this.isAuth()) return this;
118+
119+
// options
120+
options = _.defaults(options || {}, {
121+
'updateReports': false
122+
})
123+
124+
// Sync with server
125+
return api.request("post", this.get("token")+"/account/sync", {
126+
'settings': this.get("settings", {})
127+
}).done(function(data) {
128+
// Update user
129+
that.set(data, {
130+
silent: true
131+
});
132+
133+
// Update reports
134+
if (options.updateReports) {
135+
that.reports.reset(that.get("settings.reports", []));
127136
}
128-
return memo;
129-
}, []);
130-
},
131137

132-
/*
133-
* Add a new report
134-
*/
135-
addReport: function(report, reportId) {
136-
var reports = this.reports();
137-
reports.push({
138-
'id': reportId || _.uniqueId('report_'),
139-
'report': report
138+
that.syncLocal();
140139
});
141-
this.setSettings("reports", reports);
142-
return this;
143140
},
144141

145-
/*
146-
* Remove a report
147-
*/
148-
removeReport: function(reportId) {
149-
var reports = this.reports();
150-
_.each(reports, function(report, i) {
151-
if (report.id == reportId) {
152-
delete reports[i];
153-
}
154-
});
155-
this.setSettings("reports", reports);
156-
return this;
142+
syncLocal: function() {
143+
// Sync in localStorage
144+
hr.Storage.set("email", this.get("email", ""));
145+
hr.Storage.set("token", this.get("token", ""));
146+
hr.Storage.set("settings", this.get("settings", {}));
157147
}
158148
}, {
159149
current: null

client/resources/templates/homepage.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ <h1>Reportr</h1>
3939
<a href="https://github.com/SamyPesse/reportr" class="pull-right">Contribute on GitHub</a>
4040
<p class="hidden-xs">This project is open source</p>
4141
</div>
42+
<svg xmlns="http://www.w3.org/2000/svg">
43+
<filter id="svg-blur">
44+
<feGaussianBlur stdDeviation="3"/>
45+
</filter>
46+
</svg>
4247
</div>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
<a href="#" class="action action-report-add"><i class="icon-plus"></i></a>
12
<img src="<%- object.icon() %>" />
2-
<%- object.get('name') %>
3-
<a href="#" class="action action-report-add"><i class="icon-plus"></i></a>
3+
<%- object.get('name') %>

client/resources/templates/report.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="report-inner">
22
<div class="report-header">
3-
<img src="<%- model.icon() %>" class="model-icon"/>
3+
<img src="<%- model.icon() %>" class="report-icon"/>
44

5-
<div class="btn-group pull-right">
5+
<div class="report-controls btn-group pull-right">
66
<!-- Actions -->
77
<a href="#" class="btn btn-default action-toggle-options"><i class="icon-cog"></i></a>
88
<a href="#" class="btn btn-default action-report-remove"><i class="icon-remove"></i></a>
@@ -20,12 +20,12 @@
2020
</ul>
2121
</div>
2222

23-
<h3 class="model-name"><%- model.get('name') %></h3>
23+
<h3 class="report-name"><%- model.get('name') %></h3>
2424
</div>
2525
<%= view.component("report."+visualization.id, {
2626
"report": report
2727
}, "visualization") %>
2828
<div class="report-footer">
29-
<p class="model-description"><%- model.get('description') %></p>
29+
<p class="report-description"><%- model.get('description') %></p>
3030
</div>
3131
</div>

client/resources/templates/reports.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
<div class="reports-list">
2-
<% _.each(reports, function(report) { %>
3-
<%= view.component("report", {
4-
"report": report
5-
}) %>
6-
<% }); %>
7-
</div>
1+
<div class="reports-list-outer"></div>
82
<div class="settings">
93
<form class="form-horizontal" role="form">
104
<fieldset>

client/stylesheets/dashboard.less

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@
5959
/* Models */
6060
.models {
6161
position: absolute;
62-
right: 10px;
63-
left: 10px;
62+
right: 0px;
63+
left: 0px;
6464
bottom: 50px;
6565
top: 60px;
66+
overflow-y: auto;
6667
}
6768
}
6869

client/stylesheets/homepage.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
right: 0px;
66
top: 0px;
77
background: #eee;
8+
9+
svg {
10+
display: none;
11+
}
812

913
#intro {
1014
position: relative;
@@ -28,6 +32,7 @@
2832
bottom: 0px;
2933
left: 0px;
3034
right: 0px;
35+
filter: url('#svg-blur');
3136
-webkit-filter: blur(@blur);
3237
-moz-filter: blur(@blur);
3338
-o-filter: blur(@blur);

0 commit comments

Comments
 (0)