order track list item done

Signed-off-by: bachy <git@g-u-i.net>
This commit is contained in:
bachy
2012-05-18 18:15:49 +02:00
parent 292f704f37
commit b4b346b8a5
5 changed files with 235 additions and 54 deletions

View File

@@ -28,7 +28,26 @@
//currently selected items to it.
if($(this).hasClass('term-reference-tree-track-list-shown')) {
var track_list_container = $(this).find('.term-reference-tree-track-list');
var tracklist_is_orderable = track_list_container.is('.order-list');
if(tracklist_is_orderable){
track_list_container.sortable({
update: function(event, ui) {
console.log('sort update : event', event);
console.log('sort update : ui', ui);
$.each(event.target.children, function(index, val) {
var $item = $(val),
// event.target = ul.list
// ui.item = li.track-item
control_id = $item.data('control_id'),
$hiddenInput = $('#'+control_id).parent('.form-item').next('input[type=hidden]');
// $hiddenInput.attr('value', $item.index());
$hiddenInput.val($item.index());
});
},
});
}
//Var to track whether using checkboxes or radio buttons.
var input_type =
( $(this).has('input[type=checkbox]').size() > 0 ) ? 'checkbox' : 'radio';
@@ -40,14 +59,22 @@
var labels = checked_controls.next();
var label_element;
//get delta
if(tracklist_is_orderable){
var weights = checked_controls.parent('.form-item').next('input[type=hidden]');
}
//For each label of the checked boxes, add item to the track list.
labels.each(function(index) {
label_element = $(labels[index]);
delta = tracklist_is_orderable ? $(weights[index]).val() : -1;
addItemToTrackList(
track_list_container, //Where to add new item.
label_element.html(), //Text of new item.
$(label_element).attr('for'), //Id of control new item is for.
input_type //checkbox or radio
input_type, //checkbox or radio
delta //delta
);
}); //End labels.each
@@ -62,10 +89,14 @@
//Remove the "nothing selected" message if showing - add it later if needed.
//removeNothingSelectedMessage(track_list_container);
var event_target = $(event.target);
var control_id = event_target.data('control_id');
if(control_id) {
event_target.remove();
var event_parent_list = event_target.parent('li');
var control_id = event_parent_list.data('control_id');
// console.log('event', event);
// console.log('event_target.parent("li")', event_target.parent('li'));
// console.log('control_id', control_id);
// console.log('event_target.is(term-reference-tree-delete)', event_target.is('term-reference-tree-delete'));
if(event_target.is('.term-reference-tree-button-delete') && control_id) {
event_parent_list.remove();
var checkbox = $('#' + control_id);
checkbox.removeAttr('checked');
@@ -89,7 +120,8 @@
track_list_container, //Where to add new item.
label_element.html(), //Text of new item.
$(label_element).attr('for'), //Id of control new item is for.
input_type //checkbox or radio
input_type, //checkbox or radio
-1 // delta
);
}
else {
@@ -144,8 +176,9 @@
*
* @param control_type Control type - 'checkbox' or 'radio'.
*/
function addItemToTrackList(track_list_container, item_text, control_id, control_type) {
var new_item = $('<li class="track-item">' + item_text + '</li>');
function addItemToTrackList(track_list_container, item_text, control_id, control_type, delta) {
console.log('addItemToTrackList');
var new_item = $('<li class="track-item" delta="'+ delta +'"><div class="term-reference-tree-button-move"></div>' + item_text + '<div class="term-reference-tree-button-delete"></div></li>');
new_item.data('control_id', control_id);
//Add an id for easy finding of the item.
@@ -173,35 +206,66 @@
}
return;
}
//Using checkboxes, so there can be more than one selected item.
//Find the right place to put the new item, to match the order of the
//checkboxes.
//Using checkboxes, so there can be more than one selected item.
//Find the right place to put the new item,
// to match the order of the checkboxes.
// OR order of delta
var list_items = track_list_container.find('li');
var item_comparing_to;
//Flag to tell whether the item was inserted.
var inserted_flag = false;
list_items.each(function(index){
item_comparing_to = $(list_items[index]);
if(!track_list_container.is('.order-list')){
list_items.each(function(index){
item_comparing_to = $(list_items[index]);
//If item is already on the track list, do nothing.
if ( control_id == item_comparing_to.data('control_id') ) {
inserted_flag = true;
return false; //Returning false stops the loop.
}
else if ( control_id < item_comparing_to.data('control_id') ) {
//Add it here.
item_comparing_to.before(new_item);
inserted_flag = true;
return false; //Returning false stops the loop.
}
});
//If item is already on the track list, do nothing.
if ( control_id == item_comparing_to.data('control_id') ) {
inserted_flag = true;
return false; //Returning false stops the loop.
}
else if ( control_id < item_comparing_to.data('control_id') ) {
//Add it here.
item_comparing_to.before(new_item);
inserted_flag = true;
return false; //Returning false stops the loop.
}
});
//If not inserted yet, add new item at the end of the track list.
if ( ! inserted_flag ) {
track_list_container.append(new_item);
}
//If not inserted yet, add new item at the end of the track list.
if ( ! inserted_flag ) {
track_list_container.append(new_item);
}
}else{
if( ! track_list_container.find('#'+new_item.attr('id')).size() ){
if(delta == -1){
track_list_container.append(new_item);
inserted_flag = true;
}else{
list_items.each(function(index){
item_comparing_to = $(this);
if ( delta < item_comparing_to.attr('delta') ) {
//Add it here.
item_comparing_to.before(new_item);
inserted_flag = true;
return false; //Returning false stops the loop.
}
});
//If not inserted yet, add new item at the end of the track list.
if ( ! inserted_flag )
track_list_container.append(new_item);
}
track_list_container.sortable('refresh');
}
}
}
/**
@@ -246,7 +310,7 @@
// This helper function checks if the maximum number of choices is already selected.
// If so, it disables all the other options. If not, it enables them.
function checkMaxChoices(item, checkbox) {
function checkMaxChoices(item, checkbox, order_list) {
var maxChoices = -1;
try {
maxChoices = parseInt(Drupal.settings.term_reference_tree.trees[item.attr('id')]['max_choices']);