Bläddra i källkod

refactored articles, added articles index

Bachir Soussi Chiadmi 5 år sedan
förälder
incheckning
a6fb21f696

+ 2 - 1
sites/all/modules/figli/edlp_ajax/edlp_ajax.module

@@ -55,7 +55,8 @@ function edlp_ajax_page_attachments(array &$attachments) {
     || preg_match('/^\/?agenda/', $current_path)
     || preg_match('/^\/?studio/', $current_path)
     || preg_match('/^\/?search/', $current_path)
-    || preg_match('/^\/?lastdocs/', $current_path)){
+    || preg_match('/^\/?lastdocs/', $current_path)
+    || preg_match('/^\/?articles/', $current_path)){
       $redirect = true;
     }
 

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
sites/all/modules/figli/edlp_corpus/assets/dist/scripts/corpus.min.js


+ 102 - 70
sites/all/modules/figli/edlp_corpus/assets/scripts/corpus.js

@@ -46,7 +46,7 @@
     var _nodes = [];
     var _articles_nodes = [];
     var _no_articles_nodes = [];
-    var _articles_filter_on = false;
+    // var _articles_filter_on = false;
     var _nodes_by_entries = {};
     var _nodes_by_nid = {};
     var _nodes_Nid_Id = {};
@@ -807,7 +807,7 @@
       }
     };
     function filterEntree(t){
-      shutDownArticles();
+      // shutDownArticles();
 
       _nodes_centered = [];
       for (var n = 0; n < _nodes.length; n++) {
@@ -850,7 +850,7 @@
     // |___/\___\__,_|_| \__|_||_|
     // and ajax modal content filtering
     function filterByNids(nids){
-      shutDownArticles();
+      // shutDownArticles();
 
       _nodes_centered = [];
       for (var n = 0; n < _nodes.length; n++) {
@@ -903,11 +903,14 @@
     // /_/ \_\_|  \__|_\__|_\___/__/
     function initArtilesLink(){
      // add "articles link to blockentrees"
+     var href = window.location.origin + drupalSettings.path.baseUrl +_settings.articlesindex_url;
      _$articles_link = $('<a>')
        .html('Articles')
-       .attr("href", "#articles")
-       .addClass('articles-link')
-       .on('click', onCLickedOnArticles);
+       .attr("href", href)
+       .attr("data-drupal-link-system-path", _settings.articlesindex_url)
+       .addClass('articles-link ajax-link');
+       // main.js from template will load the index then trigger the filter here
+       // .on('click', onCLickedOnArticles);
 
        $('.item-list ul',_$entrees_block).append(
          $('<li>').append(
@@ -915,69 +918,98 @@
          )
        );
     };
-    function onCLickedOnArticles(e){
-      e.preventDefault();
-      $(this).toggleClass('is-active');
-      if($(this).is('.is-active')){
-        activateArticlesFilter();
-        closeAllEntries();
-      }else{
-        deactivateArticlesFilter();
-      }
-      // _$canvas.trigger({
-      //   'type':'corpus-cliqued-on-articles'
-      // });
-      return false;
-    };
-    function activateArticlesFilter(){
-      //console.log('activateArticlesFilter');
-      _articles_filter_on = true;
-      // OPTION 1 : just fade no article nodes
-      // for (var i = 0; i < _no_articles_nodes.length; i++) {
-      //   _no_articles_nodes[i].fade();
-      // }
-
-      // OPTION 2 : center the articles nodes and aside others
-      closeAllEntries();
-      _nodes_centered = [];
-      for (var i = 0; i < _articles_nodes.length; i++) {
-        _articles_nodes[i].setCentered();
-        // record centered nodes for inter node repulsions
-        _nodes_centered.push(_articles_nodes[i]);
-      }
-      for (var i = 0; i < _no_articles_nodes.length; i++) {
-        _no_articles_nodes[i].setAside();
-      }
-
-      updateRandomPlaylist(_articles_nodes);
-      if(typeof _paq !== 'undefined'){
-        // trackEvent(category, action, [name], [value])
-        _paq.push(['trackEvent', 'Corpus Articles', 'on']);
-      }
-    };
-    function deactivateArticlesFilter(){
-      //console.log('deactivateArticlesFilter');
-      _articles_filter_on = false;
-
-      // OPTION 1 : just fade no article nodes
-      // for (var i = 0; i < _no_articles_nodes.length; i++) {
-      //   _no_articles_nodes[i].unFade();
-      // }
-
-      // OPTION 2 : centered / aside
-      scrambleCollection();
-
-      updateRandomPlaylist(_playlist);
-      if(typeof _paq !== 'undefined'){
-        // trackEvent(category, action, [name], [value])
-        _paq.push(['trackEvent', 'Corpus Articles', 'off']);
-      }
-    };
-    function shutDownArticles(){
+    // function onCLickedOnArticles(e){
+    //   e.preventDefault();
+    //   $(this).toggleClass('is-active');
+    //   if($(this).is('.is-active')){
+    //     activateArticlesFilter();
+    //     closeAllEntries();
+    //   }else{
+    //     deactivateArticlesFilter();
+    //   }
+    //   // _$canvas.trigger({
+    //   //   'type':'corpus-cliqued-on-articles'
+    //   // });
+    //   return false;
+    // };
+    // function activateArticlesFilter(){
+    //   //console.log('activateArticlesFilter');
+    //   _articles_filter_on = true;
+    //   // OPTION 1 : just fade no article nodes
+    //   // for (var i = 0; i < _no_articles_nodes.length; i++) {
+    //   //   _no_articles_nodes[i].fade();
+    //   // }
+    //   // END OPTION 1
+    //
+    //   // OPTION 2 : center the articles nodes and aside others
+    //   closeAllEntries();
+    //   _nodes_centered = [];
+    //   for (var i = 0; i < _articles_nodes.length; i++) {
+    //     _articles_nodes[i].setCentered();
+    //     // record centered nodes for inter node repulsions
+    //     _nodes_centered.push(_articles_nodes[i]);
+    //   }
+    //   for (var i = 0; i < _no_articles_nodes.length; i++) {
+    //     _no_articles_nodes[i].setAside();
+    //   }
+    //   // TODO: open an index of nodes with articles
+    //   // loadArticlesIndex();
+    //   // END OPTION 2
+    //
+    //   updateRandomPlaylist(_articles_nodes);
+    //   if(typeof _paq !== 'undefined'){
+    //     // trackEvent(category, action, [name], [value])
+    //     _paq.push(['trackEvent', 'Corpus Articles', 'on']);
+    //   }
+    // };
+    // function loadArticlesIndex(){
+    //   var path = window.location.origin + drupalSettings.path.baseUrl +_settings.articlesindex_url+'/ajax';
+    //   $.getJSON(path)
+    //     .done(function(data){
+    //       onArticlesIndexLoaded(data);
+    //     })
+    //     .fail(function(jqxhr, textStatus, error){
+    //       onArticlesIndexLoadFail(jqxhr, textStatus, error);
+    //     });
+    // };
+    //
+    // function onArticlesIndexLoaded(data){
+    //   // insert index col
+    //   _$container.find('main[role="main"] .row').append(data.rendered);
+    //
+    //   // trigger event
+    //   _$body.trigger({
+    //     'type':'articles-index-loaded',
+    //     'results':data.results_nids
+    //   });
+    // };
+    //
+    // function onArticlesIndexLoadFail(jqxhr, textStatus, error){
+    //   console.warn('EdlpCorpus : articles index ajax load failed : '+error, jqxhr.responseText);
+    // };
+    // function deactivateArticlesFilter(){
+    //   //console.log('deactivateArticlesFilter');
+    //   _articles_filter_on = false;
+    //
+    //   // OPTION 1 : just fade no article nodes
+    //   // for (var i = 0; i < _no_articles_nodes.length; i++) {
+    //   //   _no_articles_nodes[i].unFade();
+    //   // }
+    //
+    //   // OPTION 2 : centered / aside
+    //   scrambleCollection();
+    //
+    //   updateRandomPlaylist(_playlist);
+    //   if(typeof _paq !== 'undefined'){
+    //     // trackEvent(category, action, [name], [value])
+    //     _paq.push(['trackEvent', 'Corpus Articles', 'off']);
+    //   }
+    // };
+    // function shutDownArticles(){
       // shutdown articles if active
-      if(_$articles_link.is('.is-active'))
-        _$articles_link.trigger('click');
-    };
+    //   if(_$articles_link.is('.is-active'))
+    //     _$articles_link.trigger('click');
+    // };
 
     //  ___             _
     // | __|_ _____ _ _| |_ ___
@@ -1009,7 +1041,7 @@
               var event = {
                 'type':'corpus-cliked-on-node',
                 'target_node':_nodes[_node_hover_id],
-                'article':_articles_filter_on
+                // 'article':_articles_filter_on
               };
               _$canvas.trigger(event);
               // instead of directly opening the doc, create an event listener (e.g. : audio played from random)
@@ -1017,7 +1049,7 @@
             }else{
               // console.log('corpus : click on map');
               _$canvas.trigger('corpus-cliked-on-map');
-              shutDownArticles();
+              // shutDownArticles();
             }
           }
         })

