xref: /plugin/sortablejs/syntax.php (revision 12db27a468764f96a8959c2c6bd29045d3ddb615)
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(); // re-open the paragraph
103          break;
104      }
105      return true;
106    }
107    return false;
108  }
109
110  function __validateOptions($opts) {
111    $oa = split(" ", $opts);
112    $ret = "";
113    foreach($oa as $opt) {
114      list($c,$v) = split("=",$opt);
115      if ($c=="sumrow") {
116        $c=$v;
117        $v="sumrow";
118        if ($c=="") {
119          $c="1";
120        }
121      } else if ($c=="3phase") {
122        $v=$c;
123        $c="";
124      }
125      if ($v!=null) {
126        $cmpr=$v;
127      } else {
128        if (preg_match('/r?\d*/', $c, $matches)) {
129          $cmpr='sort';
130        }
131      }
132      switch ($cmpr) {
133        case '3phase':
134            $ret .= " threephase";
135            break;
136        case 'nosort':
137            $ret .= " col_" . $c . "_nosort";
138            break;
139        case 'numeric':
140            $ret .= " col_" . $c . "_numeric";
141            break;
142        case 'ddmm':
143            $ret .= " col_" . $c . "_ddmm";
144            break;
145        case 'mmdd':
146            $ret .= " col_" . $c . "_mmdd";
147            break;
148        case 'alpha':
149        case 'text':
150            $ret .= " col_" . $c . "_alpha";
151            break;
152        case 'sort':
153            $ret .= ' sort' . $opt;
154            break;
155        case 'sumrow':
156            $ret .= ' sortbottom_' . $c;
157            //$ret = ' sortbottom' . $ret;
158            break;
159      }
160    }
161    return $ret;
162  }
163
164}
165?>
166