Stackedit/public/res/extensions/buttonSync.js

100 lines
3.1 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",
2013-10-12 19:13:29 +00:00
"mousetrap",
2013-06-22 23:48:57 +00:00
"text!html/buttonSyncSettingsBlock.html",
2013-10-12 19:13:29 +00:00
], function($, _, crel, utils, Extension, mousetrap, buttonSyncSettingsBlockHTML) {
2013-05-29 19:55:23 +00:00
2013-10-12 19:13:29 +00:00
var buttonSync = new Extension("buttonSync", 'Button "Synchronize"', false, true);
2013-06-22 23:48:57 +00:00
buttonSync.settingsBlock = buttonSyncSettingsBlockHTML;
buttonSync.defaultConfig = {
2013-10-12 19:13:29 +00:00
syncPeriod: 180000,
syncShortcut: 'mod+s'
2013-06-10 22:32:34 +00:00
};
buttonSync.onLoadSettings = function() {
utils.setInputValue("#input-sync-period", buttonSync.config.syncPeriod);
2013-10-12 19:13:29 +00:00
utils.setInputValue("#input-sync-shortcut", buttonSync.config.syncShortcut);
2013-06-10 22:32:34 +00:00
};
buttonSync.onSaveSettings = function(newConfig, event) {
2013-06-12 20:37:43 +00:00
newConfig.syncPeriod = utils.getInputIntValue("#input-sync-period", event, 0);
2013-10-12 19:13:29 +00:00
newConfig.syncShortcut = utils.getInputTextValue("#input-sync-shortcut", event);
2013-05-29 19:55:23 +00:00
};
2013-11-07 23:10:38 +00:00
var synchronizer;
2013-07-21 14:38:53 +00:00
buttonSync.onSynchronizerCreated = function(synchronizerParameter) {
synchronizer = synchronizerParameter;
};
2013-11-07 23:10:38 +00:00
var $button;
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() {
2013-10-12 19:13:29 +00:00
if(!buttonSync.config.syncPeriod || lastSync + buttonSync.config.syncPeriod > utils.currentTime) {
2013-06-10 22:32:34 +00:00
return;
}
2013-10-12 19:13:29 +00:00
synchronizer.sync() && (lastSync = utils.currentTime);
2013-06-10 22:32:34 +00:00
};
2013-05-29 19:55:23 +00:00
buttonSync.onCreateButton = function() {
2014-03-12 00:53:15 +00:00
var button = crel('a', {
2013-10-06 14:34:40 +00:00
class: 'btn btn-success button-synchronize',
2013-09-03 10:37:59 +00:00
title: 'Synchronize all'
2013-08-22 00:19:59 +00:00
}, crel('i', {
class: 'icon-refresh'
}));
2013-10-12 19:13:29 +00:00
$button = $(button);
$button.click(function() {
if(!$button.hasClass("disabled")) {
synchronizer.sync() && (lastSync = utils.currentTime);
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();
};
2013-10-12 19:13:29 +00:00
buttonSync.onReady = function() {
mousetrap.bind(buttonSync.config.syncShortcut, function(e) {
synchronizer.sync() && (lastSync = utils.currentTime);
e.preventDefault();
});
};
2013-05-29 19:55:23 +00:00
return buttonSync;
2013-05-27 19:45:33 +00:00
});