+ 12 - 0
sites/all/modules/figli/edlp_corpus/edlp_corpus.module

@@ -35,6 +35,13 @@ function edlp_corpus_theme($existing, $type, $theme, $path) {
         'lastdocs_nodes' => NULL,
       ),
     ),
+    'edlp_corpus_articlesindex' => array(
+      // 'render element' => '',
+      'file' => 'includes/edlp_corpus_articlesindex.inc',
+      'variables' => array(
+        'articles_nodes' => NULL,
+      ),
+    ),
   );
 }
 
@@ -230,6 +237,11 @@ function edlp_corpus_page_attachments(array &$attachments) {
   // load corpus
   $url = Url::fromRoute('edlp_corpus.corpusjson');
   $attachments['#attached']['drupalSettings']['edlp_corpus']['load_corpus_ajax_url'] = $url->getInternalPath();
+
+
+  // load corpus
+  $url = Url::fromRoute('edlp_corpus.articlesindex');
+  $attachments['#attached']['drupalSettings']['edlp_corpus']['articlesindex_url'] = $url->getInternalPath();
 }
 
 

+ 17 - 0
sites/all/modules/figli/edlp_corpus/edlp_corpus.routing.yml

@@ -30,3 +30,20 @@ edlp_corpus.lastdocsajax:
     _title: 'Last docs'
   requirements:
     _permission: 'access content'
