/*
* 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.
*/
(function(global, $) {
var EditSession = require('ace/edit_session')
.EditSession;
var UndoManager = require('ace/undomanager')
.UndoManager;
var codiad = global.codiad;
$(function() {
codiad.active.init();
});
//////////////////////////////////////////////////////////////////
//
// Active Files Component for Codiad
// ---------------------------------
// Track and manage EditSession instaces of files being edited.
//
//////////////////////////////////////////////////////////////////
codiad.active = {
controller: 'components/active/controller.php',
// Path to EditSession instance mapping
sessions: {},
//////////////////////////////////////////////////////////////////
//
// Check if a file is open.
//
// Parameters:
// path - {String}
//
//////////////////////////////////////////////////////////////////
isOpen: function(path) {
return !!this.sessions[path];
},
open: function(path, content, inBackground) {
var _this = this;
if (this.isOpen(path)) {
this.focus(path);
return;
}
var ext = codiad.filemanager.getExtension(path);
var mode = codiad.editor.selectMode(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 = _this.checkDraft(path);
if (draft) {
content = draft;
codiad.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 (!inBackground) {
codiad.editor.setSession(session);
}
_this.add(path, session);
};
$.loadScript('components/editor/ace-editor/mode-' + mode + '.js',
fn);
},
init: function() {
var _this = this;
// Focus
$('#active-files a')
.live('click', function() {
_this.focus($(this)
.attr('data-path'));
});
// Remove
$('#active-files a>span')
.live('click', function(e) {
e.stopPropagation();
_this.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(_this.controller + '?action=list', function(data) {
var listResponse = codiad.jsend.parse(data);
if (listResponse !== null) {
$.each(listResponse, function(index, value) {
codiad.filemanager.openFile(value);
});
// Run resize command to fix render issues
codiad.editor.resize();
}
});
// Run resize on window resize
$(window)
.on('resize', function() {
codiad.editor.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 errMsg = 'You have unsaved files.';
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = errMsg;
}
// For rest
return errMsg;
}
};
},
//////////////////////////////////////////////////////////////////
// Drafts
//////////////////////////////////////////////////////////////////
checkDraft: function(path) {
var draft = localStorage.getItem(path);
if (draft !== null) {
return draft;
} else {
return false;
}
},
removeDraft: function(path) {
localStorage.removeItem(path);
},
//////////////////////////////////////////////////////////////////
// Get active editor path
//////////////////////////////////////////////////////////////////
getPath: function() {
try {
return codiad.editor.getActive()
.getSession()
.path;
} catch (e) {
return null;
}
},
//////////////////////////////////////////////////////////////////
// Check if opened by another user
//////////////////////////////////////////////////////////////////
check: function(path) {
$.get(this.controller + '?action=check&path=' + path,
function(data) {
var checkResponse = codiad.jsend.parse(data);
});
},
//////////////////////////////////////////////////////////////////
// Add newly opened file to list
//////////////////////////////////////////////////////////////////
add: function(path, session) {
var thumb = $('