Stackedit/js/extensions/buttonSync.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
2013-06-10 21:22:32 +00:00
"underscore",
2013-06-10 22:32:34 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-10 21:22:32 +00:00
"text!html/buttonSync.html",
2013-06-22 23:48:57 +00:00
"text!html/buttonSyncSettingsBlock.html",
], function($, _, utils, Extension, buttonSyncHTML, buttonSyncSettingsBlockHTML) {
2013-05-29 19:55:23 +00:00
2013-06-22 23:48:57 +00:00
var buttonSync = new Extension("buttonSync", 'Button "Synchronize"');
buttonSync.settingsBlock = buttonSyncSettingsBlockHTML;
buttonSync.defaultConfig = {
syncPeriod: 180000
2013-06-10 22:32:34 +00:00
};
buttonSync.onLoadSettings = function() {
utils.setInputValue("#input-sync-period", buttonSync.config.syncPeriod);
};
buttonSync.onSaveSettings = function(newConfig, event) {
2013-06-12 20:37:43 +00:00
newConfig.syncPeriod = utils.getInputIntValue("#input-sync-period", event, 0);
2013-05-29 19:55:23 +00:00
};
var button = undefined;
var syncRunning = false;
var uploadPending = false;
var isOffline = false;
// Enable/disable the button
var updateButtonState = function() {
if(button === undefined) {
return;
}
if(syncRunning === true || uploadPending === false || isOffline) {
button.addClass("disabled");
}
else {
button.removeClass("disabled");
}
};
var synchronizer = undefined;
buttonSync.onSynchronizerCreated = function(synchronizerParameter) {
synchronizer = synchronizerParameter;
};
2013-06-10 22:32:34 +00:00
// Run sync periodically
var lastSync = 0;
buttonSync.onPeriodicRun = function() {
if(viewerMode === true || !buttonSync.config.syncPeriod || lastSync + buttonSync.config.syncPeriod > utils.currentTime) {
return;
}
if(synchronizer.sync() === true) {
lastSync = utils.currentTime;
}
};
2013-05-29 19:55:23 +00:00
buttonSync.onCreateButton = function() {
2013-06-10 21:22:32 +00:00
button = $(buttonSyncHTML).click(function() {
2013-05-29 19:55:23 +00:00
if(!$(this).hasClass("disabled")) {
2013-06-10 22:32:34 +00:00
synchronizer.sync();
2013-05-29 19:55:23 +00:00
}
});
return button;
};
buttonSync.onReady = updateButtonState;
buttonSync.onSyncRunning = function(isRunning) {
syncRunning = isRunning;
uploadPending = true;
updateButtonState();
};
buttonSync.onSyncSuccess = function() {
uploadPending = false;
updateButtonState();
};
buttonSync.onOfflineChanged = function(isOfflineParameter) {
isOffline = isOfflineParameter;
updateButtonState();
};
2013-07-18 23:30:28 +00:00
// Check that a file has synchronized locations and no real time synchronized location
2013-05-29 19:55:23 +00:00
var checkSynchronization = function(fileDesc) {
2013-07-18 23:30:28 +00:00
if(_.size(fileDesc.syncLocations) !== 0 && !_.some(fileDesc.syncLocations, function(syncAttributes) {
return syncAttributes.isRealtime;
})) {
2013-05-29 19:55:23 +00:00
uploadPending = true;
updateButtonState();
}
};
buttonSync.onContentChanged = checkSynchronization;
buttonSync.onTitleChanged = checkSynchronization;
return buttonSync;
2013-05-27 19:45:33 +00:00
});