2018-07-26 21:39:40 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) Codiad, Kent Safranski (codiad.com), and Isaac Brown (telaaedifex.com), distributed
|
|
|
|
* as-is and without warranty under the MIT License. See
|
|
|
|
* [root]/license.txt for more. This information must remain intact.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function(global, $){
|
|
|
|
|
|
|
|
// Define core
|
|
|
|
var codiad = global.codiad,
|
2018-09-21 03:44:10 +02:00
|
|
|
scripts = document.getElementsByTagName('script'),
|
2018-07-26 21:39:40 +02:00
|
|
|
path = scripts[scripts.length-1].src.split('?')[0],
|
|
|
|
curpath = path.split('/').slice(0, -1).join('/')+'/';
|
|
|
|
|
|
|
|
// Instantiates plugin
|
2018-11-10 06:41:28 +01:00
|
|
|
$( function() {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
amplify.subscribe( 'settings.changed', function() {
|
2018-07-27 19:59:08 +02:00
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
codiad.auto_save.settings.autosave = codiad.settings.get_option( 'codiad.settings.autosave' );
|
2018-07-31 21:59:15 +02:00
|
|
|
codiad.auto_save.reload_interval();
|
2018-07-26 21:39:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
codiad.auto_save.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
codiad.auto_save = {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2018-07-26 21:39:40 +02:00
|
|
|
// Allows relative `this.path` linkage
|
2018-07-26 22:50:47 +02:00
|
|
|
auto_save_trigger: null,
|
2018-07-27 19:59:08 +02:00
|
|
|
invalid_states: [ "", " ", null, undefined ],
|
2018-07-26 21:39:40 +02:00
|
|
|
path: curpath,
|
|
|
|
saving: false,
|
|
|
|
settings: {
|
|
|
|
autosave: true,
|
|
|
|
toggle: true,
|
|
|
|
},
|
2018-09-21 03:44:10 +02:00
|
|
|
verbose: false,
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
init: function() {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
|
|
|
codiad.auto_save.settings.autosave = codiad.settings.get_option( 'codiad.settings.autosave' );
|
2018-07-26 21:39:40 +02:00
|
|
|
|
|
|
|
// Check if the auto save setting is true or false
|
2018-07-27 19:59:08 +02:00
|
|
|
// Also check to see if the editor is any of the invalid states
|
2018-07-26 21:39:40 +02:00
|
|
|
if( this.settings.autosave === false || this.settings.autosave === "false" ) {
|
2018-07-26 22:50:47 +02:00
|
|
|
|
|
|
|
window.clearInterval( this.auto_save_trigger );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
|
|
|
if( codiad.auto_save.verbose ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
console.log( 'Auto save disabled' );
|
|
|
|
}
|
2018-07-26 21:39:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$( window ).focus( function() {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
//Turn auto save on if the user comes back the tab.
|
|
|
|
codiad.auto_save.settings.toggle = true;
|
2018-09-21 03:44:10 +02:00
|
|
|
if( codiad.auto_save.verbose ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
console.log( 'Auto save resumed' );
|
|
|
|
}
|
2018-07-26 21:39:40 +02:00
|
|
|
});
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$( window ).blur( function() {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
|
|
|
//Turn auto save off if the user leaves the tab.
|
|
|
|
codiad.auto_save.settings.toggle = false;
|
2018-09-21 03:44:10 +02:00
|
|
|
if( codiad.auto_save.verbose ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
console.log( 'Auto save paused' );
|
|
|
|
}
|
2018-07-26 21:39:40 +02:00
|
|
|
});
|
|
|
|
|
2018-12-11 22:52:53 +01:00
|
|
|
if( codiad.auto_save.verbose ) {
|
|
|
|
|
|
|
|
console.log( 'Auto save Enabled' );
|
|
|
|
}
|
2018-07-26 22:50:47 +02:00
|
|
|
this.auto_save_trigger = setInterval( this.auto_save, 256 );
|
2018-07-26 21:39:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This is where the core functionality goes, any call, references,
|
|
|
|
* script-loads, etc...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
auto_save: function() {
|
|
|
|
|
2018-07-27 19:59:08 +02:00
|
|
|
if( this.settings.toggle === false || this.settings.autosave === false || codiad.auto_save.invalid_states.includes( codiad.editor.getContent() ) ) {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.saving = true;
|
|
|
|
|
|
|
|
if ( codiad.active.getPath() === null ) {
|
|
|
|
|
|
|
|
this.saving = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-07-27 19:59:08 +02:00
|
|
|
|
2018-07-26 21:39:40 +02:00
|
|
|
let tabs = document.getElementsByClassName( "tab-item" );
|
|
|
|
let path = codiad.active.getPath();
|
|
|
|
let content = codiad.editor.getContent();
|
|
|
|
|
|
|
|
codiad.active.save;
|
2018-11-10 06:41:28 +01:00
|
|
|
codiad.filemanager.saveFile( path, content, localStorage.removeItem( path ), false );
|
2018-07-26 21:39:40 +02:00
|
|
|
var session = codiad.active.sessions[path];
|
2018-07-26 22:50:47 +02:00
|
|
|
if( typeof session != 'undefined' ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2018-07-26 21:39:40 +02:00
|
|
|
session.untainted = content;
|
|
|
|
session.serverMTime = session.serverMTime;
|
2018-11-10 06:41:28 +01:00
|
|
|
if ( session.listThumb ) {
|
|
|
|
|
|
|
|
session.listThumb.removeClass('changed');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( session.tabThumb ) {
|
|
|
|
|
|
|
|
session.tabThumb.removeClass('changed');
|
|
|
|
}
|
2018-07-26 21:39:40 +02:00
|
|
|
}
|
|
|
|
this.saving = false;
|
|
|
|
},
|
|
|
|
|
2018-07-31 21:59:15 +02:00
|
|
|
reload_interval: function() {
|
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
window.clearInterval( codiad.autosave.auto_save_trigger );
|
|
|
|
window.clearInterval( this.auto_save_trigger );
|
|
|
|
} catch( error ) {}
|
2018-07-31 21:59:15 +02:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
if( codiad.auto_save.settings.autosave === true || codiad.auto_save.settings.autosave === "true" ) {
|
2018-07-31 21:59:15 +02:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
codiad.auto_save.auto_save_trigger = setInterval( codiad.auto_save.auto_save, 256 );
|
2018-07-31 21:59:15 +02:00
|
|
|
}
|
2018-07-26 21:39:40 +02:00
|
|
|
}
|
|
|
|
};
|
2018-11-10 06:41:28 +01:00
|
|
|
})( this, jQuery );
|