+
+
+edlp_corpus.articlesindex:
+  path: '/articles'
+  defaults:
+    _controller: '\Drupal\edlp_corpus\Controller\CorpusController::articlesindex'
+    _title: 'Articles Index'
+  requirements:
+    _permission: 'access content'
+
+edlp_corpus.articlesindexajax:
+  path: '/articles/ajax'
+  defaults:
+    _controller: '\Drupal\edlp_corpus\Controller\CorpusController::articlesindexjson'
+    _title: 'Articles Index'
+  requirements:
+    _permission: 'access content'

+ 30 - 0
sites/all/modules/figli/edlp_corpus/includes/edlp_corpus_articlesindex.inc

@@ -0,0 +1,30 @@
+<?php
+
+// use Drupal\Core\Url;
+
+function template_preprocess_edlp_corpus_articlesindex(&$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');
+
+  if(count($vars['articles_nodes'])){
+    $articles_list = array (
+      '#theme' => 'item_list',
+      '#items' => [],
+    );
+    foreach($vars['articles_nodes'] as $node){
+      $articles_list['#items'][] = $view_builder->view($node, 'index');
+    }
+
+    $vars['articles'] = array(
+      "#type"=>"container",
+      "#attributes"=>array(
+        "class"=>['articles']
+      ),
+      "#markup"=>"<h3>".t("Articles")."</h3>",
+      "articles"=>$articles_list
+    );
+  }
+}

