praiadeseselle/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectMultiple.js

167 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2022-03-08 15:55:41 +01:00
var InputfieldPageListSelectMultiple = {
2022-11-05 18:32:48 +01:00
selectLabel: 'Select',
selectedLabel: 'Selected',
2022-03-08 15:55:41 +01:00
init: function($inputfield) {
2022-11-05 18:32:48 +01:00
var $t;
2022-03-08 15:55:41 +01:00
if($inputfield.hasClass('InputfieldPageListSelectMultipleData')) {
2022-11-05 18:32:48 +01:00
$t = $inputfield;
2022-03-08 15:55:41 +01:00
} else {
2022-11-05 18:32:48 +01:00
$t = $inputfield.find(".InputfieldPageListSelectMultipleData");
2022-03-08 15:55:41 +01:00
}
if(!$t.length) return;
if($t.hasClass('InputfieldPageListSelectMultipleInit')) return;
2022-11-05 18:32:48 +01:00
InputfieldPageListSelectMultiple.selectLabel = $t.attr('data-select');
InputfieldPageListSelectMultiple.selectedLabel = $t.attr('data-selected');
2022-03-08 15:55:41 +01:00
$t.ProcessPageList({
mode: 'select',
rootPageID: $t.attr('data-root'),
showRootPage: true,
2022-11-05 18:32:48 +01:00
selectMultiple: true,
2022-03-08 15:55:41 +01:00
selectShowPageHeader: false,
selectSelectHref: $t.attr('data-href'),
selectStartLabel: $t.attr('data-start'),
selectCancelLabel: $t.attr('data-cancel'),
selectSelectLabel: $t.attr('data-select'),
selectUnselectLabel: $t.attr('data-unselect'),
moreLabel: $t.attr('data-more'),
labelName: $t.attr('data-labelName')
}).hide().addClass('InputfieldPageListSelectMultipleInit');
$t.on('pageSelected', $t, InputfieldPageListSelectMultiple.pageSelected);
$t.on('pageListChildrenDone', $t, InputfieldPageListSelectMultiple.pageListChildrenDone);
2022-03-08 15:55:41 +01:00
InputfieldPageListSelectMultiple.initList($('#' + $t.attr('id') + '_items'));
},
/**
* Initialize the given InputfieldPageListSelectMultiple OL by making it sortable
*
*/
initList: function($ol) {
var makeSortable = function($ol) {
$ol.sortable({
// items: '.InputfieldPageListSelectMultiple ol > li',
axis: 'y',
update: function(e, data) {
InputfieldPageListSelectMultiple.rebuildInput($(this));
2022-11-05 18:32:48 +01:00
$ol.trigger('sorted', [ data.item ]);
2022-03-08 15:55:41 +01:00
},
start: function(e, data) {
data.item.addClass('ui-state-highlight');
},
stop: function(e, data) {
data.item.removeClass('ui-state-highlight');
}
});
$ol.addClass('InputfieldPageListSelectMultipleSortable');
};
$('#' + $ol.attr('id')).on('mouseover', '>li', function() {
$(this).removeClass('ui-state-default').addClass('ui-state-hover');
if(!$ol.is(".InputfieldPageListSelectMultipleSortable")) makeSortable($ol);
}).on('mouseout', '>li', function() {
$(this).removeClass('ui-state-hover').addClass('ui-state-default');
});
$ol.on('click', 'a.itemRemove', function() {
var $li = $(this).parent();
var $ol = $li.parent();
var id = $li.children(".itemValue").text();
$li.remove();
2022-11-05 18:32:48 +01:00
$ol.closest('.InputfieldPageListSelectMultiple').find('.pw-iplsm-disabled-' + id)
.removeClass('ui-state-disabled pw-iplsm-disabled-' + id)
.text(InputfieldPageListSelectMultiple.selectLabel);
2022-03-08 15:55:41 +01:00
InputfieldPageListSelectMultiple.rebuildInput($ol);
return false;
});
},
2022-11-05 18:32:48 +01:00
/**
* Callback when children have been listed in the pageList
*
* @param e
* @param data
*/
pageListChildrenDone: function(e, data) {
var $t = $(this);
var $inputfield = $t.closest('.Inputfield');
var ids = $t.val().split(',');
for(var n = 0; n < ids.length; n++) {
var id = ids[n];
var $item = $inputfield.find('.PageListID' + id);
// mark items already selected
if($item.length) $item.find('.PageListActionSelect').children('a')
.addClass('ui-state-disabled pw-iplsm-disabled-' + id)
.text(InputfieldPageListSelectMultiple.selectedLabel);
}
},
2022-03-08 15:55:41 +01:00
/**
* Callback function executed when a page is selected from PageList
*
*/
pageSelected: function(e, page) {
$input = e.data;
var $ol = $('#' + $input.attr('id') + '_items');
var $li = $ol.children(".itemTemplate").clone();
2022-11-05 18:32:48 +01:00
page.actionLink
.addClass('ui-state-disabled pw-iplsm-disabled-' + page.id)
.text(InputfieldPageListSelectMultiple.selectedLabel);
2022-03-08 15:55:41 +01:00
$li.removeClass("itemTemplate");
$li.children('.itemValue').text(page.id);
$li.children('.itemLabel').text(page.title);
$ol.append($li);
InputfieldPageListSelectMultiple.rebuildInput($ol);
},
/**
* Rebuild the CSV values present in the hidden input[text] field
*
*/
rebuildInput: function($ol) {
var id = $ol.attr('id');
id = id.substring(0, id.lastIndexOf('_'));
var $input = $('#' + id);
var value = '';
var selected = {};
$ol.children(':not(.itemTemplate)').each(function() {
var $li = $(this);
var v = $li.children('.itemValue').text();
if(typeof selected[v] != "undefined") {
// item already in list
if(jQuery.ui) selected[v].effect('highlight', 1000);
$li.remove();
} else {
// add new item to list
selected[v] = $li;
if(value.length > 0) value += ',';
value += v;
}
});
$input.val(value);
$input.trigger('change');
2022-03-08 15:55:41 +01:00
}
};
$(document).ready(function() {
$(".InputfieldPageListSelectMultiple").each(function() {
InputfieldPageListSelectMultiple.init($(this));
});
$(document).on('reloaded', '.InputfieldPageListSelectMultiple, .InputfieldPage', function() {
InputfieldPageListSelectMultiple.init($(this));
});
});