Upgrade to Bootstrap 3

This commit is contained in:
benweet 2013-08-11 01:52:27 +01:00
parent caa64d981f
commit 10b9768ba4
2 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,29 @@
define(["utils"], function(utils) {
function FolderDescriptor(folderIndex, name, fileList) {
this.folderIndex = folderIndex;
this._name = name || localStorage[folderIndex + ".name"];
this.fileList = fileList || {};
Object.defineProperty(this, 'name', {
get: function() {
return this._name;
},
set: function(name) {
this._name = name;
localStorage[this.folderIndex + ".name"] = name;
}
});
}
FolderDescriptor.prototype.addFile = function(fileDesc) {
utils.appendIndexToArray(this.folderIndex + ".files", fileDesc.fileIndex);
this.fileList[fileDesc.fileIndex] = fileDesc;
};
FolderDescriptor.prototype.removeFile = function(fileDesc) {
utils.removeIndexFromArray(this.folderIndex + ".files", fileDesc.fileIndex);
delete this.fileList[fileDesc.fileIndex];
};
return FolderDescriptor;
});

View File

@ -0,0 +1,180 @@
define([
"jquery",
"underscore",
"utils",
"classes/Extension",
"classes/FolderDescriptor",
"fileSystem",
"config"
], function($, _, utils, Extension, FolderDescriptor, fileSystem) {
var documentManager = new Extension("documentManager", 'Document Manager', false, true);
var fileMgr = undefined;
documentManager.onFileMgrCreated = function(fileMgrParameter) {
fileMgr = fileMgrParameter;
};
var eventMgr = undefined;
documentManager.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
};
var documentListElt = undefined;
var selectFileDesc = undefined;
var folderList = {};
var folderEltTmpl = [
'<a href="#" class="list-group-item folder clearfix" data-folder-index="<%= folderDesc.folderIndex %>" data-toggle="collapse" data-target=".modal-document-manager .nav.<%= id %>">',
'<label class="checkbox" title="Select"><input type="checkbox"></label>',
'<button class="btn btn-default button-rename" title="Rename"><i class="icon-pencil"></i></button>',
'<div class="name"><i class="icon-folder"></i> ',
'<%= folderDesc.name %></div>',
'<input type="text" class="input-rename form-control hide"></a>',
'<ul class="nav collapse <%= id %>"><%= fileListHtml %></ul>'
].join('');
var orphanEltTmpl = [
'<div class="list-group-item file clearfix" data-file-index="<%= fileDesc.fileIndex %>">',
'<label class="checkbox" title="Select"><input type="checkbox"></label>',
'<button class="btn btn-default button-rename" title="Rename"><i class="icon-pencil"></i></button>',
'<div class="name"><%= fileDesc.composeTitle() %></div>',
'<input type="text" class="input-rename form-control hide"></div>',
].join('');
var buildManager = function() {
var documentListHtml = '';
// Add orphan documents
documentListHtml += _.chain(fileSystem).filter(function(fileDesc) {
return fileDesc.folder === undefined;
}).sortBy(function(fileDesc) {
return fileDesc.title.toLowerCase();
}).reduce(function(result, fileDesc) {
return result + _.template(orphanEltTmpl, {
fileDesc: fileDesc,
});
}, '').value();
// Build directories
_.chain(folderList).sortBy(function(folderDesc) {
return folderDesc.name.toLowerCase();
}).each(function(folderDesc) {
var fileListHtml = _.chain(folderDesc.fileList).sortBy(function(fileDesc) {
return fileDesc.title.toLowerCase();
}).reduce(function(result, fileDesc) {
return result + '<li><a href="#">' + fileDesc.composeTitle() + '</a></li>';
}, '').value();
documentListHtml += _.template(folderEltTmpl, {
folderDesc: folderDesc,
fileListHtml: fileListHtml,
id: folderDesc.folderIndex.replace('.', '')
});
});
documentListElt.innerHTML = documentListHtml;
// Set rename event listeners
_.each(documentListElt.querySelectorAll('.button-rename'), function(buttonElt) {
buttonElt = $(buttonElt);
buttonElt.click(function() {
var parentElt = buttonElt.parent();
var name = undefined;
var folderDesc = folderList[parentElt.data('folderIndex')];
var fileDesc = fileSystem[parentElt.data('fileIndex')];
if(folderDesc) {
name = folderDesc.name;
}
else if(fileDesc) {
name = fileDesc.title;
}
parentElt.find('.name').addClass('hide');
parentElt.find('.input-rename').removeClass('hide').val(name)[0].select();
});
});
_.each(documentListElt.querySelectorAll('.input-rename'), function(inputElt) {
inputElt = $(inputElt);
function rename() {
var parentElt = inputElt.parent();
var name = $.trim(inputElt.val());
var folderDesc = folderList[parentElt.data('folderIndex')];
var fileDesc = fileSystem[parentElt.data('fileIndex')];
if(name && folderDesc && name != folderDesc.name) {
folderDesc.name = name;
buildManager();
}
else if(name && fileDesc && name != fileDesc.title) {
fileDesc.title = name;
buildManager();
eventMgr.onTitleChanged(fileDesc);
}
else {
inputElt.addClass('hide');
parentElt.find('.name').removeClass('hide');
}
}
inputElt.blur(function() {
rename();
}).keyup(function(e) {
if(e.keyCode == 13) {
rename();
e.stopPropagation();
}
if(e.keyCode == 27) {
inputElt.val('');
rename();
e.stopPropagation();
}
});
});
};
documentManager.onFileSelected = function(fileDesc) {
selectFileDesc = fileDesc;
buildManager();
};
documentManager.onFileCreated = buildManager;
documentManager.onFileDeleted = buildManager;
documentManager.onTitleChanged = buildManager;
documentManager.onSyncExportSuccess = buildManager;
documentManager.onSyncRemoved = buildManager;
documentManager.onNewPublishSuccess = buildManager;
documentManager.onPublishRemoved = buildManager;
documentManager.onInit = function() {
// Retrieve folder descriptors from localStorage
_.each(utils.retrieveIndexArray("folder.list"), function(folderIndex) {
folderList[folderIndex] = new FolderDescriptor(folderIndex);
});
};
documentManager.onReady = function() {
var modalElt = document.querySelector('.modal-document-manager');
documentListElt = modalElt.querySelector('.list-group.document-list');
$('.action-create-folder').click(function() {
// Create new folder
var folderIndex = undefined;
do {
folderIndex = "folder." + utils.randomString();
} while (_.has(folderList, folderIndex));
localStorage[folderIndex + ".name"] = DEFAULT_FOLDER_NAME;
// Create the folder descriptor
var folderDesc = new FolderDescriptor(folderIndex, DEFAULT_FOLDER_NAME);
// Add the index to the folder list
utils.appendIndexToArray("folder.list", folderIndex);
folderList[folderIndex] = folderDesc;
buildManager();
var renameButtonElt = $(modalElt.querySelector('[data-folder-index="' + folderIndex + '"] .button-rename')).click();
modalElt.scrollTop = modalElt.scrollTop + renameButtonElt.offset().top;
});
};
return documentManager;
});