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