xref: /plugin/sortablejs/syntax.php (revision 9d7cf522d7fd0dbc29b7ffcd0ef9444c5aa2e61a)
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 */
20// must be run within Dokuwiki
21if (!defined('DOKU_INC')) die();
22if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
23require_once(DOKU_PLUGIN.'syntax.php');
24
25class syntax_plugin_sortablejs extends DokuWiki_Syntax_Plugin {
26
27
28  function getType() { return 'container';}
29  function getPType(){ return 'block';}
30  function getSort() { return 371; }
31  function getAllowedTypes() {return array('container','formatting','substition');}
32	function connectTo($mode) {
33    $this->Lexer->addEntryPattern('<sortable[^>]*>(?=.*?</sortable>)',$mode,'plugin_sortablejs');
34//    $this->Lexer->addEntryPattern('\x3Csortable.*?\x3E',$mode,'plugin_sortablejs');
35//    $this->Lexer->addEntryPattern('<sortable>',$mode,'plugin_sortablejs');
36  }
37  function postConnect() {
38    $this->Lexer->addExitPattern('</sortable>','plugin_sortablejs');
39  }
40  function handle($match, $state, $pos, &$handler){
41
42    switch ($state) {
43      case DOKU_LEXER_ENTER :
44        $match = substr($match,9,-1);
45        $match=trim($match);
46        $scl="";
47        if (strlen($match)>0) {
48          $scl=$this->__validateOptions($match);
49        }
50        return array($state, $scl);
51        break;
52      case DOKU_LEXER_UNMATCHED :
53//        return p_render('xhtml',p_get_instructions($match),$info);
54        return array($state, $match);
55        break;
56      case DOKU_LEXER_EXIT :
57//        return "</div>";
58        return array($state, "");
59        break;
60    }
61    return array();
62  }
63
64  function render($mode, &$renderer, $data) {
65    list($state,$match) = $data;
66    if ($mode == 'xhtml'){
67      switch ($state) {
68        case DOKU_LEXER_ENTER :
69          $renderer->doc .= "<div class=\"sortable$match\">";
70          break;
71        case DOKU_LEXER_UNMATCHED :
72//          $dbgr = p_render('xhtml',p_get_instructions($match),$info);
73//          $renderer->doc .= p_render('xhtml',p_get_instructions($match),$info);
74//          $renderer->doc .= $match;
75//          $instructions = array_slice(p_get_instructions($match), 1, -1);
76          $instructions = p_get_instructions($match);
77          foreach ($instructions as $instruction) {
78            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
79          }
80
81          break;
82        case DOKU_LEXER_EXIT :
83          $renderer->doc .=  "</div>";
84          break;
85      }
86      return true;
87    } else if($mode == 'odt'){
88      switch ($state) {
89        case DOKU_LEXER_ENTER :
90          // In ODT, tables must not be inside a paragraph. Make sure we
91          // closed any opened paragraph
92          $renderer->p_close();
93          break;
94        case DOKU_LEXER_UNMATCHED :
95          $instructions = array_slice(p_get_instructions($match), 1, -1);
96          foreach ($instructions as $instruction) {
97            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
98          }
99          break;
100        case DOKU_LEXER_EXIT :
101          $renderer->p_open(); // re-open the paragraph
102          break;
103      }
104      return true;
105    }
106    return false;
107  }
108
109  function __validateOptions($opts) {
110    $oa = split(" ", $opts);
111    $ret = "";
112    foreach($oa as $opt) {
113      list($c,$v) = split("=",$opt);
114      if ($c=="sumrow") {
115        $v=$c;
116        $c="";
117      }
118      if ($v!=null) {
119        $cmpr=$v;
120      } else {
121        if (preg_match('/r?\d*/', $c, $matches)) {
122          $cmpr='sort';
123        }
124      }
125      switch ($cmpr) {
126        case 'nosort':
127            $ret .= " col_" . $c . "_nosort";
128            break;
129        case 'numeric':
130            $ret .= " col_" . $c . "_numeric";
131            break;
132        case 'ddmm':
133            $ret .= " col_" . $c . "_ddmm";
134            break;
135        case 'mmdd':
136            $ret .= " col_" . $c . "_mmdd";
137            break;
138        case 'alpha':
139        case 'text':
140            $ret .= " col_" . $c . "_alpha";
141            break;
142        case 'sort':
143            $ret = ' sort' . $opt . $ret;
144            break;
145        case 'sumrow':
146            $ret = ' sortbottom' . $c . $ret;
147            break;
148      }
149    }
150    return $ret;
151  }
152
153}
154?>
155