Skip to content

Commit

Permalink
WIP: Manager JS Umbau. CSP optimiert. (#1439)
Browse files Browse the repository at this point in the history
* Manager JS Umbau. CSP optimiert.

* debug alert entfernt
  • Loading branch information
dergel authored Sep 23, 2023
1 parent 1fad431 commit 772bf2d
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 213 deletions.
4 changes: 4 additions & 0 deletions plugins/manager/assets/frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function() {
$(document).trigger('rex:ready', [$('form')]);
});

80 changes: 1 addition & 79 deletions plugins/manager/assets/manager.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,6 @@
var be_relation_media_counter = 100; // no conflicts to 100 media in one page
var be_relation_counter = 100; // no conflicts to 100 media in one page

rex_retain_popup_event_handlers("rex:YForm_selectData");

function yform_manager_openDatalist(id, field, link, multiple){
newLinkMapWindow(link+'&rex_yform_manager_opener[id]='+id+'&rex_yform_manager_opener[field]='+field+'&rex_yform_manager_opener[multiple]='+multiple);
}
function yform_manager_deleteDatalist(id, multiple){
if(multiple == 1) {
deleteREX(id, 'yform_MANAGER_DATALIST_', 'yform_MANAGER_DATALIST_SELECT_');
} else {
var a = new getObj("yform_MANAGER_DATANAME_"+id);
a.obj.value = "";
var a = new getObj("yform_MANAGER_DATA_"+id);
a.obj.value = "";
}
}
function yform_manager_moveDatalist(id, direction){
moveREX(id, 'yform_MANAGER_DATALIST_', 'yform_MANAGER_DATALIST_SELECT_', direction);
}
function yform_manager_writeDatalist(id){
writeREX(id, 'yform_MANAGER_DATALIST_', 'yform_MANAGER_DATALIST_SELECT_');
}
function yform_manager_setData(id, data_id, data_name, multiple){

var event = opener.jQuery.Event("rex:YForm_selectData");
opener.jQuery(window).trigger(event, [data_id, data_name, multiple]);
if (event.isDefaultPrevented()) {
self.close();
}

if(multiple == 1) {
var datalist = "yform_MANAGER_DATALIST_SELECT_"+id;
var source = opener.document.getElementById(datalist);
var sourcelength = source.options.length;

option = opener.document.createElement("OPTION");
option.text = data_name;
option.value = data_id;

source.options.add(option, sourcelength);
opener.writeREX(id, 'yform_MANAGER_DATALIST_', 'yform_MANAGER_DATALIST_SELECT_');
}else {
var data_field_name = "yform_MANAGER_DATANAME_"+id;
var data_field_id = "yform_MANAGER_DATA_"+id;
opener.document.getElementById(data_field_name).value = data_name;
opener.document.getElementById(data_field_id).value = data_id;
self.close();
}

}

