Stackedit/res/extensions/buttonSync.js

90 lines
2.6 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-08-22 00:19:59 +00:00
"crel",
2013-06-10 22:32:34 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
"text!html/buttonSyncSettingsBlock.html",
2013-08-22 00:19:59 +00:00
], function($, _, crel, utils, Extension, 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
};
2013-07-21 14:38:53 +00:00
var synchronizer = undefined;
buttonSync.onSynchronizerCreated = function(synchronizerParameter) {
synchronizer = synchronizerParameter;
};
2013-08-22 00:19:59 +00:00
var $button = undefined;
2013-05-29 19:55:23 +00:00
var syncRunning = false;
var isOffline = false;
// Enable/disable the button
var updateButtonState = function() {
2013-08-22 00:19:59 +00:00
if($button === undefined) {
2013-05-29 19:55:23 +00:00
return;
}
2013-07-21 14:38:53 +00:00
if(syncRunning === true || synchronizer.hasSync() === false || isOffline) {
2013-08-22 00:19:59 +00:00
$button.addClass("disabled");
2013-05-29 19:55:23 +00:00
}
else {
2013-08-22 00:19:59 +00:00
$button.removeClass("disabled");
2013-05-29 19:55:23 +00:00
}
};
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-08-22 00:19:59 +00:00
var button = crel('button', {
2013-09-03 10:37:59 +00:00
class: 'btn btn-success',
title: 'Synchronize all'
2013-08-22 00:19:59 +00:00
}, crel('i', {
class: 'icon-refresh'
}));
$button = $(button).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
}
});
2013-08-22 00:19:59 +00:00
return button;
2013-05-29 19:55:23 +00:00
};
buttonSync.onReady = updateButtonState;
2013-07-21 14:38:53 +00:00
buttonSync.onFileCreated = updateButtonState;
buttonSync.onFileDeleted = updateButtonState;
buttonSync.onSyncImportSuccess = updateButtonState;
buttonSync.onSyncExportSuccess = updateButtonState;
buttonSync.onSyncRemoved = updateButtonState;
2013-05-29 19:55:23 +00:00
buttonSync.onSyncRunning = function(isRunning) {
syncRunning = isRunning;
updateButtonState();
};
buttonSync.onOfflineChanged = function(isOfflineParameter) {
isOffline = isOfflineParameter;
updateButtonState();
};
return buttonSync;
2013-05-27 19:45:33 +00:00
});