+ 106 - 0
sites/all/modules/figli/edlp_corpus/src/Controller/CorpusController.php

@@ -243,4 +243,110 @@ class CorpusController extends ControllerBase {
     return $response;
   }
 
+
+
+  //    _       _   _    _          ___         _
+  //   /_\  _ _| |_(_)__| |___ ___ |_ _|_ _  __| |_____ __
+  //  / _ \| '_|  _| / _| / -_|_-<  | || ' \/ _` / -_) \ /
+  // /_/ \_\_|  \__|_\__|_\___/__/ |___|_||_\__,_\___/_\_\
+
+
+    private function articlesQuery() {
+      $query = \Drupal::entityQuery('node')
+      ->condition('status', 1)
+      ->condition('type', 'enregistrement')
+      ->condition('body', '', "<>")
+      ->sort('created', 'DESC')
+      ->range(0,20);
+
+      $nids = $query->execute();
+      $nodes = entity_load_multiple('node', $nids);
+
+      $this->articles_nodes = [];
+      $this->articles_nids = [];
+      foreach ($nodes as $node) {
+        // remove masqué
+        $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
+        if($sid != 'corpus_documents_publie') continue;
+
+        $this->articles_nodes[] = $node;
+        // record an array of nids for corpus map filtering
+        $this->articles_nids[] = $node->get('nid')->getString();
+      }
+
+    }
+
+    private function articlesToRenderable(){
+       $this->articlesQuery();
+       // dpm($this->next_event_node);
+
+       return array(
+         "#theme"=>'edlp_corpus_articlesindex',
+         '#articles_nodes' => $this->articles_nodes
+       );
+
+     }
+    /**
+     * Display lastdocs as a page.
+     *
+     * @return renderable array
+     */
+    public function articlesindex() {
+      return $this->articlesToRenderable();
+    }
+
+    /**
+     * Get lastdocs data as json through ajax.
+     *
+     * @return json
+     */
+    public function articlesindexjson() {
+
+      $renderable = $this->articlesToRenderable();
+      // $rendered = render($renderable);
+      // We can't render directly the entity as it throw an exception with cachable data
+      //http://blog.dcycle.com/blog/2018-01-24/caching-drupal-8-rest-resource/#the-dreaded-leaked-metadata-error
+      $rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($renderable) {
+        return render($renderable);
+      });
+
+      $data = [
+        'rendered'=> $rendered,
+        'title'=>'Articles',
+        'articles' => $this->articles_nids,
+        'documents_lies' => $this->articles_nids,
+      ];
+
+      // translations links
+      // use Drupal\Core\Url;
+      // use Drupal\Core\Language\LanguageInterface;
+      $route_name = 'edlp_corpus.articlesindex';
+      $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
+      if (isset($links->links)) {
+        $translations_build = [
+          '#theme' => 'links__language_block',
+          '#links' => $links->links,
+          '#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
+          '#set_active_class' => TRUE,
+        ];
+        $translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
+
+        $data['translations_links'] = $translations_rendered;
+      }
+
+      $data['#cache'] = [
+        'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
+        'tags' => ['edlp-articlesindex-cache']
+      ];
+
+      // $response = new JsonResponse();
+      // $response->setData($data);
+      $response = new CacheableJsonResponse($data);
+      $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
+      $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
+
+      return $response;
+    }
+
+
 }

+ 1 - 0
sites/all/modules/figli/edlp_corpus/templates/edlp-corpus-articlesindex.html.twig

@@ -0,0 +1 @@
+{{ articles }}

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
sites/all/themes/custom/edlptheme/assets/dist/scripts/main.min.js


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
sites/all/themes/custom/edlptheme/assets/dist/styles/app.min.css


+ 24 - 6
sites/all/themes/custom/edlptheme/assets/scripts/main.js

@@ -301,7 +301,7 @@
 
       // handle clicked link classes
       $('.ajax-loading').removeClass('ajax-loading');
