refactored edlp_ajax_node module to edlp_ajax to be able to get taxonomy terms

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-08 16:44:54 +01:00
parent fb4701f043
commit 3980479031
21 changed files with 248 additions and 206 deletions
@@ -1,11 +1,11 @@
# @Author: Bachir Soussi Chiadmi <bach>
# @Email: bachir@figureslibres.io
# @Filename: edlp_ajax_node.info.yml
# @Filename: edlp_ajax.info.yml
# @License: GPL-V3
name: Edlp Ajax Node
name: Edlp Ajax
type: module
description: Manage ajax node loading for edlp d8.
description: Manage ajax entity loading for edlp d8.
core: 8.x
package: Edlp
@@ -0,0 +1,41 @@
<?php
# @Author: Bachir Soussi Chiadmi <bach>
# @Email: bachir@figureslibres.io
# @Filename: edlp_ajax.module
# @License: GPL-V3
/**
* Implements hook_theme().
*/
function edlp_ajax_theme($existing, $type, $theme, $path) {
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
return array(
'edlp_ajax' => array(
'file' => 'includes/edlp_ajax.inc',
'variables' => array(
'entity_type' => 'node',
'entity' => NULL,
'view_mode' => 'default'
),
),
);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
// dpm($vars);
$suggestions = [];
// $node = $variables['elements']['#node'];
$sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
//
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
// $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
@@ -6,20 +6,20 @@
# @Last modified time: 20-12-2017
# @License: GPL-V3
edlp_ajax_node.node:
path: '/edlp/node/{nid}/{viewmode}'
edlp_ajax.node:
path: '/edlp/ajax/{entity_type}/{id}/{viewmode}'
defaults:
_controller: '\Drupal\edlp_ajax_node\Controller\EdlpAjaxNodeController::node'
_title: 'Node'
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::entity'
_title: 'EdlpAjax'
viewmode: 'default'
requirements:
_permission: 'access content'
edlp_ajax_node.ajaxnode:
path: '/edlp/node/{nid}/ajax/{viewmode}'
edlp_ajax.ajaxnode:
path: '/edlp/ajax/json/{entity_type}/{id}/{viewmode}'
defaults:
_controller: '\Drupal\edlp_ajax_node\Controller\EdlpAjaxNodeController::nodejson'
_title: 'AjaxNode'
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::entityjson'
_title: 'EdlpAjaxJson'
viewmode: 'default'
requirements:
_permission: 'access content'
@@ -0,0 +1,12 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_ajax(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($vars['entity_type']);
$vars['content'] = $view_builder->view($vars['entity'], $vars['view_mode']);
}
@@ -0,0 +1,69 @@
<?php
namespace Drupal\edlp_ajax\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
class EdlpAjaxController extends ControllerBase {
private function query() {
$this->entity = entity_load($this->entity_type, $this->id);
}
private function toRenderable(){
$this->query();
if($this->entity){
return array(
"#theme"=>'edlp_ajax',
"#entity_type" => $this->entity_type,
'#entity' => $this->entity,
'#view_mode' => $this->viewmode,
);
}else{
return array(
'#markup'=>'404! '.$this->entity_type.' '.$this->id." not found!"
);
}
}
/**
* Display entity as a page.
*
* @return renderable array
*/
public function entity($entity_type, $id, $viewmode) {
$this->entity_type = $entity_type;
$this->id = $id;
$this->viewmode = $viewmode;
return $this->toRenderable();
}
/**
* Get entity as json through ajax.
*
* @return json
*/
public function entityjson($entity_type, $id, $viewmode) {
$this->entity_type = $entity_type;
$this->id = $id;
$this->viewmode = $viewmode;
$renderable = $this->toRenderable();
$rendered = render($renderable);
$response = new JsonResponse();
$response->setData([
'test' => 'hello edlp ajax',
'id' => $this->id,
'view_mode' => $this->viewmode,
'bundle' => $this->entity->getType(),
'entity_type' => $this->entity_type,
'rendered'=> $rendered,
]);
return $response;
}
}
@@ -0,0 +1 @@
{{ content }}
@@ -1,22 +0,0 @@
<?php
# @Author: Bachir Soussi Chiadmi <bach>
# @Email: bachir@figureslibres.io
# @Filename: edlp_ajax_node.module
# @License: GPL-V3
/**
* Implements hook_theme().
*/
function edlp_ajax_node_theme($existing, $type, $theme, $path) {
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
return array(
'edlp_ajax_node' => array(
'file' => 'includes/edlp_ajax_node.inc',
'variables' => array(
'node_object' => NULL,
'view_mode' => 'default'
),
),
);
}
@@ -1,12 +0,0 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_ajax_node(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$vars['node'] = $view_builder->view($vars['node_object'], $vars['view_mode']);
}
@@ -1,64 +0,0 @@
<?php
namespace Drupal\edlp_ajax_node\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
class EdlpAjaxNodeController extends ControllerBase {
private function query() {
$this->node = entity_load('node', $this->nid);
}
private function toRenderable(){
$this->query();
if($this->node){
return array(
"#theme"=>'edlp_ajax_node',
'#node_object' => $this->node,
'#view_mode' => $this->viewmode,
);
}else{
return array(
'#markup'=>'404! Node '.$this->nid." not found!"
);
}
}
/**
* Display node as a page.
*
* @return renderable array
*/
public function node($nid, $viewmode) {
$this->nid = $nid;
$this->viewmode = $viewmode;
return $this->toRenderable();
}
/**
* Get node as json through ajax.
*
* @return json
*/
public function nodejson($nid, $viewmode) {
$this->nid = $nid;
$this->viewmode = $viewmode;
$renderable = $this->toRenderable();
$rendered = render($renderable);
$response = new JsonResponse();
$response->setData([
'test' => 'hello edlp ajax node',
'nid' => $this->nid,
'view_mode' => $this->viewmode,
'rendered'=> $rendered,
]);
return $response;
}
}
@@ -1 +0,0 @@
{{ node }}
@@ -155,41 +155,30 @@
this.r = 5;
break;
}
// this.base_radius = _base_radius;
// this.g = {
// l : 1, // drawing line width
// s : 1 // drawing space between squares
// };
// this.real_radius = this.base_radius + (this.g.l+this.g.s)*this.entrees.length;
// prototype function not available on construct :(
// this.calcWallLimits();
this.wall_limits = {
top: _canvas_props.margin_top +this.r,
right: _canvas.width -_canvas_props.margin_right -this.r,
bottom: _canvas.height -_canvas_props.margin_bottom -this.r,
left: _canvas_props.margin_left +this.r
}
this.x = this.wall_limits.left + Math.random()*(this.wall_limits.right - this.wall_limits.left);
this.y = this.wall_limits.top + Math.random()*(this.wall_limits.bottom - this.wall_limits.top);
this.e_color = 'e_col_'+this.entrees[Math.floor(Math.random(this.entrees.length))];
this.hover = false;
this.opened = false;
// physics
this.p = _physics.makeParticle(this.mass, this.x, this.y);
this.p.velocity = new Physics.Vector((Math.random()-0.5)*_p_velocity_factor, (Math.random()-0.5)*_p_velocity_factor);
// if(this.id == '620'){
// _node_pop_up.setNode(this);
// }
// prototypes
if (typeof Node.initialized == "undefined") {
Node.prototype.init = function(){
this.calcWallLimits();
this.x = this.wall_limits.left + Math.random()*(this.wall_limits.right - this.wall_limits.left);
this.y = this.wall_limits.top + Math.random()*(this.wall_limits.bottom - this.wall_limits.top);
// physics
this.p = _physics.makeParticle(this.mass, this.x, this.y);
this.p.velocity = new Physics.Vector((Math.random()-0.5)*_p_velocity_factor, (Math.random()-0.5)*_p_velocity_factor);
// if(this.id == '620'){
// _node_pop_up.setNode(this);
// }
};
Node.prototype.calcWallLimits = function(){
this.wall_limits = {
top: _canvas_props.margin_top +this.r,
@@ -223,6 +212,7 @@
if (Math.abs(this.p.velocity.x) < this.velocity_threshold
&& Math.abs(this.p.velocity.y) < this.velocity_threshold){
this.p.velocity.multiplyScalar(0);
// this.p.makeFixed();
}
};
@@ -287,8 +277,8 @@
// actif entouré de rouge
_ctx.beginPath();
// _ctx.fillStyle = !this.p.resting() ? edlp_vars[this.e_color] : 'rgb(0, 0, 0)';
_ctx.fillStyle = edlp_vars[this.e_color];
_ctx.fillStyle = !this.p.resting() ? edlp_vars[this.e_color] : 'rgb(0, 0, 0)';
// _ctx.fillStyle = edlp_vars[this.e_color];
_ctx.fillRect(this.x - this.r,this.y - this.r,this.r*2,this.r*2);
if(this.opened){
@@ -303,7 +293,7 @@
Node.initialized = true;
}
// this.init();
this.init();
};
// TODO: we may convert a lot of computation into a web worker https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
function checkParticulesCollisions(){
@@ -155,41 +155,30 @@
this.r = 5;
break;
}
// this.base_radius = _base_radius;
// this.g = {
// l : 1, // drawing line width
// s : 1 // drawing space between squares
// };
// this.real_radius = this.base_radius + (this.g.l+this.g.s)*this.entrees.length;
// prototype function not available on construct :(
// this.calcWallLimits();
this.wall_limits = {
top: _canvas_props.margin_top +this.r,
right: _canvas.width -_canvas_props.margin_right -this.r,
bottom: _canvas.height -_canvas_props.margin_bottom -this.r,
left: _canvas_props.margin_left +this.r
}
this.x = this.wall_limits.left + Math.random()*(this.wall_limits.right - this.wall_limits.left);
this.y = this.wall_limits.top + Math.random()*(this.wall_limits.bottom - this.wall_limits.top);
this.e_color = 'e_col_'+this.entrees[Math.floor(Math.random(this.entrees.length))];
this.hover = false;
this.opened = false;
// physics
this.p = _physics.makeParticle(this.mass, this.x, this.y);
this.p.velocity = new Physics.Vector((Math.random()-0.5)*_p_velocity_factor, (Math.random()-0.5)*_p_velocity_factor);
// if(this.id == '620'){
// _node_pop_up.setNode(this);
// }
// prototypes
if (typeof Node.initialized == "undefined") {
Node.prototype.init = function(){
this.calcWallLimits();
this.x = this.wall_limits.left + Math.random()*(this.wall_limits.right - this.wall_limits.left);
this.y = this.wall_limits.top + Math.random()*(this.wall_limits.bottom - this.wall_limits.top);
// physics
this.p = _physics.makeParticle(this.mass, this.x, this.y);
this.p.velocity = new Physics.Vector((Math.random()-0.5)*_p_velocity_factor, (Math.random()-0.5)*_p_velocity_factor);
// if(this.id == '620'){
// _node_pop_up.setNode(this);
// }
};
Node.prototype.calcWallLimits = function(){
this.wall_limits = {
top: _canvas_props.margin_top +this.r,
@@ -223,6 +212,7 @@
if (Math.abs(this.p.velocity.x) < this.velocity_threshold
&& Math.abs(this.p.velocity.y) < this.velocity_threshold){
this.p.velocity.multiplyScalar(0);
// this.p.makeFixed();
}
};
@@ -287,8 +277,8 @@
// actif entouré de rouge
_ctx.beginPath();
// _ctx.fillStyle = !this.p.resting() ? edlp_vars[this.e_color] : 'rgb(0, 0, 0)';
_ctx.fillStyle = edlp_vars[this.e_color];
_ctx.fillStyle = !this.p.resting() ? edlp_vars[this.e_color] : 'rgb(0, 0, 0)';
// _ctx.fillStyle = edlp_vars[this.e_color];
_ctx.fillRect(this.x - this.r,this.y - this.r,this.r*2,this.r*2);
if(this.opened){
@@ -303,7 +293,7 @@
Node.initialized = true;
}
// this.init();
this.init();
};
// TODO: we may convert a lot of computation into a web worker https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
function checkParticulesCollisions(){