/**
* An easy tree view plugin for jQuery and Bootstrap
* @Copyright yuez.me 2014
* @Author yuez
* @Version 0.1
*/
(function ($) {
$.fn.EasyTree = function (options) {
var defaults = {
selectable: true,
deletable: false,
editable: false,
addable: false,
i18n: {
deleteNull: 'Select a node to delete',
deleteConfirmation: 'Delete this node?',
confirmButtonLabel: 'Okay',
editNull: 'Select a node to edit',
editMultiple: 'Only one node can be edited at one time',
addMultiple: 'Select a node to add a new node',
collapseTip: 'collapse',
expandTip: 'expand',
selectTip: 'select',
unselectTip: 'unselet',
editTip: 'edit',
addTip: 'add',
deleteTip: 'delete',
cancelButtonLabel: 'cancle'
}
};
var warningAlert = $('
');
var dangerAlert = $('
');
var createInput = $('
');
options = $.extend(defaults, options);
this.each(function () {
var easyTree = $(this);
$.each($(easyTree).find('ul > li'), function() {
var text;
if($(this).is('li:has(ul)')) {
var children = $(this).find(' > ul');
$(children).remove();
text = $(this).text();
$(this).html('');
$(this).find(' > span > span').addClass('fa-folder-open ');
$(this).find(' > span > a').text(text);
$(this).append(children);
}
else {
text = $(this).text();
$(this).html('');
$(this).find(' > span > span').addClass('fa-file');
$(this).find(' > span > a').text(text);
}
});
$(easyTree).find('li:has(ul)').addClass('parent_li').find(' > span').attr('title', options.i18n.collapseTip);
// add easy tree toolbar dom
if (options.deletable || options.editable || options.addable) {
$(easyTree).prepend(' ');
}
// addable
if (options.addable) {
$(easyTree).find('.easy-tree-toolbar').append(' ');
$(easyTree).find('.easy-tree-toolbar .create > button').attr('title', options.i18n.addTip).click(function () {
var createBlock = $(easyTree).find('.easy-tree-toolbar .create');
$(createBlock).append(createInput);
$(createInput).find('input').focus();
$(createInput).find('.confirm').text(options.i18n.confirmButtonLabel);
$(createInput).find('.confirm').click(function () {
if ($(createInput).find('input').val() === '')
return;
var selected = getSelectedItems();
var item = $('