Skip to content

Commit

Permalink
added ability to supply LI and A attributes with the types plugin - c…
Browse files Browse the repository at this point in the history
…lose vakata#1208
  • Loading branch information
vakata committed Sep 23, 2015
1 parent 4e33345 commit 2440418
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 6 deletions.
139 changes: 137 additions & 2 deletions dist/jstree.js
Original file line number Diff line number Diff line change
Expand Up @@ -7398,7 +7398,7 @@
var m = this._model.data,
dpc = data.nodes,
t = this.settings.types,
i, j, c = 'default';
i, j, c = 'default', k;
for(i = 0, j = dpc.length; i < j; i++) {
c = 'default';
if(m[dpc[i]].original && m[dpc[i]].original.type && t[m[dpc[i]].original.type]) {
Expand All @@ -7411,6 +7411,39 @@
if(m[dpc[i]].icon === true && t[c].icon !== undefined) {
m[dpc[i]].icon = t[c].icon;
}
if(t[c].li_attr !== undefined && typeof t[c].li_attr === 'object') {
for (k in t[c].li_attr) {
if (t[c].li_attr.hasOwnProperty(k)) {
if (k === 'id') {
continue;
}
else if (m[dpc[i]].li_attr[k] === undefined) {
m[dpc[i]].li_attr[k] = t[c].li_attr[k];
}
else if (k === 'class') {
m[dpc[i]].li_attr['class'] = t[c].li_attr['class'] + ' ' + m[dpc[i]].li_attr['class'];
}
}
}
}
if(t[c].a_attr !== undefined && typeof t[c].a_attr === 'object') {
for (k in t[c].a_attr) {
if (t[c].a_attr.hasOwnProperty(k)) {
if (k === 'id') {
continue;
}
else if (m[dpc[i]].a_attr[k] === undefined) {
m[dpc[i]].a_attr[k] = t[c].a_attr[k];
}
else if (k === 'href' && m[dpc[i]].a_attr[k] === '#') {
m[dpc[i]].a_attr['href'] = t[c].a_attr['href'];
}
else if (k === 'class') {
m[dpc[i]].a_attr['class'] = t[c].a_attr['class'] + ' ' + m[dpc[i]].a_attr['class'];
}
}
}
}
}
m[$.jstree.root].type = $.jstree.root;
}, this));
Expand Down Expand Up @@ -7541,7 +7574,7 @@
* @plugin types
*/
this.set_type = function (obj, type) {
var t, t1, t2, old_type, old_icon;
var m = this._model.data, t, t1, t2, old_type, old_icon, k, d, a;
if($.isArray(obj)) {
obj = obj.slice();
for(t1 = 0, t2 = obj.length; t1 < t2; t1++) {
Expand All @@ -7552,18 +7585,120 @@
t = this.settings.types;
obj = this.get_node(obj);
if(!t[type] || !obj) { return false; }
d = this.get_node(obj, true);
if (d && d.length) {
a = d.children('.jstree-anchor');
}
old_type = obj.type;
old_icon = this.get_icon(obj);
obj.type = type;
if(old_icon === true || (t[old_type] && t[old_type].icon !== undefined && old_icon === t[old_type].icon)) {
this.set_icon(obj, t[type].icon !== undefined ? t[type].icon : true);
}

// remove old type props
if(t[old_type].li_attr !== undefined && typeof t[old_type].li_attr === 'object') {
for (k in t[old_type].li_attr) {
if (t[old_type].li_attr.hasOwnProperty(k)) {
if (k === 'id') {
continue;
}
else if (k === 'class') {
m[obj.id].li_attr['class'] = (m[obj.id].li_attr['class'] || '').replace(t[old_type].li_attr[k], '');
if (d) { d.removeClass(t[old_type].li_attr[k]); }
}
else if (m[obj.id].li_attr[k] === t[old_type].li_attr[k]) {
m[obj.id].li_attr[k] = null;
if (d) { d.removeAttr(k); }
}
}
}
}
if(t[old_type].a_attr !== undefined && typeof t[old_type].a_attr === 'object') {
for (k in t[old_type].a_attr) {
if (t[old_type].a_attr.hasOwnProperty(k)) {
if (k === 'id') {
continue;
}
else if (k === 'class') {
m[obj.id].a_attr['class'] = (m[obj.id].a_attr['class'] || '').replace(t[old_type].a_attr[k], '');
if (a) { a.removeClass(t[old_type].a_attr[k]); }
}
else if (m[obj.id].a_attr[k] === t[old_type].a_attr[k]) {
if (k === 'href') {
m[obj.id].a_attr[k] = '#';
if (a) { a.attr('href', '#'); }
}
else {
delete m[obj.id].a_attr[k];
if (a) { a.removeAttr(k); }
}
}
}
}
}

// add new props
if(t[type].li_attr !== undefined && typeof t[type].li_attr === 'object') {
for (k in t[type].li_attr) {
if (t[type].li_attr.hasOwnProperty(k)) {
if (k === 'id') {
continue;
}
else if (m[obj.id].li_attr[k] === undefined) {
m[obj.id].li_attr[k] = t[type].li_attr[k];
if (d) {
if (k === 'class') {
d.addClass(t[type].li_attr[k]);
}
else {
d.attr(k, t[type].li_attr[k]);
}
}
}
else if (k === 'class') {
m[obj.id].li_attr['class'] = t[type].li_attr[k] + ' ' + m[obj.id].li_attr['class'];
if (d) { d.addClass(t[type].li_attr[k]); }
}
}
}
}
if(t[type].a_attr !== undefined && typeof t[type].a_attr === 'object') {
for (k in t[type].a_attr) {
if (t[type].a_attr.hasOwnProperty(k)) {
if (k === 'id') {
continue;
}
else if (m[obj.id].a_attr[k] === undefined) {
m[obj.id].a_attr[k] = t[type].a_attr[k];
if (a) {
if (k === 'class') {
a.addClass(t[type].a_attr[k]);
}
else {
a.attr(k, t[type].a_attr[k]);
}
}
}
else if (k === 'href' && m[obj.id].a_attr[k] === '#') {
m[obj.id].a_attr['href'] = t[type].a_attr['href'];
if (a) { a.attr('href', t[type].a_attr['href']); }
}
else if (k === 'class') {
m[obj.id].a_attr['class'] = t[type].a_attr['class'] + ' ' + m[obj.id].a_attr['class'];
if (a) { a.addClass(t[type].a_attr[k]); }
}
}
}
}

return true;
};
};
// include the types plugin by default
// $.jstree.defaults.plugins.push("types");


/**
* ### Unique plugin
*
Expand Down
Loading

0 comments on commit 2440418

Please sign in to comment.