-      $('.ajax-link.is-active').removeClass('is-active');
+      $('.ajax-link.is-active:not(.articles-link)').removeClass('is-active');
       $('.is-active-trail').removeClass('is-active-trail');
 
       if(typeof state.selector != 'undefined'){
@@ -314,7 +314,10 @@
         if(typeof state.view_mode != 'undefined'){
           $('a[viewmode="'+state.view_mode+'"][data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
         }else{
-          $('a[data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
+          // wait for corpus map beeing ready (e.g. for articles link)
+          _corpus_promise.done(function(){
+            $('a[data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
+          });
         }
         // as new content is not related to entree, we trigger close entree
         _$body.trigger({'type':'new-content-not-entree-ajax-loaded'});
@@ -787,12 +790,15 @@
           //console.log('theme : corpus-cliked-on-node', e);
           _audioPlayer
             .emmit('stop-shuffle')
-            .setAutoOpenArticle(e.article)
+            // .setAutoOpenArticle(e.article)
             .openDocument(e.target_node);
         });
 
         _randomPlayer = new RandomPlayer(e.playlist);
 
+      // mainly for articles link
+      initAjaxLinks();
+
       _$body.attr('corpus-map', 'ready');
     }
     function openEntree(tid){
@@ -1193,6 +1199,7 @@
         // call drupal behaviours (for addtoany)
         Drupal.attachBehaviors(this.$cartel);
         // open automaticly tha article if needed
+        this.setAutoOpenArticle();
         if(this.auto_open_article){
           this.$cartel.find('a.link-article').trigger('click');
           this.auto_open_article = false;
@@ -1203,7 +1210,7 @@
         this.$cartel.removeClass('loading').html('');
       },
       setAutoOpenArticle(art){
-        this.auto_open_article = art;
+        this.auto_open_article = $('a.articles-link').is('.is-active');
         return this;
       },
       // global
@@ -1881,15 +1888,26 @@
     };
 
     function checkRowEmpty(){
+      console.log('checkRowEmpty');
       // if row is empty and we are not in productions or entree notice|index call closeAllModals()
       if(!$('.col', _$row).length){
         if(!_$body.is('.entity-type-taxonomy_term.bundle-entrees')){
           if(!_$body.is('.entity-type-node.bundle-page')){
             // we weren't on production or entree, so go back to front page
-            if(!$('a.articles-link').is('.is-active')){
-              // don't go back to front page if articles filter is on
+            // debugger;
+            if(_$body.is('.bundle-enregistrement.view-mode-article')
+              && $('a.articles-link').is('.is-active')){
+                console.log('Closing article while article index is active');
+                // if node article and articles is active, reload index
+                $('a.articles-link').removeClass('is-active').trigger('click');
+            }else{
               backToFrontPage();
             }
+            // if(!$('a.articles-link').is('.is-active')){
+            //   // don't go back to front page if articles filter is on
+              // backToFrontPage();
+            // }else{
+            // }
           }else{
             // if we were on production page just scramble collection in case of map was filtered
             _$corpus_canvas.trigger({'type':'scramble-collection'});

+ 19 - 0
sites/all/themes/custom/edlptheme/assets/styles/app.scss

@@ -459,6 +459,25 @@ main[role="main"]{
     }
   }
 
+  div.articles{
+    padding-top:1em!important;
+    &:not(.home_mobile)>h3{
+      display:none;
+      // @include content_subtitles;
+      // margin: 1em 0 0.5em;
+    }
+    &.home_mobile h2{
+      margin:0;
+      @include content_titles;
+    }
+    article.node--type-enregistrement{
+      h2.node-title{
+        @include content_subtitles;
+        margin:0.9em 0 0 0;
+      }
+    }
+  }
+
   div.lastdocs{
     article.node--type-enregistrement{
       h2.node-title{

+ 5 - 0
sites/all/themes/custom/edlptheme/templates/content/edlp-corpus-articlesindex.html.twig

@@ -0,0 +1,5 @@
+<div class="col small-col-12 med-col-6 large-col-6">
+  <div class="wrapper">
+      {{ articles }}
+  </div>
+</div>

Vissa filer visades inte eftersom för många filer har ändrats