boussole: moving entity points is ok, and it records the new values on the form, so it can be recorded
This commit is contained in:
parent
1410059001
commit
7e4e6dca11
|
@ -3,11 +3,15 @@
|
||||||
Drupal.behaviors.boussole = {
|
Drupal.behaviors.boussole = {
|
||||||
attach: function (context, settings) {
|
attach: function (context, settings) {
|
||||||
console.log("Drupal Boussole behavior", context);
|
console.log("Drupal Boussole behavior", context);
|
||||||
|
let $boussole_wrapper = document.querySelector('#boussole-layout');
|
||||||
once('boussole-class-behaviour', '#boussole-layout', context).forEach(element => {
|
once('boussole-class-behaviour', '#boussole-layout', context).forEach(element => {
|
||||||
_boussole = new Boussole();
|
let tabs =
|
||||||
|
// div.field-group-tabs-wrapper
|
||||||
|
// input.horizontal-tabs-active-tab
|
||||||
|
_boussole = new Boussole($boussole_wrapper);
|
||||||
});
|
});
|
||||||
|
|
||||||
let $form = document.querySelector('#boussole-layout>.form-item');
|
let $form = $boussole_wrapper.querySelector(':scope>.form-item');
|
||||||
console.log($form);
|
console.log($form);
|
||||||
let $tabledrag = $form.querySelector('table.field-multiple-table');
|
let $tabledrag = $form.querySelector('table.field-multiple-table');
|
||||||
console.log($tabledrag);
|
console.log($tabledrag);
|
||||||
|
@ -47,19 +51,74 @@
|
||||||
entities[id] = e;
|
entities[id] = e;
|
||||||
});
|
});
|
||||||
console.log(entities);
|
console.log(entities);
|
||||||
_boussole.updateEntities(entities);
|
_boussole.updateCreateEntities(entities);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Boussole {
|
class Boussole {
|
||||||
constructor() {
|
constructor($boussole_wrapper) {
|
||||||
|
this.$boussole_wrapper = $boussole_wrapper;
|
||||||
this.$boussole = document.querySelector('#boussole-layout>.boussole-wrapper>.boussole');
|
this.$boussole = document.querySelector('#boussole-layout>.boussole-wrapper>.boussole');
|
||||||
|
|
||||||
this.entities = {}
|
this.entities = {}
|
||||||
|
|
||||||
|
this.initTabChecking();
|
||||||
|
this.enableDragArea()
|
||||||
}
|
}
|
||||||
|
|
||||||
updateEntities(entities){
|
initTabChecking(){
|
||||||
console.log('Class Boussole update entities', entities);
|
// debugger
|
||||||
|
this.$field_group_tab = this.$boussole_wrapper.closest('details.field-group-tab');
|
||||||
|
// watch for tabs opening and closing
|
||||||
|
// hidden input do not triggers events
|
||||||
|
// so we have to use mutationObserver tomake it trigger the change event
|
||||||
|
this.$tabInput = this.$field_group_tab.parentElement.querySelector(':scope>input[type="hidden"]');
|
||||||
|
let observer = new MutationObserver(function(mutations, observer) {
|
||||||
|
if(mutations[0].attributeName == "value") {
|
||||||
|
// console.log('mutation observed', mutations, observer);
|
||||||
|
let event = new Event('change');
|
||||||
|
mutations[0].target.dispatchEvent(event);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
observer.observe(this.$tabInput, {attributes: true});
|
||||||
|
// then we listen to the event
|
||||||
|
this.$tabInput.addEventListener('change', event => {
|
||||||
|
// console.log('tab input changed', event, this);
|
||||||
|
this.wrapperTabChanged(event);
|
||||||
|
});
|
||||||
|
this.checkTabIsOpen();
|
||||||
|
}
|
||||||
|
|
||||||
|
checkTabIsOpen(){
|
||||||
|
this.tabisopen = this.$field_group_tab.getAttribute('id') === this.$tabInput.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapperTabChanged(event){
|
||||||
|
this.checkTabIsOpen();
|
||||||
|
if(this.tabisopen){
|
||||||
|
this.updateEntities();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enableDragArea(){
|
||||||
|
this.$boussole.addEventListener("dragenter", (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
});
|
||||||
|
this.$boussole.addEventListener("dragover", (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
});
|
||||||
|
this.$boussole.addEventListener("drop", (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
let data = JSON.parse(event.dataTransfer.getData('text/plain'));
|
||||||
|
console.log('boussole drop', event, data);
|
||||||
|
data.clientX = event.clientX;
|
||||||
|
data.clientY = event.clientY;
|
||||||
|
// return on the entity class
|
||||||
|
this.entities[data.entity_id].dropedOnBoussole(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
updateCreateEntities(entities){
|
||||||
|
// console.log('Class Boussole update entities', entities);
|
||||||
for (const [id, values] of Object.entries(entities)) {
|
for (const [id, values] of Object.entries(entities)) {
|
||||||
// console.log(id, typeof this.entities[id], values);
|
// console.log(id, typeof this.entities[id], values);
|
||||||
if (typeof this.entities[id] === 'undefined') {
|
if (typeof this.entities[id] === 'undefined') {
|
||||||
|
@ -69,6 +128,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateEntities(){
|
||||||
|
for (const [id, entity] of Object.entries(this.entities)) {
|
||||||
|
entity.update();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
|
@ -92,6 +157,7 @@
|
||||||
createDomElemt(){
|
createDomElemt(){
|
||||||
this.$e = document.createElement('div');
|
this.$e = document.createElement('div');
|
||||||
this.$e.classList.add('entity');
|
this.$e.classList.add('entity');
|
||||||
|
this.$e.setAttribute('id', `${this.id}-boussole-point`);
|
||||||
this.$e.setAttribute('title', this.values.title.value);
|
this.$e.setAttribute('title', this.values.title.value);
|
||||||
this.$container.append(this.$e);
|
this.$container.append(this.$e);
|
||||||
console.log(this.$e);
|
console.log(this.$e);
|
||||||
|
@ -100,37 +166,60 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
initDraggable(){
|
initDraggable(){
|
||||||
// // this is working but to slow, we loose the item
|
|
||||||
// this.$e.addEventListener('mousedown', function(event){
|
|
||||||
// this.mousedown = true;
|
|
||||||
// this.moving_x = this.$e.offsetLeft - event.clientX;
|
|
||||||
// this.moving_y = this.$e.offsetTop - event.clientY;
|
|
||||||
// }.bind(this));
|
|
||||||
// this.$e.addEventListener('mouseup', function(event){
|
|
||||||
// this.mousedown = false;
|
|
||||||
// }.bind(this));
|
|
||||||
// this.$e.addEventListener('mousemove', function(event){
|
|
||||||
// if (this.mousedown) {
|
|
||||||
// this.$e.style.left = event.clientX + this.moving_x + 'px';
|
|
||||||
// this.$e.style.top = event.clientY + this.moving_y + 'px';
|
|
||||||
// }
|
|
||||||
// }.bind(this));
|
|
||||||
this.$e.setAttribute('draggable', true);
|
this.$e.setAttribute('draggable', true);
|
||||||
this.$e.addEventListener('dragstart', function(event){
|
this.$e.addEventListener('dragstart', event => {
|
||||||
event.dataTransfer.setData('text/plain', event.target.id);
|
// console.log('dragstart', event);
|
||||||
setTimeout(() => {
|
let data = {
|
||||||
event.target.classList.add('hide');
|
entity_id : this.id,
|
||||||
}, 0);
|
dragging_start_x : event.target.offsetLeft - event.clientX,
|
||||||
}.bind(this));
|
dragging_start_y : event.target.offsetTop - event.clientY
|
||||||
this.$e.addEventListener('drag', function(event){
|
};
|
||||||
console.log('drag', event);
|
event.dataTransfer.setData('text/plain', JSON.stringify(data));
|
||||||
}.bind(this));
|
// event.dataTransfer.effectAllowed = "move";
|
||||||
this.$e.addEventListener('dragend', function(event){
|
setTimeout(() => {event.target.classList.add('hide');}, 0);
|
||||||
console.log('dragend', event);
|
});
|
||||||
event.target.style.top = event.clientY + 'px';
|
this.$e.addEventListener('dragend', event => {
|
||||||
event.target.style.left = event.clientX + 'px';
|
// console.log('dragend', event);
|
||||||
event.target.classList.remove('hide');
|
event.target.classList.remove('hide');
|
||||||
}.bind(this))
|
});
|
||||||
|
// CAN'T DO THAT, EVENT IS FIRED FOR EACH ENTITIES
|
||||||
|
// TAKING CARE OF THE DROP IN THE BOUSSOLE CLASS
|
||||||
|
// this.$container.addEventListener('drop', event =>{
|
||||||
|
// event.preventDefault();
|
||||||
|
// console.log('drop', event);
|
||||||
|
// })
|
||||||
|
}
|
||||||
|
|
||||||
|
dropedOnBoussole(data){
|
||||||
|
this.$e.style.left = data.clientX + data.dragging_start_x + 'px';
|
||||||
|
this.$e.style.top = data.clientY + data.dragging_start_y + 'px';
|
||||||
|
this.updateValuesFromBoussole();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateValuesFromBoussole(){
|
||||||
|
let compstyle = window.getComputedStyle(this.$e);
|
||||||
|
// AF
|
||||||
|
let top = parseInt(compstyle.top);
|
||||||
|
let af = top >= this.sceneSize.h/2 ? 0 : 1;
|
||||||
|
this.values.af.value = af;
|
||||||
|
this.values.af.$dom.querySelector(`input[value="${af}"]`).checked = true;
|
||||||
|
|
||||||
|
// (x, y)
|
||||||
|
let x = parseInt(compstyle.left) - this.sceneSize.h/2;
|
||||||
|
let y = Math.abs(this.sceneSize.h/2 - top);
|
||||||
|
let a = parseFloat(Math.atan(x/y) * (180/Math.PI)).toFixed(2);
|
||||||
|
let r = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
|
||||||
|
|
||||||
|
this.values.mm.value = this.values.mm.$dom.value = a;
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/5731863/mapping-a-numeric-range-onto-another
|
||||||
|
// from range 0 -> rayon_de_la_boussole to range 0 -> 100
|
||||||
|
let r_max = this.sceneSize.w/2;
|
||||||
|
let r_slope = (100 - 0) / (r_max - 0);
|
||||||
|
this.values.p.value = this.values.p.$dom.value = parseFloat(0 + r_slope * (r - 0)).toFixed(2);
|
||||||
|
// this.values.p.$dom.value = this.values.p.value;
|
||||||
|
|
||||||
|
console.log('x:', x, 'r:', r, 'a:', a, "y:", y);
|
||||||
}
|
}
|
||||||
|
|
||||||
listenToFormValue(){
|
listenToFormValue(){
|
||||||
|
@ -149,17 +238,6 @@
|
||||||
this.updatePos();
|
this.updatePos();
|
||||||
}.bind(this, v));
|
}.bind(this, v));
|
||||||
})
|
})
|
||||||
// this.values.af.$dom.addEventListener('change', function(event) {
|
|
||||||
// console.log('af changed', event);
|
|
||||||
// let af = event.target.parent('.fieldset-wrapper').querySelector('input:checked').value;
|
|
||||||
// this.values.af.value = parseInt(af);
|
|
||||||
// console.log(`new af`, this.values.af.value);
|
|
||||||
// this.updatePos();
|
|
||||||
// }.bind(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
updateValuesFromForm(values){
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePos(){
|
updatePos(){
|
||||||
|
@ -170,6 +248,7 @@
|
||||||
// from range 0 -> 100 to range 0 -> diagonale de la scene
|
// from range 0 -> 100 to range 0 -> diagonale de la scene
|
||||||
// let diagonale = Math.sqrt(Math.pow(this.sceneSize.w, 2) + Math.pow(this.sceneSize.h, 2))/2;
|
// let diagonale = Math.sqrt(Math.pow(this.sceneSize.w, 2) + Math.pow(this.sceneSize.h, 2))/2;
|
||||||
// let r_max = diagonale;
|
// let r_max = diagonale;
|
||||||
|
// actualy we don't use diagonale, we stay on the circle
|
||||||
let r_max = this.sceneSize.w/2;
|
let r_max = this.sceneSize.w/2;
|
||||||
let r_slope = (r_max - 0) / (100 - 0)
|
let r_slope = (r_max - 0) / (100 - 0)
|
||||||
let r = parseInt(0 + r_slope * (this.values.p.value - 0));
|
let r = parseInt(0 + r_slope * (this.values.p.value - 0));
|
||||||
|
@ -181,14 +260,19 @@
|
||||||
let af = this.values.af.value;
|
let af = this.values.af.value;
|
||||||
|
|
||||||
let left = this.sceneSize.w / 2 + x;
|
let left = this.sceneSize.w / 2 + x;
|
||||||
let top = this.sceneSize.h / 2 - (af ? -y : y);
|
let top = this.sceneSize.h / 2 - (af ? y : -y);
|
||||||
// console.log('left:', left, "top:", top);
|
// console.log('left:', left, "top:", top);
|
||||||
|
|
||||||
this.$e.style.left = left+'px';
|
this.$e.style.left = left+'px';
|
||||||
this.$e.style.top = top+'px';
|
this.$e.style.top = top+'px';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
update(){
|
||||||
|
console.log('Entity update');
|
||||||
|
this.updateSceneSize()
|
||||||
|
this.updatePos()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue