2011-03-14 08:49:46 +00:00
|
|
|
var reorder = function(class) {
|
|
|
|
console.log("QQQ Enter reorder");
|
2011-03-13 23:32:13 +00:00
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
var divs = $.find('div[class=' + class + ']');
|
|
|
|
console.log('divs length = ' + divs.length);
|
2011-03-13 23:32:13 +00:00
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
$(divs).find('a').hide();
|
2011-03-13 23:32:13 +00:00
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
$(divs).each(function(i, div) {
|
2011-03-13 23:32:13 +00:00
|
|
|
console.log("ROW " + i);
|
2011-03-14 08:49:46 +00:00
|
|
|
$(div).find('a.remove_row').show();
|
|
|
|
replaceIndex(rowIndex(div), i);
|
2011-03-13 23:32:13 +00:00
|
|
|
});
|
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
$(divs).last().find('a.add_row').show();
|
2011-03-13 23:32:13 +00:00
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
if (divs.length == 1) {
|
|
|
|
$(divs).find('a.remove_row').hide();
|
2011-03-13 23:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
var rowIndex = function(div) {
|
|
|
|
var idstr = $(div).attr('id');
|
|
|
|
var id = idstr.match(/\d+/);
|
2011-03-13 23:32:13 +00:00
|
|
|
console.log("id = " + id);
|
2011-03-14 08:49:46 +00:00
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
|
|
|
var rowCount = function(class) {
|
|
|
|
var divs = $.find('div[class=' + class + ']');
|
|
|
|
return divs.length;
|
2011-03-13 23:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var replaceIndex = function(elem, oldIndex, newIndex) {
|
|
|
|
$(elem).find('*').each(function() {
|
|
|
|
$.each(this.attributes, function(i, attrib) {
|
|
|
|
var regexp = /extprofile-.*-\d.*/;
|
|
|
|
var value = attrib.value;
|
|
|
|
var match = value.match(regexp);
|
2011-03-14 08:49:46 +00:00
|
|
|
if (match !== null) {
|
2011-03-13 23:32:13 +00:00
|
|
|
attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
|
|
|
|
console.log('match: oldIndex = ' + oldIndex + ' newIndex = ' + newIndex + ' name = ' + attrib.name + ' value = ' + attrib.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var resetRow = function(elem) {
|
|
|
|
$(elem).find('input').attr('value', '');
|
|
|
|
$(elem).find("select option[value='office']").attr("selected", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
var addRow = function() {
|
2011-03-14 08:49:46 +00:00
|
|
|
var div = $(this).closest('div');
|
|
|
|
var id = $(div).attr('id');
|
|
|
|
var class = $(div).attr('class');
|
|
|
|
var index = id.match(/\d+/);
|
|
|
|
console.log("Current row = " + index + ', class = ' + class);
|
2011-03-13 23:32:13 +00:00
|
|
|
var tr = $(this).closest('tr');
|
|
|
|
var newtr = $(tr).clone();
|
|
|
|
var newIndex = parseInt(index) + 1;
|
|
|
|
replaceIndex(newtr, index, newIndex);
|
|
|
|
resetRow(newtr);
|
|
|
|
$(tr).after(newtr);
|
2011-03-14 08:49:46 +00:00
|
|
|
reorder(class);
|
2011-03-13 23:32:13 +00:00
|
|
|
};
|
|
|
|
|
2011-03-14 08:49:46 +00:00
|
|
|
var removeRow = function() {
|
|
|
|
var div = $(this).closest('div');
|
|
|
|
var id = $(div).attr('id');
|
|
|
|
var class = $(div).attr('class');
|
|
|
|
|
|
|
|
cnt = rowCount(class);
|
|
|
|
console.debug("removeRow - cnt = " + cnt);
|
|
|
|
if (cnt > 1) {
|
|
|
|
var target = $(this).closest('tr');
|
|
|
|
target.remove();
|
|
|
|
reorder(class);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var init = function() {
|
|
|
|
reorder('phone-edit');
|
2011-03-14 22:29:11 +00:00
|
|
|
reorder('experience-edit');
|
2011-03-14 08:49:46 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 23:32:13 +00:00
|
|
|
$(document).ready(
|
|
|
|
|
|
|
|
function() {
|
2011-03-14 08:49:46 +00:00
|
|
|
init();
|
2011-03-13 23:32:13 +00:00
|
|
|
$('.add_row').live('click', addRow);
|
|
|
|
$('.remove_row').live('click', removeRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|