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
|
|
|
|
2019-01-15 16:33:59 +01:00
|
|
|
amplify.subscribe( 'settings.save', async function() {
|
|
|
|
|
|
|
|
let option = await codiad.settings.get_option( 'codiad.settings.autosave' );
|
|
|
|
|
|
|
|
if( option != codiad.auto_save.settings.autosave ) {
|
|
|
|
|
|
|
|
//codiad.auto_save.reload_interval();
|
|
|
|
window.location.reload();
|
|
|
|
}
|
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,
|
2019-03-21 17:07:03 +01:00
|
|
|
change: null,
|
2019-02-06 23:52:49 +01:00
|
|
|
content: null,
|
|
|
|
editor: null,
|
2018-07-27 19:59:08 +02:00
|
|
|
invalid_states: [ "", " ", null, undefined ],
|
2018-07-26 21:39:40 +02:00
|
|
|
path: curpath,
|
2019-05-01 22:21:45 +02:00
|
|
|
save_interval: null,
|
2018-07-26 21:39:40 +02:00
|
|
|
saving: false,
|
|
|
|
settings: {
|
|
|
|
autosave: true,
|
|
|
|
toggle: true,
|
|
|
|
},
|
2019-05-06 15:12:57 +02:00
|
|
|
verbose: true,
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2019-01-15 16:33:59 +01:00
|
|
|
init: async function() {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
let _this = codiad.auto_save;
|
|
|
|
_this.settings.autosave = await 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
|
2019-01-15 16:33:59 +01: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' );
|
|
|
|
}
|
2019-02-06 23:52:49 +01:00
|
|
|
|
|
|
|
let content = codiad.editor.getContent();
|
|
|
|
|
|
|
|
if( ! codiad.active.getPath() == null && ! codiad.auto_save.invalid_states.includes( content ) ) {
|
|
|
|
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Subscribe to know when a file is being closed. */
|
|
|
|
amplify.subscribe( 'active.onClose', function( path ) {
|
|
|
|
|
|
|
|
let _this = codiad.auto_save;
|
2019-04-11 01:31:28 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
_this.editor.removeEventListener( "change", _this.change );
|
|
|
|
} catch( e ) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the listener is not currently on file and we
|
|
|
|
* try to close it, the program will throw an exception and
|
|
|
|
* stop you from closing the file
|
|
|
|
*/
|
2019-05-06 15:12:57 +02:00
|
|
|
if( codiad.auto_save.verbose ) {
|
|
|
|
|
|
|
|
console.log( "Error removing event listener", e );
|
|
|
|
}
|
2019-04-11 01:31:28 +02:00
|
|
|
}
|
2019-02-06 23:52:49 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/* Subscribe to know when a file become active. */
|
|
|
|
amplify.subscribe( 'active.onFocus', function( path ) {
|
|
|
|
|
|
|
|
let _this = codiad.auto_save;
|
|
|
|
|
|
|
|
if( ! _this.editor == null && path == _this.editor.getSession().path ) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_this.editor = codiad.editor.getActive();
|
|
|
|
_this.content = codiad.editor.getContent();
|
2019-03-21 17:07:03 +01:00
|
|
|
_this.change = _this.editor.addEventListener( "change", _this.auto_save );
|
2019-02-06 23:52:49 +01:00
|
|
|
});
|
2018-07-26 21:39:40 +02:00
|
|
|
},
|
|
|
|
|
2019-05-01 22:21:45 +02:00
|
|
|
auto_save: function() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When saving after every change, we encounter an issue where every
|
|
|
|
* once in a while the last change or last few changes will not get
|
|
|
|
* saved. Due to this, I decided to instead only save after the user
|
|
|
|
* has finished typing their changes.
|
|
|
|
*
|
|
|
|
* On every change to the editor, we set a timeout of half a second
|
|
|
|
* to see if the user is still typing. If they are, we clear the
|
|
|
|
* timeout and set it again. If they have stopped typing then the
|
|
|
|
* timout is triggered after 500 miliseconds and the file is saved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let _this = codiad.auto_save;
|
|
|
|
|
|
|
|
if( _this.save_interval !== null ) {
|
|
|
|
|
|
|
|
clearTimeout( _this.save_interval );
|
|
|
|
_this.save_interval = null;
|
|
|
|
}
|
|
|
|
_this.save_interval = setTimeout( _this.save, 500 );
|
|
|
|
},
|
|
|
|
|
2018-07-26 21:39:40 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This is where the core functionality goes, any call, references,
|
|
|
|
* script-loads, etc...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-05-01 22:21:45 +02:00
|
|
|
save: function() {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
let _this = codiad.auto_save;
|
2019-05-01 22:21:45 +02:00
|
|
|
|
|
|
|
if( _this.saving ) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
_this.saving = true;
|
|
|
|
let tabs = document.getElementsByClassName( "tab-item" );
|
|
|
|
let path = codiad.active.getPath();
|
|
|
|
let content = codiad.editor.getContent();
|
|
|
|
|
|
|
|
if( _this.settings.toggle == false || _this.settings.autosave == false || codiad.auto_save.invalid_states.includes( content ) ) {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
_this.saving = false;
|
2018-07-26 21:39:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
if( path == null ) {
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
_this.saving = false;
|
2018-07-26 21:39:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-27 19:59:08 +02:00
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
if( _this.verbose ) {
|
|
|
|
|
|
|
|
console.log( content, _this.content );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( content == _this.content ) {
|
|
|
|
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
let session = codiad.active.sessions[path];
|
2019-02-06 23:52:49 +01:00
|
|
|
if( typeof session != 'undefined' ) {
|
|
|
|
|
|
|
|
session.untainted = content;
|
|
|
|
session.serverMTime = session.serverMTime;
|
|
|
|
if ( session.listThumb ) {
|
|
|
|
|
|
|
|
session.listThumb.removeClass('changed');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( session.tabThumb ) {
|
|
|
|
|
|
|
|
session.tabThumb.removeClass('changed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-07-26 21:39:40 +02:00
|
|
|
|
2019-02-06 05:04:14 +01:00
|
|
|
/*
|
|
|
|
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
this code caused issues even though it is the proper way to save something.
|
2019-02-06 05:04:14 +01:00
|
|
|
Whenever in collaboration, the server constantly gave a wrong file version error.
|
|
|
|
|
|
|
|
let path = codiad.active.getPath();
|
|
|
|
codiad.active.save( path, false );
|
2019-02-06 23:52:49 +01:00
|
|
|
_this.saving = false;
|
2019-02-06 05:04:14 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
_this.content = content;
|
2018-07-26 21:39:40 +02:00
|
|
|
codiad.active.save;
|
2019-05-01 22:21:45 +02:00
|
|
|
//codiad.filemanager.saveFile( path, content, localStorage.removeItem( path ), false );
|
|
|
|
codiad.filemanager.saveFile( path, content, localStorage.removeItem( path ) );
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
let 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');
|
|
|
|
}
|
2019-02-06 05:04:14 +01:00
|
|
|
}
|
2019-02-06 23:52:49 +01:00
|
|
|
_this.saving = false;
|
2018-07-26 21:39:40 +02:00
|
|
|
},
|
|
|
|
|
2019-01-15 16:33:59 +01:00
|
|
|
reload_interval: async function() {
|
2018-07-31 21:59:15 +02:00
|
|
|
|
2019-01-15 16:33:59 +01:00
|
|
|
codiad.auto_save.settings.autosave = await codiad.settings.get_option( 'codiad.settings.autosave' );
|
2018-09-21 03:44:10 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
window.clearInterval( codiad.autosave.auto_save_trigger );
|
|
|
|
window.clearInterval( this.auto_save_trigger );
|
2019-05-06 15:12:57 +02:00
|
|
|
} catch( error ) {
|
|
|
|
|
|
|
|
if( codiad.auto_save.verbose ) {
|
|
|
|
|
|
|
|
console.log( "Error clearing interval", error );
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 21:59:15 +02:00
|
|
|
|
2019-01-15 16:33:59 +01: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 );
|