Skip to content

Commit

Permalink
Merge pull request #18 from Zemistr/master
Browse files Browse the repository at this point in the history
Created listRenderer and itemRenderer. Refactored build from JSON.
  • Loading branch information
RamonSmit committed Oct 6, 2014
2 parents 87b625d + 0ec0513 commit be03203
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 60 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ These advanced config options are also available:
* `expandBtnHTML` The HTML text used to generate a list item expand button (default `'<button data-action="expand">Expand></button>'`)
* `collapseBtnHTML` The HTML text used to generate a list item collapse button (default `'<button data-action="collapse">Collapse</button>'`)
* `includeContent` Enable or disable the content in output (default `false`)
* `listRenderer` The callback for customizing final list output (default `function(children, options) { ... }` - see defaults in code)
* `itemRenderer` The callback for customizing final item output (default `function(item_attrs, content, children, options) { ... }` - see defaults in code)
* `json` JSON string used to dynamically generate a Nestable list. This is the same format as the `serialize()` output

**Inspect the [Nestable Demo](http://ramonsmit.github.io/Nestable/) for guidance.**
Expand All @@ -163,6 +165,7 @@ These advanced config options are also available:

### 6th October 2014

* [zemistr] Created listRenderer and itemRenderer. Refactored build from JSON.
* [zemistr] Added support for adding classes via input data. (```[{"id": 1, "content": "First item", "classes": ["dd-nochildren", "dd-nodrag", ...] }, ... ]```)

### 3th October 2014
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestable2",
"version": "1.1.1",
"version": "1.2.1",
"homepage": "https://github.com/RamonSmit/Nestable",
"authors": [
"Ramon Smit <@_RamonSmit>",
Expand Down
30 changes: 15 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -500,24 +500,24 @@ <h1>Nestable</h1>
"content": "Item 8"
}
]
},
{
"id": 9,
"content": "Item 9"
},
}
]
},
{
"id": 9,
"content": "Item 9"
},
{
"id": 10,
"content": "Item 10",
"children": [
{
"id": 10,
"content": "Item 10",
"id": 11,
"content": "Item 11",
"children": [
{
"id": 11,
"content": "Item 11",
"children": [
{
"id": 12,
"content": "Item 12"
}
]
"id": 12,
"content": "Item 12"
}
]
}
Expand Down
114 changes: 70 additions & 44 deletions jquery.nestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,30 @@
fixedDepth: false, //fixed item's depth
fixed: false,
includeContent: false,
callback: function(l, e) {}
callback: function(l, e) {},
listRenderer: function(children, options) {
var html = '<' + options.listNodeName + ' class="' + options.listClass + '">';
html += children;
html += '</' + options.listNodeName + '>';

return html;
},
itemRenderer: function(item_attrs, content, children, options) {
var item_attrs_string = $.map(item_attrs, function(value, key) {
return ' ' + key + '="' + value + '"';
}).join(' ');

var html = '<' + options.itemNodeName + item_attrs_string + '>';
html += '<' + options.handleNodeName + ' class="' + options.handleClass + '">';
html += '<' + options.contentNodeName + ' class="' + options.contentClass + '">';
html += content;
html += '</' + options.contentNodeName + '>';
html += '</' + options.handleNodeName + '>';
html += children;
html += '</' + options.itemNodeName + '>';

return html;
}
};

function Plugin(element, options) {
Expand Down Expand Up @@ -164,10 +187,6 @@
},

_build: function() {
var output = "<" + this.options.listNodeName +
" class='" + this.options.listClass +
"'>";

function escapeHtml(text) {
var map = {
'&': '&amp;',
Expand All @@ -180,77 +199,84 @@
return text + "".replace(/[&<>"']/g, function(m) { return map[m]; });
}

function buildItem(item, options) {
var children = item.children;
var content = options.contentCallback(item);
var item_classes = {};
var classes = item.classes || {};
function filterClasses(classes) {
var new_classes = {};

if(typeof classes == 'string') {
classes = [classes];
for(var k in classes) {
// Remove duplicates
new_classes[classes[k]] = classes[k];
}

delete item.children;
delete item.classes;
delete item.content;
return new_classes;
}

for(var k in classes) {
// Remove duplicates
item_classes[classes[k]] = classes[k];
function createClassesString(item, options) {
var classes = item.classes || {};

if(typeof classes == 'string') {
classes = [classes];
}

var item_classes = filterClasses(classes);
item_classes[options.itemClass] = options.itemClass;

// create class string
var classes_string = $.map(item_classes, function(val) {
return $.map(item_classes, function(val) {
return val;
}).join(' ');
}

function createDataAttrs(attr) {
attr = $.extend({}, attr);

delete attr.children;
delete attr.classes;
delete attr.content;

var html = "<" + options.itemNodeName + " class='" + classes_string + "'";
var data_attrs = {};

$.each(item, function(key, value) {
$.each(attr, function(key, value) {
if(typeof value == 'object') {
value = JSON.stringify(value);
}

html += " data-" + key + "='" + escapeHtml(value) + "'";
data_attrs["data-" + key] = escapeHtml(value);
});

html += ">";
html += "<" + options.handleNodeName + " class='" + options.handleClass + "'>";
html += "<" + options.contentNodeName + " class='" + options.contentClass + "'>";
html += content;
html += "</" + options.contentNodeName + "></" + options.handleNodeName + ">";
return data_attrs;
}

if(children) {
html += "<" + options.listNodeName + " class='" + options.listClass + "'>";
function buildList(items, options) {
if(!items) {
return '';
}

$.each(children, function(index, sub) {
html += buildItem(sub, options);
});
var children = '';

html += "</" + options.listNodeName + ">";
}
$.each(items, function(index, sub) {
children += buildItem(sub, options);
});

html += "</" + options.itemNodeName + ">";
return options.listRenderer(children, options);
}

function buildItem(item, options) {
var item_attrs = createDataAttrs(item);
item_attrs["class"] = createClassesString(item, options);

var content = options.contentCallback(item);
var children = buildList(item.children, options);

return html;
return options.itemRenderer(item_attrs, content, children, options);
}

var options = this.options;
var json = this.options.json;

if(typeof json == 'string') {
json = JSON.parse(json);
}

$.each(json, function(index, item) {
output += buildItem(item, options);
});

output += "</" + this.options.listNodeName + ">";

$(this.el).html(output);
$(this.el).html(buildList(json, this.options));
},

serialize: function() {
Expand Down

0 comments on commit be03203

Please sign in to comment.