xref: /plugin/sortablejs/syntax.php (revision d61cb45e7c129c86a4433f9a5bb46997ebf889f1)
1<?php
2/**
3 * Sortablejs: Javascript for Sortable table
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Otto Vainio
7 * version 1.1 Fixed javascript error in sorttable js
8 * version 2.0 Added <div> to sort any table
9 * version 2.1 Changed script to allow multiple sortable tables in one page
10 * version 2.2 A table can now be sorted by one column by default.
11 * version 2.2a css+js compress broke this script. Now fixed some jslint complains.
12 * version 2.3 Added support for odt plugin. (Aurélien Bompard)
13 * version 2.3a Fixed default sort with aligned text (Andre Rauschenbach)
14 * version 2.4 Added options to set manual override options for column sort. (nosort, numeric, alpha, ddmm, mmdd)
15 * version 2.5 Fixed problems with secionediting, footnotes and edittable
16 * version 2.6 Added support for jQuery and dokuwiki Weatherwax ->
17 * version 2.7 Fixed problem with first row not getting sorted
18 * version 2.8 Fixed problem with first row not getting sorted in default sort. Added option "sumrow" to prevent sum line sort.
19 * version 2.9 fixed problem with header row being sorted in earlier versions of dokuwiki.
20 */
21// must be run within Dokuwiki
22if (!defined('DOKU_INC')) die();
23if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
24require_once(DOKU_PLUGIN.'syntax.php');
25
26class syntax_plugin_sortablejs extends DokuWiki_Syntax_Plugin {
27
28
29  function getType() { return 'container';}
30  function getPType(){ return 'block';}
31  function getSort() { return 371; }
32  function getAllowedTypes() {return array('container','formatting','substition');}
33	function connectTo($mode) {
34    $this->Lexer->addEntryPattern('<sortable[^>]*>(?=.*?</sortable>)',$mode,'plugin_sortablejs');
35//    $this->Lexer->addEntryPattern('\x3Csortable.*?\x3E',$mode,'plugin_sortablejs');
36//    $this->Lexer->addEntryPattern('<sortable>',$mode,'plugin_sortablejs');
37  }
38  function postConnect() {
39    $this->Lexer->addExitPattern('</sortable>','plugin_sortablejs');
40  }
41  function handle($match, $state, $pos, &$handler){
42
43    switch ($state) {
44      case DOKU_LEXER_ENTER :
45        $match = substr($match,9,-1);
46        $match=trim($match);
47        $scl="";
48        if (strlen($match)>0) {
49          $scl=$this->__validateOptions($match);
50        }
51        return array($state, $scl);
52        break;
53      case DOKU_LEXER_UNMATCHED :
54//        return p_render('xhtml',p_get_instructions($match),$info);
55        return array($state, $match);
56        break;
57      case DOKU_LEXER_EXIT :
58//        return "</div>";
59        return array($state, "");
60        break;
61    }
62    return array();
63  }
64
65  function render($mode, &$renderer, $data) {
66    list($state,$match) = $data;
67    if ($mode == 'xhtml'){
68      switch ($state) {
69        case DOKU_LEXER_ENTER :
70          $renderer->doc .= "<div class=\"sortable$match\">";
71          break;
72        case DOKU_LEXER_UNMATCHED :
73//          $dbgr = p_render('xhtml',p_get_instructions($match),$info);
74//          $renderer->doc .= p_render('xhtml',p_get_instructions($match),$info);
75//          $renderer->doc .= $match;
76//          $instructions = array_slice(p_get_instructions($match), 1, -1);
77          $instructions = p_get_instructions($match);
78          foreach ($instructions as $instruction) {
79            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
80          }
81
82          break;
83        case DOKU_LEXER_EXIT :
84          $renderer->doc .=  "</div>";
85          break;
86      }
87      return true;
88    } else if($mode == 'odt'){
89      switch ($state) {
90        case DOKU_LEXER_ENTER :
91          // In ODT, tables must not be inside a paragraph. Make sure we
92          // closed any opened paragraph
93          $renderer->p_close();
94          break;
95        case DOKU_LEXER_UNMATCHED :
96          $instructions = array_slice(p_get_instructions($match), 1, -1);
97          foreach ($instructions as $instruction) {
98            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
99          }
100          break;
101        case DOKU_LEXER_EXIT :
102          //$renderer->p_open();
103          // DO NOT re-open the paragraph, it would cause an error if the table is the last content on a page
104          break;
105      }
106      return true;
107    }
108    return false;
109  }
110
111  function __validateOptions($opts) {
112    $oa = split(" ", $opts);
113    $ret = "";
114    foreach($oa as $opt) {
115      list($c,$v) = split("=",$opt);
116      if ($c=="sumrow") {
117        $c=$v;
118        $v="sumrow";
119        if ($c=="") {
120          $c="1";
121        }
122      } else if ($c=="3phase") {
123        $v=$c;
124        $c="";
125      }
126      if ($v!=null) {
127        $cmpr=$v;
128      } else {
129        if (preg_match('/r?\d*/', $c, $matches)) {
130          $cmpr='sort';
131        }
132      }
133      switch ($cmpr) {
134        case '3phase':
135            $ret .= " threephase";
136            break;
137        case 'nosort':
138            $ret .= " col_" . $c . "_nosort";
139            break;
140        case 'numeric':
141            $ret .= " col_" . $c . "_numeric";
142            break;
143        case 'ddmm':
144            $ret .= " col_" . $c . "_ddmm";
145            break;
146        case 'mmdd':
147            $ret .= " col_" . $c . "_mmdd";
148            break;
149        case 'alpha':
150        case 'text':
151            $ret .= " col_" . $c . "_alpha";
152            break;
153        case 'sort':
154            $ret .= ' sort' . $opt;
155            break;
156        case 'sumrow':
157            $ret .= ' sortbottom_' . $c;
158            //$ret = ' sortbottom' . $ret;
159            break;
160      }
161    }
162    return $ret;
163  }
164
165}
166?>
167