]> git.0d.be Git - chloro.git/blobdiff - chloro/phyll/static/js/chloro.js
do not include non-feed posts on homepage
[chloro.git] / chloro / phyll / static / js / chloro.js
index 816d0223b08951a7f1c307f5ff71442c0260f309..ecc6c97d031f59b2f6bcc777aa9352d9415cba83 100644 (file)
@@ -1,7 +1,89 @@
+// from django/contrib/admin/static/admin/js/urlify.js
+var LATIN_MAP = {
+    'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE',
+    'Ç': 'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I',
+    'Î': 'I', 'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O',
+    'Õ': 'O', 'Ö': 'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U',
+    'Ü': 'U', 'Ű': 'U', 'Ý': 'Y', 'Þ': 'TH', 'Ÿ': 'Y', 'ß': 'ss', 'à': 'a',
+    'á': 'a', 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'æ': 'ae', 'ç': 'c',
+    'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e', 'ì': 'i', 'í': 'i', 'î': 'i',
+    'ï': 'i', 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o',
+    'ö': 'o', 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u',
+    'ű': 'u', 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
+};
+
+function downcode(string) {
+  return string.toLowerCase().replace(/[^A-Za-z0-9\[\] ]/g,function(a){ return LATIN_MAP[a]||a }).replace(/[^-\w\s]/g, '').replace(/^\s+|\s+$/g, '').replace(/[-\s]+/g, '-');
+};
+
+function remove_auto_anchors() {
+  $('article .wiki-anchor-auto').each(function(idx, anchor) {
+          $(anchor).parent().removeAttr('id');
+          $(anchor).remove();
+  });
+}
+
+function auto_anchors() {
+  $('article h2, article h3, article h4').each(function(idx, elem) {
+    var $elem = $(elem);
+    if ($elem.attr('id')) return;
+    if ($elem.find('.wiki-anchor').length) return;
+    $elem.attr('id', downcode($elem.text()));
+    $('<a class="wiki-anchor wiki-anchor-auto" href="#' + $elem.attr('id') + '">¶</a>').appendTo($elem);
+  });
+}
+
+function create_toc() {
+  $('#toc').remove();
+  if ($('article h2').length == 0) return;
+  $div_toc = $('<div id="toc"><ul></ul></div>');
+  $div_toc_ul = $div_toc.find('ul');
+  var li_titles = Array();
+  $('article h2').each(function(idx, elem) {
+    var $elem = $(elem);
+    var slug = elem.id;
+    var $a_title = $('<a></a>', {href: '#' + slug, text: $elem.text().replace(/¶$/, '')});
+    var $li_title = $('<li></li>');
+    $li_title[0].related_position =  $(elem).position().top;
+    li_titles.push($li_title[0]);
+    $a_title.appendTo($li_title);
+    $li_title.appendTo($div_toc_ul);
+  });
+  $('article h1').first().after($div_toc);
+
+  li_titles = li_titles.reverse();
+
+  $(window).on('load', function() {
+    // update positions after images have been loaded
+    $('article h2').each(function(idx, elem) {
+      $('#toc li')[idx].related_position =  $(elem).position().top;
+    });
+    $(window).trigger('scroll');
+  });
+
+  var scroll_timeout_id = null;
+  $(window).on('scroll', function() {
+    if (scroll_timeout_id) clearTimeout(scroll_timeout_id);
+    scroll_timeout_id = setTimeout(function() {  // throttle
+      scroll_timeout_id = null;
+      var current_position = window.scrollY;
+      $('#toc li').removeClass('active');
+      for (const li_title of li_titles) {
+        if (li_title.related_position < current_position - 25) {
+          $(li_title).addClass('active');
+          break;
+        }
+      }
+    }, 50);
+  });
+};
+
 (function(window, document, undefined) {
   var Phylly = {
     BLOCKS: [
+          {name: 'intertitle', tag: 'H2', klass: 'intertitle'},
           {name: 'code', tag: 'PRE', klass: 'screen'},
+          {name: 'list', special: 'list', tag: 'UL', klass: 'list'},
           {name: 'figure', special: 'img', tag: 'DIV', subtag: true, klass: 'figure'},
           {name: 'note', tag: 'DIV', subtag: true, klass: 'note'},
           {name: 'quote', tag: 'BLOCKQUOTE', subtag: true, klass: 'quote'},
         } else if (inline_style_toolbar) {
           $(inline_style_toolbar).hide();
         }
+        if ($(sel.anchorNode).is('div.figure') && $(sel.anchorNode).find('img').length) {
+          show_figure_toolbar(sel);
+        } else if ($(sel.anchorNode).parents('.figure-toolbar').length == 0) {
+          $(figure_toolbar).hide();
+        }
       });
       var $image_upload = $('<input type="file" nam="image" id="image-upload">');
       $image_upload.on('change', upload_image);
       var file = $(this).prop('files')[0];
       var params = new FormData();
       params.append('upload', file);
-      $.post({url: '/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
+      $.post({url: '/ajax/upload/', processData: false, data: params, contentType: false}).done(function(data) {
         var img = document.createElement('IMG');
         img.src = data.url;
         if (data.orig_url) {
       var file = $(this).prop('files')[0];
       var params = new FormData();
       params.append('upload', file);
-      $.post({url: '/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
+      $.post({url: '/ajax/upload/', processData: false, data: params, contentType: false}).done(function(data) {
         var doc_link = document.createElement('A');
         doc_link.className = 'button';
         doc_link.textContent = 'Télécharger ' + data.filename;
       var params = {};
       params.title = text;
       params.request_id = request_id;
-      $.post('/wiki/ajax/newpage/', params).success(function(data) {
+      $.post('/ajax/newpage/', params).done(function(data) {
         $('a[data-request-id=' + data.request_id + ']').attr('href', data.url).removeAttr('data-request-id');
       });
       param = $new_link[0].outerHTML;
     }
     if (action == 'createLink') {
       var sel = window.getSelection();
+      var selected_link = get_parent(sel.anchorNode, 'A');
+      if (sel.anchorNode.nodeType == Node.TEXT_NODE) {
+        if (sel.anchorNode.length == sel.anchorOffset && sel.anchorNode.nextSibling.nodeName == 'A') {
+          selected_link = sel.anchorNode.nextSibling;
+        }
+      }
       var $input = $('input[name=link-target]');
       $input[0]._range = sel.getRangeAt(0);
-      if (sel.anchorNode instanceof Element) {
-        var elem = sel.anchorNode.childNodes[sel.anchorOffset];
-        if (elem.tagName == 'A') {
-          $input.val(elem.href);
-        }
+      if (selected_link) {
+        $input[0]._selected_link = selected_link;
+        $input.val(selected_link.href);
       }
       $input.addClass('shown');
       $input.focus();
       $input.removeClass('shown');
       var sel = window.getSelection();
       sel.addRange(this._range);
+      var selected_link = $input[0]._selected_link;
       if (url) {
-        document.execCommand('createLink', false, url);
+        if (selected_link) {
+          selected_link.href = url;
+        } else {
+          var $new_link = $('<a></a>', {text: sel.toString(), href: url});
+          this._range.deleteContents();
+          this._range.insertNode($new_link[0]);
+          sel.empty();
+          sel.collapse($new_link[0]);
+          sel.empty();
+        }
       } else {
-        document.execCommand('unlink', false, null);
+        if (selected_link) {
+          selected_link.replaceWith(document.createTextNode(selected_link.textContent));
+        }
       }
-      sel.empty();
       $input.val('');
+      $input[0]._selected_link = null;
     }
   }
   function focusout_link(ev) {
                       '<button data-action="bold" data-accel="b"><b>b</b></button>' +
                       '<button data-action="code" data-accel="<">&lt;&gt;</button>' +
                       '<button data-action="removeFormat" data-accel="m">×</button>' +
+                      '<button data-action="wiki">W</button>' +
                       '<button data-action="createLink">a</button>' +
                       '<input name="link-target"/>' +
                       '</div>');
     inline_style_toolbar.css('left', pos.left + window.scrollX);
     inline_style_toolbar.show();
   };
+
+  var figure_toolbar = null;
+  function show_figure_toolbar(sel) {
+    if (figure_toolbar === null) {
+      figure_toolbar = $('<div class="figure-toolbar"><label>Alt: <input name="figure-alt"></label></div>')
+      figure_toolbar.hide();
+      figure_toolbar.insertAfter(document.body);
+      $('[name="figure-alt"]').on('change', function() {
+       $(this.img).attr('alt', $(this).val());
+      });
+    }
+    figure_toolbar.css('position', 'absolute');
+    var pos = sel.getRangeAt(0).getClientRects()[0];
+    figure_toolbar.css('top', pos.bottom + window.scrollY);
+    figure_toolbar.css('left', pos.left + window.scrollX);
+    figure_toolbar.show();
+    $('[name="figure-alt"]')[0].img = $(sel.anchorNode).find('img');
+    $('[name="figure-alt"]').val($('[name="figure-alt"]')[0].img.attr('alt') || '');
+  };
+
 }(window, document));
 
 $(function() {
-  Phylly.init(),
-  $('div[contenteditable]').each(function(i, elem) {Phylly.bind_events(elem)});
-  $('#save').on('click', function() {
-    var text = $('div[contenteditable]')[0].innerHTML;
-    var csrf = $('[name=csrfmiddlewaretoken]').val();
-    $.post('api-save/',
-      { text: text, csrfmiddlewaretoken: csrf}
-    ).fail(function() {
-      $('#save').css('background', 'red');
+  $('#quickedit input').on('change', function() {
+    var enable = $(this).is(':checked');
+    if (enable) {
+      remove_auto_anchors();
+      $('div[data-editable]').each(function(i, elem) {
+        $(elem).attr('contenteditable', 'true');
+        var $button = $('<button class="save">Enregistrer</button>');
+        $button[0].div_zone = elem;
+        $button.insertBefore($('#quickedit label'));
+      });
+      Phylly.init(),
+      $('div[data-editable]').each(function(i, elem) {
+        Phylly.bind_events(elem);
+      });
+      $('.save').on('click', function() {
+        var text = $('div[contenteditable]')[0].innerHTML;
+        var csrf = $('[name=csrfmiddlewaretoken]').val();
+        $.post('api-save/',
+          { text: text, csrfmiddlewaretoken: csrf}
+        ).fail(function() {
+          $('.save').addClass('error');
+        }).done(function() {
+          $('.save').removeClass('error');
+        });
+        return false;
+      });
+    } else {
+      auto_anchors();
+      if ($('main.phyll-toc').length) create_toc();
+      Phylly.off(),
+      $('button.save').remove();
+      $('div[data-editable]').each(function(i, elem) {
+        $(elem).attr('contenteditable', 'false');
+        Phylly.unbind_events(elem);
+      });
+    }
+  });
+  $('#quickedit input').trigger('change');
+  if ($('#quickedit input').length == 0) {
+    create_toc();
+  }
+  $('.search-results').empty();
+  $('#search-enable').on('change', function() {
+    if ($(this).is(':checked')) {
+      $('.search-field input').focus();
+    }
+  });
+  $('.search-field').on('submit', function() {
+    var value = $('input[name=q]').val();
+    $.ajax({url: '/ajax/search/', data: {q: value}}).done(function(data) {
+      $('.search-results').empty();
+      for (const hit of data.data) {
+        var $a_hit = $('<a>', {text: hit.title, href: hit.url});
+        var $li_hit = $('<li>');
+        $li_hit.append($a_hit);
+        $('.search-results').append($li_hit);
+      }
     });
     return false;
   });