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(){
@@ -120,7 +120,7 @@ edlp_vars = {
// cartel functions
loadNode(nid){
this.$cartel.addClass('loading');
$.getJSON('/edlp/node/'+nid+'/ajax/player_cartel', {})
$.getJSON('/edlp/ajax/json/node/'+nid+'/player_cartel', {})
.done(this.onNodeLoaded.bind(this))
.fail(this.onNodeLoadFail.bind(this));
},
@@ -242,11 +242,7 @@ edlp_vars = {
var $this = $(this);
// avoid already ajaxified links
// if($this.is('.ajax-enable')) return;
var sys_path = $this.attr('data-drupal-link-system-path');
if(sys_path){
// convert node link to edlp_ajax_node module links
m = sys_path.match(/^\/?(node\/\d+)$/g);
if(m) $this.attr('data-drupal-link-system-path', 'edlp/'+m[0]);
if($this.attr('data-drupal-link-system-path')){
$this.on('click', onClickAjaxLink).addClass('ajax-enable');
}
});
@@ -260,19 +256,28 @@ edlp_vars = {
return false;
var sys_path = $(this).attr('data-drupal-link-system-path');
var ajax_path = sys_path;
if(sys_path == '<front>'){
backToFrontPage();
return false;
}
m = ajax_path.match(/^\/?(node\/\d+)$/g);
if(m){
// convert node link to edlp_ajax_node module links
ajax_path = 'edlp/ajax/json/'+m[0];
// check for viewmode attribute
if($link.attr('viewmode')){
ajax_path += '/'+$link.attr('viewmode');
}
}else{
// convert other link to ajax
ajax_path += '/ajax'
}
var path = window.location.origin + drupalSettings.path.baseUrl + sys_path;
_$body.addClass('ajax-loading');
$link.addClass('ajax-loading');
// check for viewmode attribute
path += '/ajax';
if($link.attr('viewmode') != ''){
path += '/'+$link.attr('viewmode');
}
var path = window.location.origin + drupalSettings.path.baseUrl + ajax_path;
$.getJSON(path, {})
.done(function(data){
onAjaxLinkLoaded(data, $link, sys_path);
@@ -298,9 +303,16 @@ edlp_vars = {
_$content_container.html(data.rendered);
// add body class for currently loaded content
_$body.removeClass().addClass('path-'+sys_path.replace(/\//g, '-'));
var body_classes = [
'path-'+sys_path.replace(/\//g, '-'),
'entity-type-'+data.entity_type,
'bundle-'+data.bundle,
'view-mode-'+data.view_mode
];
_$body.removeClass().addClass(body_classes.join(' '));
// id node add a generic path-node class to body
m = sys_path.match(/^\/?(edlp\/node\/\d+)$/g);
m = sys_path.match(/^\/?(node\/\d+)$/g);
if(m)
_$body.addClass('path-edlp-node');
@@ -1607,7 +1607,7 @@ footer {
footer .block-block-edlp-entrees {
pointer-events: none;
display: inline-block; }
body:not(.path-frontpage) footer .block-block-edlp-entrees {
body.path-productions footer .block-block-edlp-entrees {
display: none; }
footer .block-block-edlp-entrees ul {
white-space: nowrap; }
@@ -97,7 +97,7 @@
// cartel functions
loadNode(nid){
this.$cartel.addClass('loading');
$.getJSON('/edlp/node/'+nid+'/ajax/player_cartel', {})
$.getJSON('/edlp/ajax/json/node/'+nid+'/player_cartel', {})
.done(this.onNodeLoaded.bind(this))
.fail(this.onNodeLoadFail.bind(this));
},
@@ -219,11 +219,7 @@
var $this = $(this);
// avoid already ajaxified links
// if($this.is('.ajax-enable')) return;
var sys_path = $this.attr('data-drupal-link-system-path');
if(sys_path){
// convert node link to edlp_ajax_node module links
m = sys_path.match(/^\/?(node\/\d+)$/g);
if(m) $this.attr('data-drupal-link-system-path', 'edlp/'+m[0]);
if($this.attr('data-drupal-link-system-path')){
$this.on('click', onClickAjaxLink).addClass('ajax-enable');
}
});
@@ -237,19 +233,28 @@
return false;
var sys_path = $(this).attr('data-drupal-link-system-path');
var ajax_path = sys_path;
if(sys_path == '<front>'){
backToFrontPage();
return false;
}
m = ajax_path.match(/^\/?(node\/\d+)$/g);
if(m){
// convert node link to edlp_ajax_node module links
ajax_path = 'edlp/ajax/json/'+m[0];
// check for viewmode attribute
if($link.attr('viewmode')){
ajax_path += '/'+$link.attr('viewmode');
}
}else{
// convert other link to ajax
ajax_path += '/ajax'
}
var path = window.location.origin + drupalSettings.path.baseUrl + sys_path;
_$body.addClass('ajax-loading');
$link.addClass('ajax-loading');
// check for viewmode attribute
path += '/ajax';
if($link.attr('viewmode') != ''){
path += '/'+$link.attr('viewmode');
}
var path = window.location.origin + drupalSettings.path.baseUrl + ajax_path;
$.getJSON(path, {})
.done(function(data){
onAjaxLinkLoaded(data, $link, sys_path);
@@ -275,9 +280,16 @@
_$content_container.html(data.rendered);
// add body class for currently loaded content
_$body.removeClass().addClass('path-'+sys_path.replace(/\//g, '-'));
var body_classes = [
'path-'+sys_path.replace(/\//g, '-'),
'entity-type-'+data.entity_type,
'bundle-'+data.bundle,
'view-mode-'+data.view_mode
];
_$body.removeClass().addClass(body_classes.join(' '));
// id node add a generic path-node class to body
m = sys_path.match(/^\/?(edlp\/node\/\d+)$/g);
m = sys_path.match(/^\/?(node\/\d+)$/g);
if(m)
_$body.addClass('path-edlp-node');
@@ -590,7 +590,7 @@ footer{
.block-block-edlp-entrees{
pointer-events: none;
body:not(.path-frontpage) & {display:none}
body.path-productions & {display:none}
display: inline-block;
// vertical-align: top;
@@ -1,7 +0,0 @@
<div class="row">
<div class="col small-col-12 med-col-4 large-col-4">
<div class="wrapper">
{{ node }}
</div>
</div>
</div>
@@ -0,0 +1,7 @@
<div class="row">
<div class="col small-col-12 med-col-6 large-col-6">
<div class="wrapper">
{{ content }}
</div>
</div>
</div>
@@ -0,0 +1,7 @@
<div class="row">
<div class="col small-col-12 med-col-6 large-col-6">
<div class="wrapper">
{{ content }}
</div>
</div>
</div>
@@ -0,0 +1,7 @@
<div class="row">
<div class="col small-col-12 med-col-6 large-col-6">
<div class="wrapper">
{{ content }}
</div>
</div>
</div>
+1 -1
View File
@@ -28,7 +28,7 @@ module:
editor: 0
edlp_admin: 0
edlp_agenda: 0
edlp_ajax_node: 0
edlp_ajax: 0
edlp_corpus: 0
edlp_fils: 0
edlp_home: 0