Skip to content

Commit a2bab68

Browse files
author
Sebastian Kippe
authored
Merge pull request #79 from remotestorage/feature/skip_initial
Add config option for skipping the initial state
2 parents 5f10e9b + 9dc9caa commit a2bab68

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

demo/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
remoteStorage.access.claim('bookmarks', 'rw');
2828
remoteStorage.caching.enable('/bookmarks/');
2929

30-
var widget = new Widget(remoteStorage, { logging: true });
30+
var widget = new Widget(remoteStorage, {
31+
//leaveOpen: true,
32+
//autoCloseAfter: 2000,
33+
// skipInitial: true,
34+
logging: true
35+
});
3136
widget.attach();
3237
</script>
3338
</body>

src/widget.js

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
* RemoteStorage connect widget
33
* @constructor
44
*
5-
* @param {object} remoteStorage - remoteStorage instance
6-
* @param {object} options - Widget options
7-
* @param {boolean} options.leaveOpen - Do not minimize widget when user clicks
8-
* outside of it (default: false)
9-
* @param {number} options.autoCloseAfter - Time after which the widget closes
10-
* automatically in ms (default: 1500)
11-
* @param {boolean} options.logging - Enable logging (default: false)
5+
* @param {object} remoteStorage - remoteStorage instance
6+
* @param {object} options - Widget options
7+
* @param {boolean} options.leaveOpen - Do not minimize widget when user clicks
8+
* outside of it (default: false)
9+
* @param {number} options.autoCloseAfter - Time after which the widget closes
10+
* automatically in ms (default: 1500)
11+
* @param {boolean} options.skipInitial - Don't show the initial connect hint,
12+
* but show sign-in screen directly instead
13+
* (default: false)
14+
* @param {boolean} options.logging - Enable logging (default: false)
1215
*/
1316
let Widget = function(remoteStorage, options={}) {
1417
this.rs = remoteStorage;
1518

16-
this.state = 'initial';
19+
this.leaveOpen = options.leaveOpen ? options.leaveOpen : false;
20+
this.autoCloseAfter = options.autoCloseAfter ? options.autoCloseAfter : 1500;
21+
this.skipInitial = options.skipInitial ? options.skipInitial : false;
22+
this.logging = options.logging ? options.logging : false;
1723

1824
// true if we have remoteStorage connection's info
1925
this.active = false;
@@ -24,17 +30,10 @@ let Widget = function(remoteStorage, options={}) {
2430
// widget is minimized ?
2531
this.closed = false;
2632

27-
this.leaveOpen = options.leaveOpen ? options.leaveOpen : false;
28-
29-
this.logging = typeof options.logging === 'boolean' ? options.logging : false;
30-
31-
this.autoCloseAfter = options.autoCloseAfter ? options.autoCloseAfter : 1500;
32-
3333
this.lastSynced = null;
3434
this.lastSyncedUpdateLoop = null;
3535
};
3636

37-
3837
Widget.prototype = {
3938

4039
log (...msg) {
@@ -73,7 +72,7 @@ Widget.prototype = {
7372
this.active = false;
7473
this.setOnline();
7574
this.setBackendClass(); // removes all backend CSS classes
76-
this.setState('initial');
75+
this.setInitialState();
7776
break;
7877
case 'connected':
7978
this.active = true;
@@ -135,6 +134,20 @@ Widget.prototype = {
135134
}
136135
},
137136

137+
138+
/**
139+
* Set widget to its inital state
140+
*
141+
* @private
142+
*/
143+
setInitialState () {
144+
if (this.skipInitial) {
145+
this.showChooseOrSignIn();
146+
} else {
147+
this.setState('initial');
148+
}
149+
},
150+
138151
/**
139152
* Create the widget element and add styling.
140153
*
@@ -166,7 +179,7 @@ Widget.prototype = {
166179
this.rsConnected = document.querySelector('.rs-box-connected');
167180
this.rsSignIn = document.querySelector('.rs-box-sign-in');
168181

169-
this.rsConnectedLabel = document.querySelector('.rs-box-connected .rs-sub-headline')
182+
this.rsConnectedLabel = document.querySelector('.rs-box-connected .rs-sub-headline');
170183
this.rsChooseRemoteStorageButton = document.querySelector('button.rs-choose-rs');
171184
this.rsChooseDropboxButton = document.querySelector('button.rs-choose-dropbox');
172185
this.rsChooseGoogleDriveButton = document.querySelector('button.rs-choose-googledrive');
@@ -234,6 +247,7 @@ Widget.prototype = {
234247

235248
this.setupElements();
236249
this.setupHandlers();
250+
this.setInitialState();
237251
},
238252

239253
setEventListeners () {
@@ -343,12 +357,13 @@ Widget.prototype = {
343357
if (!this.leaveOpen && this.active) {
344358
this.closed = true;
345359
this.rsWidget.classList.add('rs-closed');
360+
} else if (this.active) {
361+
this.setState('connected');
346362
} else {
347-
this.setState(this.active ? 'connected' : 'initial');
363+
this.setInitialState();
348364
}
349365
},
350366

351-
352367
/**
353368
* Mark the widget as offline.
354369
*
@@ -432,7 +447,7 @@ Widget.prototype = {
432447
},
433448

434449
updateLastSyncedOutput () {
435-
if (!this.lastSynced) { return } // don't do anything when we've never synced yet
450+
if (!this.lastSynced) { return; } // don't do anything when we've never synced yet
436451
let now = new Date();
437452
let secondsSinceLastSync = Math.round((now.getTime() - this.lastSynced.getTime())/1000);
438453
let subHeadlineEl = document.querySelector('.rs-box-connected .rs-sub-headline');

0 commit comments

Comments
 (0)