-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJavascriptSlideshow.hooks.php
130 lines (118 loc) · 4.96 KB
/
JavascriptSlideshow.hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Javascript Slideshow
* Javascript Slideshow Hooks
*
* @author @See $wgExtensionCredits
* @license GPL
* @package Javacsript Slideshow
* @link https://gitlab.com/hydrawiki/extensions/javascriptslideshow
**/
class JavascriptSlideshowHooks {
/**
* Sets up this extensions parser functions.
*
* @access public
* @param object Parser object passed as a reference.
* @return boolean true
*/
public static function wfSlideshowExtension(Parser &$parser) {
$output = RequestContext::getMain()->getOutput();
$output->addModules('ext.slideshow.main');
$parser->setHook('slideshow', 'JavascriptSlideshowHooks::renderSlideshowTag');
$parser->setFunctionHook('slideshow', 'JavascriptSlideshowHooks::renderSlideshowParserFunction');
return true;
}
/**
* Explodes a string that contains a space delimited array of associative key value pairs.
*
* @access private
* @param string String to explode arguments from.
* @return array Constructed array of associative key value pairs.
*/
private static function explodeArguments($string) {
$pairDelimiter = ' ';
$kvDelimiter = '=';
$_pieces = explode($pairDelimiter, $string);
if (count($_pieces)) {
foreach ($_pieces as $value) {
// We only want the information if it is a valid key value pair.
if (strpos($value, $kvDelimiter)) {
list($key, $value) = explode($kvDelimiter, $value);
$arguments[trim($key)] = trim($value);
}
}
}
return $arguments;
}
/**
* Initiates some needed classes.
*
* @access public
* @param object Parser object passed as a reference.
* @param string First argument passed to function tag, HTML input of <div> tags.
* @param string Second argument passed to function tag, delimited list of options.
* @return string HTML output of self::renderSlideshow()
*/
public static function renderSlideshowParserFunction(&$parser, $input = '', $options = '') {
return self::renderSlideshow($input, self::explodeArguments($options));
}
/**
* The callback function for converting the input text to HTML output.
*
* @access public
* @return void
*/
public static function renderSlideshowTag($input, $argv, $parser, $frame) {
$wikitext = self::renderSlideshow($input, $argv);
return $parser->recursiveTagParse($wikitext, $frame);
}
/**
* Renders the slideshow information into output for the calling tag or function.
*
* @access public
* @param string Combined raw HTML and wiki text.
* @param array Options that have been parsed by self::explodeArguments()
* @return string Rendered output
*/
private static function renderSlideshow($wikitext, $options = []) {
$validSequences = ['forward', 'backward', 'random', 'shuffle'];
$validTransitions = ['cut', 'fade', 'blindDown'];
$id = (isset($options['id']) ? $options['id'] : 'slideshow_' . rand());
// set default if not set
$refresh = (isset($options['refresh']) ? $options['refresh'] : '1000');
$sequence = (isset($options['sequence']) ? $options['sequence'] : 'forward');
$transition = (isset($options['transition']) ? $options['transition'] : 'cut');
$transitiontime = (isset($options['transitiontime']) ? $options['transitiontime'] : '400');
$center = (isset($options['center']) ? $options['center'] : 'false');
// validate input
if (!in_array($sequence, $validSequences)) {
return '<span class="error">JavascriptSlideshow: ' . wfMessage('javascriptslideshow-invalid-parameter', 'sequence', $sequence, implode(', ', $validSequences))->inContentLanguage() . '</span>';
} elseif (!in_array($transition, $validTransitions)) {
return '<span class="error">JavascriptSlideshow: ' . wfMessage('javascriptslideshow-invalid-parameter', 'transition', $transition, implode(', ', $validTransitions))->inContentLanguage() . '</span>';
} elseif (!is_numeric($refresh)) {
return '<span class="error">JavascriptSlideshow: ' . wfMessage('javascriptslideshow-invalid-num-parameter', 'refresh')->inContentLanguage() . '</span>';
} elseif (!is_numeric($transitiontime)) {
return '<span class="error">JavascriptSlideshow: ' . wfMessage('javascriptslideshow-invalid-num-parameter', 'transitiontime')->inContentLanguage() . '</span>';
} else {
$output = '';
$dataAttrs = "data-transition='$transition' data-refresh='$refresh' data-sequence='$sequence' data-transitiontime='$transitiontime'";
$styleAttrs = ($center == 'true' ? "style='margin-left:auto;margin-right:auto'" : "");
$output .= "<div id='$id' class='slideshow' $dataAttrs $styleAttrs >$wikitext</div> ";
$output .= "<div id='$id-spacer' class='slideshowspacer'></div>";
return $output;
}
return '<span class="error">JavascriptSlideshow: ' . wfMessage('javascriptslideshow-error-unknown')->inContentLanguage() . '</span>';
}
/**
* Add a CSS module with addModuleStyles to ensure it's loaded
* even if there is no Javascript support
*
* @return [type] [description]
*/
public static function extensionHook() {
global $wgOut;
$wgOut->addModuleStyles('ext.slideshow.css');
return true;
}
}