Stackedit/js/extensions/buttonSync.js

99 lines
2.7 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-10 21:22:32 +00:00
"text!html/buttonSync.html",
2013-06-10 22:32:34 +00:00
"text!html/buttonSyncSettingsBloc.html",
], function($, _, utils, buttonSyncHTML, buttonSyncSettingsBlocHTML) {
2013-05-29 19:55:23 +00:00
var buttonSync = {
extensionId: "buttonSync",
extensionName: 'Button "Synchronize"',
2013-06-10 22:32:34 +00:00
defaultConfig: {
syncPeriod: 180000
},
settingsBloc: buttonSyncSettingsBlocHTML
};
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();
};
// Check that a file has synchronized locations
var checkSynchronization = function(fileDesc) {
if(_.size(fileDesc.syncLocations) !== 0) {
uploadPending = true;
updateButtonState();
}
};
buttonSync.onContentChanged = checkSynchronization;
buttonSync.onTitleChanged = checkSynchronization;
return buttonSync;
2013-05-27 19:45:33 +00:00
});