Skip to content

Commit

Permalink
use children.nodes.length instead of idx and fix updateTab when minIt…
Browse files Browse the repository at this point in the history
…ems enforced (jsonform#22)
  • Loading branch information
dfs123d committed Nov 16, 2012
1 parent 706f7c7 commit 3ac09ee
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/jsonform.js
Expand Up @@ -684,11 +684,11 @@ jsonform.elementTypes = {
evt.stopPropagation();
var idx = node.children.length;
if (boundaries.maxItems>=0) {
if (idx>boundaries.maxItems-2) $('> span > a._jsonform-array-addmore', $nodeid).addClass("disabled");
if (idx>boundaries.maxItems-1) return false;
if (node.children.length>boundaries.maxItems-2) $('> span > a._jsonform-array-addmore', $nodeid).addClass("disabled");
if (node.children.length>boundaries.maxItems-1) return false;
}
node.insertArrayItem(idx, $('> ul', $nodeid).get(0));
if (boundaries.minItems > 0 && idx > boundaries.minItems - 1) {
if (boundaries.minItems > 0 && node.children.length > boundaries.minItems - 1) {
$('> span > a._jsonform-array-deletelast', $nodeid).removeClass("disabled");
}
});
Expand All @@ -702,17 +702,18 @@ jsonform.elementTypes = {
//$('> span > a._jsonform-array-addmore', $nodeid).click();
node.insertArrayItem(curItems, $('> ul', $nodeid).get(0));
}
$('> span > a._jsonform-array-deletelast', $nodeid).addClass('disabled');
}

$('> span > a._jsonform-array-deletelast', $nodeid).click(function (evt) {
var idx = node.children.length - 1;
evt.preventDefault();
evt.stopPropagation();
if (boundaries.minItems > 0) {
if (idx < boundaries.minItems + 1) {
if (node.children.length < boundaries.minItems + 2) {
$('> span > a._jsonform-array-deletelast', $nodeid).addClass('disabled');
}
if (idx < boundaries.minItems) return false;
if (node.children.length <= boundaries.minItems) return false;
}
node.deleteArrayItem(idx);
if (boundaries.maxItems>=0 && idx<=boundaries.maxItems-1) {
Expand Down Expand Up @@ -789,12 +790,14 @@ jsonform.elementTypes = {
// Refreshes the list of tabs
var updateTabs = function (selIdx) {
var tabs = '';
var activateFirstTab = false;
if (selIdx === undefined) {
selIdx = $('> .tabbable > .nav-tabs .active', $nodeid).data('idx');
if (selIdx) {
selIdx = parseInt(selIdx, 10);
}
else {
activateFirstTab = true;
selIdx = 0;
}
}
Expand All @@ -811,6 +814,9 @@ jsonform.elementTypes = {
'</a></li>';
});
$('> .tabbable > .nav-tabs', $nodeid).html(tabs);
if (activateFirstTab) {
$('> .tabbable > .nav-tabs [data-idx="0"]', $nodeid).addClass('active');
}
$('> .tabbable > .nav-tabs [data-toggle="tab"]', $nodeid).eq(selIdx).click();
};

Expand All @@ -837,10 +843,10 @@ jsonform.elementTypes = {
$('> a._jsonform-array-addmore', $nodeid).click(function (evt) {
var idx = node.children.length;
if (boundaries.maxItems>=0) {
if (idx>boundaries.maxItems-2) {
if (node.children.length>boundaries.maxItems-2) {
$('> a._jsonform-array-addmore', $nodeid).addClass("disabled");
}
if (idx>boundaries.maxItems-1) return false;
if (node.children.length>boundaries.maxItems-1) return false;
}
evt.preventDefault();
evt.stopPropagation();
Expand Down Expand Up @@ -873,6 +879,7 @@ jsonform.elementTypes = {
for (var i=0;i<(boundaries.minItems-1);i++) {
$('> a._jsonform-array-addmore', $nodeid).click();
}
$('> a._jsonform-array-deleteitem', $nodeid).addClass('disabled');
updateTabs();
}

Expand Down

0 comments on commit 3ac09ee

Please sign in to comment.