$(document).on('rex:ready', function (event, container) {
container.find('[data-be-relation-wrapper]').each(function() {

be_relation_counter++;

var regexpDataset = [
new RegExp("(moveYFormDatasetList\\(?\\d+)", 'g'),
new RegExp("(openYFormDatasetList\\(?\\d+)", 'g'),
new RegExp("(deleteYFormDatasetList\\(?\\d+)", 'g'),
new RegExp("(openYFormDataset\\(?\\d+)", 'g'),
new RegExp("(deleteYFormDataset\\(?\\d+)", 'g')
];

$(this)
.find("[id^='YFORM_DATASETLIST_SELECT_'],[id^='YFORM_DATASETLIST_FIELD_'],[id^='YFORM_DATASET_SELECT_'],[id^='YFORM_DATASET_FIELD_']").each(function() {
$(this).attr("id", $(this).attr("id") + be_relation_counter);
});

$(this)
.find("[onclick]").each(function() {
var elementOnClick = $(this).attr("onclick");
for (var i in regexpDataset) {
elementOnClick = elementOnClick.replace(regexpDataset[i], '$1' + be_relation_counter);
}
$(this).attr("onclick", elementOnClick);
});

});
container.find('[data-be-media-wrapper]').each(function() {

be_relation_media_counter++;
Expand All @@ -88,7 +10,7 @@ $(document).on('rex:ready', function (event, container) {
$(this).attr("id", $(this).attr("id") + be_relation_media_counter);
});

var regexpMedia = [
let regexpMedia = [
new RegExp("(openREXMedia\\('?\\d+)", 'g'),
new RegExp("(addREXMedia\\('?\\d+)", 'g'),
new RegExp("(deleteREXMedia\\('?\\d+)", 'g'),
Expand Down
239 changes: 178 additions & 61 deletions plugins/manager/assets/widget.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,179 @@

function openYFormDataset(id, field, link)
{
var newWindowLink = link + '&rex_yform_manager_opener[id]='+id+'&rex_yform_manager_opener[field]='+field+'&rex_yform_manager_opener[multiple]=0';
return newWindow( id, newWindowLink, 1200,800,',status=yes,resizable=yes');
}

function openYFormDatasetList(id, field, link)
{
var newWindowLink = link + '&rex_yform_manager_opener[id]='+id+'&rex_yform_manager_opener[field]='+field+'&rex_yform_manager_opener[multiple]=1';
return newWindow( id, newWindowLink, 1200,800,',status=yes,resizable=yes');
}

function deleteYFormDataset(id){
var a;
a = new getObj('YFORM_DATASET_FIELD_' + id);
a.obj.value = '';
a = new getObj('YFORM_DATASET_SELECT_' + id);
a.obj.value = '';
}

function deleteYFormDatasetList(id){
deleteREX(id, 'YFORM_DATASETLIST_FIELD_', 'YFORM_DATASETLIST_SELECT_');
}

function moveYFormDatasetList(id, direction){
moveREX(id, 'YFORM_DATASETLIST_FIELD_', 'YFORM_DATASETLIST_SELECT_', direction);
}

function writeYFormDatasetlist(id){
writeREX(id, 'YFORM_DATASETLIST_FIELD_', 'YFORM_DATASETLIST_SELECT_');
}

function setYFormDataset(id, data_id, data_name, multiple){

var event = opener.jQuery.Event("rex:YForm_selectData");
opener.jQuery(window).trigger(event, [data_id, data_name, multiple]);
if (event.isDefaultPrevented()) {
self.close();
}

if(multiple == 1) {
var datalist = "YFORM_DATASETLIST_SELECT_"+id;
var source = opener.document.getElementById(datalist);
var sourcelength = source.options.length;

option = opener.document.createElement("OPTION");
option.text = data_name;
option.value = data_id;

source.options.add(option, sourcelength);
opener.writeREX(id, 'YFORM_DATASETLIST_FIELD_', 'YFORM_DATASETLIST_SELECT_');

}else {
var data_field_name = 'YFORM_DATASET_SELECT_' + id;
var data_field_id = 'YFORM_DATASET_FIELD_' + id;
opener.document.getElementById(data_field_name).value = data_name;
opener.document.getElementById(data_field_id).value = data_id;
self.close();
}

}
$(document).on('rex:ready',function() {

$(".yform-dataset-widget").each(function () {
let id = this.dataset.id;
let link = this.dataset.link;
let widget_type = this.dataset.widget_type;
let field_name = this.dataset.field_name;

$(this).find("a").each(function () {

let multiple= 1;
if (widget_type === "single") {
multiple = 0;
}

// widget pool -
if (this.classList.contains('yform-dataset-widget-pool')) {
this.onclick = function () {
return newPoolWindow( link);
};
}

// open
if (this.classList.contains('yform-dataset-widget-open')) {
this.onclick = function () {
let newWindowLink = link + '&rex_yform_manager_opener[id]='+id+'&rex_yform_manager_opener[field]='+field_name+'&rex_yform_manager_opener[multiple]='+multiple;
return newWindow( id, newWindowLink, 1200,800,',status=yes,resizable=yes');
};
}

// delete
if (this.classList.contains('yform-dataset-widget-delete')) {
this.onclick = function () {

let viewObject = document.querySelector('#yform-dataset-view-'+id);
let realObject = document.querySelector('#yform-dataset-real-'+id);

if (multiple === 1) {
for (let position = 0; position < viewObject.options.length; position++) {
if (viewObject.options[position].selected) {
viewObject.options[position].remove();

if(position === 0) {
if(viewObject.options.length > 0) {
viewObject.options[0].selected = "selected";
}
} else {
if(viewObject.options.length > position) {
viewObject.options[position].selected= "selected";
} else {
viewObject.options[position-1].selected= "selected";
}
}

realObject.value = "";
for (let i=0; i < viewObject.options.length; i++) {
realObject.value += (viewObject[i].value);
if (viewObject.options.length > (i+1)) realObject.value += ',';
}

break;
}
}

} else {
viewObject.value = '';
realObject.value = '';
}
};
}

// move up and down
if (this.classList.contains('yform-dataset-widget-move')) {
this.onclick = function () {

let viewObject = document.querySelector('#yform-dataset-view-'+id);
let realObject = document.querySelector('#yform-dataset-real-'+id);

for (let position = 0; position < viewObject.options.length; position++) {

if (viewObject.options[position].selected) {
if (this.classList.contains('yform-dataset-widget-move-up')) {
if(position > 0) {
let option_temp_value = viewObject.options[position-1].value;
let option_temp_text = viewObject.options[position-1].text;
viewObject.options[position-1].value = viewObject.options[position].value;
viewObject.options[position-1].text = viewObject.options[position].text;
viewObject.options[position-1].selected= "selected";
viewObject.options[position].value = option_temp_value;
viewObject.options[position].text = option_temp_text;
}
}
if (this.classList.contains('yform-dataset-widget-move-down')) {
if(position < (viewObject.options.length-1)) {
let option_temp_value = viewObject.options[position+1].value;
let option_temp_text = viewObject.options[position+1].text;
viewObject.options[position+1].value = viewObject.options[position].value;
viewObject.options[position+1].text = viewObject.options[position].text;
viewObject.options[position+1].selected= "selected";
viewObject.options[position].value = option_temp_value;
viewObject.options[position].text = option_temp_text;
}
}
if (this.classList.contains('yform-dataset-widget-move-top')) {
let option_temp_value = viewObject.options[position].value;
let option_temp_text = viewObject.options[position].text;
for(let i = position; i > 0; i--) {
viewObject.options[i].value = viewObject.options[i-1].value;
viewObject.options[i].text = viewObject.options[i-1].text;
}
viewObject.options[0].value = option_temp_value;
viewObject.options[0].text = option_temp_text;
viewObject.options[0].selected= "selected";
}
if (this.classList.contains('yform-dataset-widget-move-bottom')) {
let option_temp_value = viewObject.options[position].value;
let option_temp_text = viewObject.options[position].text;
for(let i = position; i < (viewObject.options.length-1); i++) {
viewObject.options[i].value = viewObject.options[i+1].value;
viewObject.options[i].text = viewObject.options[i+1].text;
}
viewObject.options[viewObject.options.length-1].value = option_temp_value;
viewObject.options[viewObject.options.length-1].text = option_temp_text;
}

realObject.value = "";
for (let i=0; i < viewObject.options.length; i++) {
realObject.value += (viewObject[i].value);
if (viewObject.options.length > (i+1)) realObject.value += ',';
}

// !!!
break;
}
}
};
}

// set
if (this.classList.contains('yform-dataset-widget-set')) {
this.onclick = function () {

let id = this.dataset.id;
let opener_id = this.dataset.opener_id;
let opener_field = this.dataset.opener_field;
let multiple = this.dataset.multiple;
let value = this.dataset.value;

if(multiple == "1") {
let viewObject = opener.document.getElementById('yform-dataset-view-'+opener_id);
let option = opener.document.createElement("OPTION");
option.text = value;
option.value = id;
viewObject.options.add(option, viewObject.options.length);

let realObject = opener.document.getElementById('yform-dataset-real-'+opener_id);
realObject.value = "";

for (let i=0; i < viewObject.options.length; i++) {
realObject.value += (viewObject[i].value);
if (viewObject.options.length > (i+1)) realObject.value += ',';
}

} else {
opener.document.getElementById('yform-dataset-view-'+opener_id).value = value;
opener.document.getElementById('yform-dataset-real-'+opener_id).value = id;
self.close();
}

}

}

});

});

});
13 changes: 7 additions & 6 deletions plugins/manager/fragments/yform/manager/page/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ static function ($params) {
}
}
}
return '<a href="javascript:setYFormDataset('.$params['params']['opener_id'].',###id###,\''.rex_escape(
$value,
'js'
).' [id=###id###]\','.$params['params']['opener_multiple'].')">'.rex_i18n::msg(
'yform_data_select'
).'</a>';
return '<span class="yform-dataset-widget"><a
class="btn btn-popup yform-dataset-widget-set"
data-id="###id###"
data-opener_id="'.$params['params']['opener_id'].'"
data-opener_field="'.$params['params']['opener_field'].'"
data-value="'.rex_escape($value, 'html').' [id=###id###]"
data-multiple="'.$params['params']['opener_multiple'].'">'.rex_i18n::msg('yform_data_select').'</a></span>';
},
[
'opener_id' => $rex_yform_manager_opener['id'],
Expand Down
Loading

0 comments on commit 772bf2d

Please sign in to comment.