/*
* Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
* as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact.
*/
var EditSession = require('ace/edit_session').EditSession;
var UndoManager = require("ace/undomanager").UndoManager;
$(function() {
active.init();
});
var active = {
controller: 'components/active/controller.php',
// Path to EditSession instance mapping
sessions: {},
is_open: function(path){
return !! this.sessions[path];
},
open: function(path, content, in_background){
if (this.is_open(path)) {
this.focus(path);
return;
}
var ext = filemanager.get_extension(path);
var mode = editor.select_mode(ext);
var _this = this;
var fn = function(){
var Mode = require('ace/mode/'+mode).Mode;
// TODO: Ask for user confirmation before recovering
// And maybe show a diff
var draft = active.check_draft(path);
if (draft) {
content = draft;
message.success('Recovered unsaved content for : ' + path );
}
var session = new EditSession(content, new Mode());
session.setUndoManager(new UndoManager());
session.path = path;
_this.sessions[path] = session;
if (! in_background) {
editor.set_session(session);
}
_this.add(path, session);
}
$.loadScript('components/editor/ace-editor/mode-' + mode + '.js',
fn );
},
init: function() {
// Focus
$('#active-files a')
.live('click', function() {
active.focus($(this)
.attr('data-path'));
});
// Remove
$('#active-files a>span')
.live('click', function(e) {
e.stopPropagation();
active.remove($(this)
.parent('a')
.attr('data-path'));
});
// Sortable
$('#active-files')
.sortable({
placeholder: 'active-sort-placeholder',
tolerance: 'intersect',
start: function(e, ui) {
ui.placeholder.height(ui.item.height());
}
});
// Open saved-state active files on load
$.get(active.controller + '?action=list', function(data) {
var list_response = jsend.parse(data);
if (list_response !== null) {
$.each(list_response, function(index, value) {
filemanager.open_file(value);
});
// Run resize command to fix render issues
active.resize();
}
});
// Run resize on window resize
$(window)
.on('resize', function() {
active.resize();
});
// Prompt if a user tries to close window without saving all filess
window.onbeforeunload = function(e) {
if ($('#active-files a.changed')
.length > 0) {
var e = e || window.event;
var err_msg = "You have unsaved files."
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = err_msg;
}
// For rest
return err_msg;
}
};
},
//////////////////////////////////////////////////////////////////
// Drafts
//////////////////////////////////////////////////////////////////
check_draft: function(path) {
var draft = localStorage.getItem(path);
if (draft !== null) {
return draft;
} else {
return false;
}
},
remove_draft: function(path) {
localStorage.removeItem(path);
},
//////////////////////////////////////////////////////////////////
// Get active editor path
//////////////////////////////////////////////////////////////////
get_path: function() {
try {
return editor.get_active().getSession().path;
} catch(e) {
return null;
}
},
//////////////////////////////////////////////////////////////////
// Check if opened by another user
//////////////////////////////////////////////////////////////////
check: function(path) {
$.get(active.controller + '?action=check&path=' + path, function(data) {
var check_response = jsend.parse(data);
});
},
//////////////////////////////////////////////////////////////////
// Add newly opened file to list
//////////////////////////////////////////////////////////////////
add: function(path, session) {
var thumb = $('