studio chutier is updating when documents added or removed, also added remove link in document list

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-12 18:33:48 +01:00
parent 119f89abca
commit 76dd9ddbc9
11 changed files with 253 additions and 107 deletions
@@ -1,23 +1,33 @@
(function ($, Drupal) { (function ($, Drupal, drupalSettings) {
var settings = drupalSettings.edlp_studio;
function init(){ function init(){
console.log('Studio Init'); console.log('Studio Init');
initEvents(); initEvents();
initAjax(); initAjaxChutier();
}; };
function initEvents(){ function initEvents(){
$('body').on('new-audio-cartel-loaded', initAjax); $('body')
.on('new-audio-cartel-loaded', initAjaxChutier)
.on('new-content-ajax-loaded', initAjaxChutier);
}; };
function initAjax(){
// ___ _ _ _ _ _ _ _ _ _
// / __| |_ _ _| |_(_)___ _ _ | \| |___ __| |___ | | (_)_ _ | |__ ___
// | (__| ' \ || | _| / -_) '_| | .` / _ \/ _` / -_) | |__| | ' \| / /(_-<
// \___|_||_\_,_|\__|_\___|_| |_|\_\___/\__,_\___| |____|_|_||_|_\_\/__/
function initAjaxChutier(){
console.log('studio initAjax chutier'); console.log('studio initAjax chutier');
$('.chutier-ajax-link:not(.ajax-enabled)') $('.chutier-ajax-link:not(.ajax-enabled)')
.addClass('ajax-enabled') .addClass('ajax-enabled')
.on('click', onClickAjaxLink); .on('click', onClickChutierAjaxLink);
}; };
function onClickAjaxLink(e){ function onClickChutierAjaxLink(e){
console.log('studio onClickAjaxLink chutier'); console.log('studio onClickAjaxLink chutier');
e.preventDefault(); e.preventDefault();
addRemoveToChutier($(this)); addRemoveToChutier($(this));
@@ -47,7 +57,8 @@
'target_id':$link.attr('target_id'), 'target_id':$link.attr('target_id'),
}); });
$link.replaceWith(data.new_link); $link.replaceWith(data.new_link);
initAjax(); initAjaxChutier();
updateStudioChutier();
// TODO: reload Studio chutier_ui's documents list // TODO: reload Studio chutier_ui's documents list
}; };
@@ -55,5 +66,41 @@
console.warn('add to chuttier load failed : '+error, jqxhr.responseText); console.warn('add to chuttier load failed : '+error, jqxhr.responseText);
}; };
// ___ _ _ _ _
// / __| |_ _ _ __| (_)___ ___ _ _(_)
// \__ \ _| || / _` | / _ \___| || | |
// |___/\__|\_,_\__,_|_\___/ \_,_|_|
// this seems dirty
function updateStudioChutier(){
$studioChutier = $('#studio-ui .chutier_ui');
if($studioChutier.length){
var ajax_path = settings.chutier_ui_ajax;
var path = window.location.origin + drupalSettings.path.baseUrl + ajax_path;
$studioChutier.addClass('loading');
$.getJSON(path, {})
.done(function(data){
onLoadedChutier($studioChutier, data);
})
.fail(function(jqxhr, textStatus, error){
onLoadChutierError(jqxhr, textStatus, error, $studioChutier);
});
}
};
function onLoadedChutier($studioChutier, data){
$studioChutier.replaceWith(data.rendered);
initAjaxChutier();
$('body').trigger({
'type':'on-studio-chutier-updated'
});
}
function onLoadChutierError(jqxhr, textStatus, error, $studioChutier){
console.warn('Chutier load failed', jqxhr.responseText);
$studioChutier.removeClass('loading');
}
init(); init();
})(jQuery, Drupal); })(jQuery, Drupal, drupalSettings);
@@ -30,9 +30,10 @@ function edlp_studio_help($route_name, RouteMatchInterface $route_match) {
*/ */
function edlp_studio_page_attachments(array &$attachments) { function edlp_studio_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'edlp_studio/edlp_studio-library'; $attachments['#attached']['library'][] = 'edlp_studio/edlp_studio-library';
$url = Url::fromRoute('edlp_studio.studio_chutier_ui_ajax', [], ['absolute' => TRUE]);
$attachments['#attached']['drupalSettings']['edlp_studio']['chutier_ui_ajax'] = $url->getInternalPath();
} }
/** /**
* Implements hook_theme(). * Implements hook_theme().
*/ */
@@ -45,6 +46,7 @@ function edlp_studio_theme($existing, $type, $theme, $path) {
'variables' => array( 'variables' => array(
'title' => t('Chutier'), 'title' => t('Chutier'),
'document_nodes' => array(), 'document_nodes' => array(),
'uid' => null,
), ),
), ),
'edlp_composition_ui' => array( 'edlp_composition_ui' => array(
@@ -64,8 +66,6 @@ function edlp_studio_theme($existing, $type, $theme, $path) {
); );
} }
/** /**
* hook_entity_extra_field_info() * hook_entity_extra_field_info()
* *
@@ -82,7 +82,6 @@ function edlp_studio_entity_extra_field_info(){
return $extra; return $extra;
} }
/** /**
* Implements hook_ENTITY_TYPE_view(). * Implements hook_ENTITY_TYPE_view().
* @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8 * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
@@ -30,3 +30,11 @@ edlp_studio.studio_ui_ajax:
_title: 'Studio User Interface' _title: 'Studio User Interface'
requirements: requirements:
_permission: 'use chutier' _permission: 'use chutier'
edlp_studio.studio_chutier_ui_ajax:
path: '/edlp/studio/chutier-ui/ajax'
defaults:
_controller: '\Drupal\edlp_studio\Controller\StudioUIController::StudioChutierUIJson'
_title: 'Chutier of Studio User Interface'
requirements:
_permission: 'use chutier'
@@ -3,6 +3,8 @@
use \Drupal\Core\Url; use \Drupal\Core\Url;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\edlp_studio\Entity\Chutier;
function template_preprocess_edlp_chutier_ui(&$vars){ function template_preprocess_edlp_chutier_ui(&$vars){
// dpm($vars); // dpm($vars);
// $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node'); // $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
@@ -34,9 +36,19 @@ function template_preprocess_edlp_chutier_ui(&$vars){
foreach ($node->get('field_entrees')->getValue() as $key => $term) { foreach ($node->get('field_entrees')->getValue() as $key => $term) {
$entrees .= '<span class="entree" tid="'.$term['target_id'].'"></span>'; $entrees .= '<span class="entree" tid="'.$term['target_id'].'"></span>';
} }
//
// get the remove url (obviously as we are in chutier all contents actions are remove ;)
$remove_url = Chutier::getActionsUrl($node->id(), $vars['uid']);
// build the link // build the link
$vars['documents']['#items'][] = array( $vars['documents']['#items'][] = array(
'#prefix' => '<div class="entrees">'.$entrees.'</div>', '#type' => 'container',
'entrees' => array(
'#type' => 'container',
'#attributes'=>['class'=>['entrees']],
'#markup'=>$entrees,
),
'link'=>array(
'#title' => $trunc_title, '#title' => $trunc_title,
'#type' => 'link', '#type' => 'link',
'#url' => $url, '#url' => $url,
@@ -49,8 +61,19 @@ function template_preprocess_edlp_chutier_ui(&$vars){
// 'entries_size' => count($entrees), // 'entries_size' => count($entrees),
'class' => $classes, 'class' => $classes,
'title'=>$title, 'title'=>$title,
) ),
) ),
),
'remove_link' => array(
'#title' => t("Chutier."),
'#type' => 'link',
'#url' => $remove_url,
'#options'=>array(
'attributes' => array(
'data-drupal-link-system-path' => $remove_url->getInternalPath()
),
),
),
); );
} }
@@ -56,7 +56,7 @@ class StudioUIController extends ControllerBase {
* Return renderable array. * Return renderable array.
*/ */
public function StudioUIJson() { public function StudioUIJson() {
$renderable = $this->buildUI(); $renderable = $this->buildStudioUI();
$rendered = render($renderable); $rendered = render($renderable);
// JSON // JSON
@@ -68,11 +68,27 @@ class StudioUIController extends ControllerBase {
} }
public function buildUI(){ public function StudioChutierUIJson(){
// get current user $renderable = $this->buildChutierUI();
// done by DependencyInjection of AccountInterface $rendered = render($renderable);
// dpm($this->user->id());
// JSON
$response = new JsonResponse();
$response->setData([
'rendered'=> $rendered,
]);
return $response;
}
private function buildStudioUI(){
return [
'#theme' => 'edlp_studio_ui',
'#chutier_ui' => $this->buildChutierUI(),
'#composition_ui' => $this->buildCompostionUI(),
];
}
private function buildChutierUI(){
// get his docs in chutier // get his docs in chutier
$documents_nids = Chutier::getUserChutiersContents($this->user->id()); $documents_nids = Chutier::getUserChutiersContents($this->user->id());
// dpm($documents_nids); // dpm($documents_nids);
@@ -84,8 +100,13 @@ class StudioUIController extends ControllerBase {
"#theme"=>'edlp_chutier_ui', "#theme"=>'edlp_chutier_ui',
'#title' => t('Chutier'), '#title' => t('Chutier'),
"#document_nodes" => $documents, "#document_nodes" => $documents,
'#uid' => $this->user->id(),
); );
return $chutier_ui;
}
private function buildCompostionUI(){
// TODO: get compositions // TODO: get compositions
// TODO: add compositions theme, preprocess, twig template to studio module // TODO: add compositions theme, preprocess, twig template to studio module
// TODO: build content renderable array // TODO: build content renderable array
@@ -95,13 +116,6 @@ class StudioUIController extends ControllerBase {
"#compositions" => array('#markup'=>'TODO : compositions UI'), "#compositions" => array('#markup'=>'TODO : compositions UI'),
); );
// TODO: add studio theme, preprocess, twig template to studio module return $composition_ui;
// TODO: build global studio renderable array
return [
'#theme' => 'edlp_studio_ui',
'#chutier_ui' => $chutier_ui,
'#composition_ui' => $composition_ui,
];
} }
} }
@@ -102,5 +102,4 @@ interface ChutierInterface extends ContentEntityInterface, EntityChangedInterfac
*/ */
public static function getActionsURL($id, $uid); public static function getActionsURL($id, $uid);
} }
@@ -46,9 +46,17 @@ edlp_vars = {
} }
initScrollbars(); initScrollbars();
initEvents();
}; };
function initEvents(){
$('body').on('on-studio-chutier-updated', function(e){
initAjaxLinks();
});
}
// _ _ _ // _ _ _
// /_\ _ _ __| (_)___ // /_\ _ _ __| (_)___
// / _ \ || / _` | / _ \ // / _ \ || / _` | / _ \
@@ -368,6 +376,8 @@ edlp_vars = {
initAjaxLinks(); initAjaxLinks();
_$body.trigger({'type':'new-content-ajax-loaded'});
}; };
// ___ // ___
@@ -1503,12 +1503,22 @@ body.ajax-loading main[role="main"]:before {
height: 100%; } height: 100%; }
#studio-ui .chutier_ui { #studio-ui .chutier_ui {
height: 50%; height: 50%;
border-bottom: 1px solid red; } border-bottom: 1px solid red;
position: relative; }
#studio-ui .chutier_ui > h2 { #studio-ui .chutier_ui > h2 {
margin: 0.5em 0; z-index: 10;
position: absolute;
width: calc(100% - 20px);
margin: 0;
padding: 0.5em 0;
font-size: 1em; font-size: 1em;
font-weight: 500; font-weight: 500;
text-transform: uppercase; } text-transform: uppercase;
background-color: white; }
#studio-ui .chutier_ui .documents {
padding-top: 35px;
height: calc(100% - 35px);
overflow-y: auto; }
#studio-ui .chutier_ui .documents ul, #studio-ui .chutier_ui .documents li { #studio-ui .chutier_ui .documents ul, #studio-ui .chutier_ui .documents li {
margin: 0; margin: 0;
padding: 0; padding: 0;
@@ -1517,10 +1527,10 @@ body.ajax-loading main[role="main"]:before {
display: inline-block; display: inline-block;
vertical-align: top; vertical-align: top;
width: 49%; width: 49%;
margin-bottom: 0.5em; } margin-bottom: 0.5em;
white-space: nowrap; }
#studio-ui .chutier_ui .documents li .entrees { #studio-ui .chutier_ui .documents li .entrees {
line-height: 0; line-height: 0; }
display: inline-block; }
#studio-ui .chutier_ui .documents li .entrees span { #studio-ui .chutier_ui .documents li .entrees span {
display: inline-block; display: inline-block;
width: 8px; width: 8px;
@@ -1565,9 +1575,16 @@ body.ajax-loading main[role="main"]:before {
background-color: #d49cb6; } background-color: #d49cb6; }
#studio-ui .chutier_ui .documents li .entrees span[tid='123'] { #studio-ui .chutier_ui .documents li .entrees span[tid='123'] {
background-color: #497715; } background-color: #497715; }
#studio-ui .chutier_ui .documents li a { #studio-ui .chutier_ui .documents li a.audio-link {
text-transform: capitalize; text-transform: capitalize;
font-size: 1em; } font-size: 0.756em;
margin-right: 0.5em;
white-space: normal; }
#studio-ui .chutier_ui .documents li .chutier-icon {
display: inline-block;
width: 0.7em;
height: 0.7em;
opacity: 0.8; }
#studio-ui .composition_ui { #studio-ui .composition_ui {
height: 50%; } height: 50%; }
@@ -23,9 +23,17 @@
} }
initScrollbars(); initScrollbars();
initEvents();
}; };
function initEvents(){
$('body').on('on-studio-chutier-updated', function(e){
initAjaxLinks();
});
}
// _ _ _ // _ _ _
// /_\ _ _ __| (_)___ // /_\ _ _ __| (_)___
// / _ \ || / _` | / _ \ // / _ \ || / _` | / _ \
@@ -345,6 +353,8 @@
initAjaxLinks(); initAjaxLinks();
_$body.trigger({'type':'new-content-ajax-loaded'});
}; };
// ___ // ___
@@ -434,13 +434,25 @@ main[role="main"]{
.chutier_ui{ .chutier_ui{
height: 50%; height: 50%;
border-bottom: 1px solid red; border-bottom: 1px solid red;
position: relative;
&>h2{ &>h2{
margin:0.5em 0; z-index: 10;
position: absolute;
width: calc(100% - 20px);
margin: 0;
padding:0.5em 0;
font-size: 1em; font-size: 1em;
font-weight: 500; font-weight: 500;
text-transform: uppercase; text-transform: uppercase;
// outline: 1px solid orange;
background-color: white;
} }
.documents{ .documents{
padding-top: 35px;
height:calc(100% - 35px);
overflow-y: auto;
// box-sizing: content-box;
// outline: 1px solid green;
ul,li{ ul,li{
margin:0; padding:0; margin:0; padding:0;
line-height: 1.2; line-height: 1.2;
@@ -448,16 +460,23 @@ main[role="main"]{
li{ li{
display: inline-block; vertical-align: top; display: inline-block; vertical-align: top;
width:49%; margin-bottom: 0.5em; width:49%; margin-bottom: 0.5em;
white-space: nowrap;
.entrees{ .entrees{
line-height: 0; line-height: 0;
display:inline-block;
span{ span{
@include entrie-micro-square; @include entrie-micro-square;
} }
} }
a{ a.audio-link{
text-transform: capitalize; text-transform: capitalize;
font-size: 1em; font-size: 0.756em;
margin-right: 0.5em;
white-space: normal;
}
.chutier-icon{
display: inline-block;
width:0.7em; height:0.7em;
opacity: 0.8;
} }
} }
@@ -1,5 +1,5 @@
<div class="row"> <div class="row">
<div class="col small-col-12 med-col-6 large-col-6"> <div class="col small-col-12 med-col-12 large-col-6">
<div class="wrapper studio-ui-wrapper"> <div class="wrapper studio-ui-wrapper">
<div id="studio-ui"> <div id="studio-ui">
{{ chutier_ui }} {{ chutier_ui }}