PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/html/behavior.php

https://github.com/joebushi/joomla
PHP | 481 lines | 361 code | 55 blank | 65 comment | 46 complexity | 281783d7482771a6cd9898fb9a5240ca MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage HTML
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. /**
  10. * Utility class for javascript behaviors
  11. *
  12. * @static
  13. * @package Joomla.Framework
  14. * @subpackage HTML
  15. * @version 1.5
  16. */
  17. abstract class JHtmlBehavior
  18. {
  19. /**
  20. * Method to load the mootools framework into the document head
  21. *
  22. * - If debugging mode is on an uncompressed version of mootools is included for easier debugging.
  23. *
  24. * @static
  25. * @param string $type Mootools file to load
  26. * @param boolean $debug Is debugging mode on? [optional]
  27. * @return void
  28. * @since 1.6
  29. */
  30. public static function framework($extras = false, $debug = null)
  31. {
  32. static $loaded = array();
  33. $type = $extras ? 'more' : 'core';
  34. // Only load once
  35. if (!empty($loaded[$type])) {
  36. return;
  37. }
  38. JHtml::core($debug);
  39. // If no debugging value is set, use the configuration setting
  40. if ($debug === null)
  41. {
  42. $config = &JFactory::getConfig();
  43. $debug = $config->getValue('config.debug');
  44. }
  45. // TODO NOTE: Here we are checking for Konqueror - If they fix thier issue with compressed, we will need to update this
  46. $konkcheck = isset($_SERVER['HTTP_USER_AGENT']) ? strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'konqueror') : null;
  47. $uncompressed = ($debug || $konkcheck) ? '-uncompressed' : '';
  48. if ($type != 'core' && empty($loaded['core'])) {
  49. self::framework(false);
  50. }
  51. JHtml::script('mootools-'.$type.$uncompressed.'.js', 'media/system/js/', false);
  52. $loaded[$type] = true;
  53. return;
  54. }
  55. /**
  56. * Deprecated. Use JHtmlBehavior::framework() instead.
  57. *
  58. * @static
  59. * @param boolean $debug Is debugging mode on? [optional]
  60. * @return void
  61. * @since 1.5
  62. */
  63. public static function mootools($debug = null)
  64. {
  65. self::framework(true, $debug);
  66. }
  67. public static function caption()
  68. {
  69. JHtml::script('caption.js');
  70. }
  71. public static function formvalidation()
  72. {
  73. JHtml::script('validate.js');
  74. }
  75. public static function switcher()
  76. {
  77. JHtml::_('behavior.framework');
  78. JHtml::script('switcher.js' );
  79. $script = "
  80. document.switcher = null;
  81. window.addEvent('domready', function(){
  82. toggler = document.id('submenu')
  83. element = document.id('config-document')
  84. if(element) {
  85. document.switcher = new JSwitcher(toggler, element, {cookieName: toggler.getAttribute('class')});
  86. }
  87. });";
  88. JFactory::getDocument()->addScriptDeclaration($script);
  89. }
  90. public static function combobox()
  91. {
  92. JHtml::script('combobox.js');
  93. }
  94. public static function tooltip($selector='.hasTip', $params = array())
  95. {
  96. static $tips;
  97. if (!isset($tips)) {
  98. $tips = array();
  99. }
  100. // Include mootools framework
  101. JHtml::_('behavior.framework', true);
  102. $sig = md5(serialize(array($selector,$params)));
  103. if (isset($tips[$sig]) && ($tips[$sig])) {
  104. return;
  105. }
  106. // Setup options object
  107. $opt['maxTitleChars'] = (isset($params['maxTitleChars']) && ($params['maxTitleChars'])) ? (int)$params['maxTitleChars'] : 50 ;
  108. // offsets needs an array in the format: array('x'=>20, 'y'=>30)
  109. $opt['offsets'] = (isset($params['offsets']) && (is_array($params['offsets']))) ? $params['offsets'] : null;
  110. $opt['showDelay'] = (isset($params['showDelay'])) ? (int)$params['showDelay'] : null;
  111. $opt['hideDelay'] = (isset($params['hideDelay'])) ? (int)$params['hideDelay'] : null;
  112. $opt['className'] = (isset($params['className'])) ? $params['className'] : null;
  113. $opt['fixed'] = (isset($params['fixed']) && ($params['fixed'])) ? '\\true' : '\\false';
  114. $opt['onShow'] = (isset($params['onShow'])) ? '\\'.$params['onShow'] : null;
  115. $opt['onHide'] = (isset($params['onHide'])) ? '\\'.$params['onHide'] : null;
  116. $options = JHTMLBehavior::_getJSObject($opt);
  117. // Attach tooltips to document
  118. $document = &JFactory::getDocument();
  119. $document->addScriptDeclaration("
  120. window.addEvent('domready', function() {
  121. $$('$selector').each(function(el) {
  122. var title = el.get('title');
  123. if (title) {
  124. var parts = title.split('::', 2);
  125. el.store('tip:title', parts[0]);
  126. el.store('tip:text', parts[1]);
  127. }
  128. });
  129. var JTooltips = new Tips($$('$selector'), $options);
  130. });");
  131. // Set static array
  132. $tips[$sig] = true;
  133. return;
  134. }
  135. public static function modal($selector='a.modal', $params = array())
  136. {
  137. static $modals;
  138. static $included;
  139. $document = &JFactory::getDocument();
  140. // Load the necessary files if they haven't yet been loaded
  141. if (!isset($included)) {
  142. // Load the javascript and css
  143. JHtml::_('behavior.framework');
  144. JHtml::script('modal.js');
  145. JHtml::stylesheet('modal.css');
  146. $included = true;
  147. }
  148. if (!isset($modals)) {
  149. $modals = array();
  150. }
  151. $sig = md5(serialize(array($selector,$params)));
  152. if (isset($modals[$sig]) && ($modals[$sig])) {
  153. return;
  154. }
  155. // Setup options object
  156. $opt['ajaxOptions'] = (isset($params['ajaxOptions']) && (is_array($params['ajaxOptions']))) ? $params['ajaxOptions'] : null;
  157. $opt['size'] = (isset($params['size']) && (is_array($params['size']))) ? $params['size'] : null;
  158. $opt['shadow'] = (isset($params['shadow'])) ? $params['shadow'] : null;
  159. $opt['onOpen'] = (isset($params['onOpen'])) ? $params['onOpen'] : null;
  160. $opt['onClose'] = (isset($params['onClose'])) ? $params['onClose'] : null;
  161. $opt['onUpdate'] = (isset($params['onUpdate'])) ? $params['onUpdate'] : null;
  162. $opt['onResize'] = (isset($params['onResize'])) ? $params['onResize'] : null;
  163. $opt['onMove'] = (isset($params['onMove'])) ? $params['onMove'] : null;
  164. $opt['onShow'] = (isset($params['onShow'])) ? $params['onShow'] : null;
  165. $opt['onHide'] = (isset($params['onHide'])) ? $params['onHide'] : null;
  166. $options = JHtmlBehavior::_getJSObject($opt);
  167. // Attach modal behavior to document
  168. $document->addScriptDeclaration("
  169. window.addEvent('domready', function() {
  170. SqueezeBox.initialize(".$options.");
  171. SqueezeBox.assign($$('".$selector."'), {
  172. parse: 'rel'
  173. });
  174. });");
  175. // Set static array
  176. $modals[$sig] = true;
  177. return;
  178. }
  179. public static function uploader($id='file-upload', $params = array(), $upload_queue='upload-queue')
  180. {
  181. JHtml::script('swf.js');
  182. JHtml::script('progressbar.js');
  183. JHtml::script('uploader.js');
  184. static $uploaders;
  185. if (!isset($uploaders)) {
  186. $uploaders = array();
  187. }
  188. if (isset($uploaders[$id]) && ($uploaders[$id])) {
  189. return;
  190. }
  191. // Setup options object
  192. $opt['url'] = (isset($params['targetURL'])) ? $params['targetURL'] : null ;
  193. $opt['path'] = (isset($params['swf'])) ? $params['swf'] : JURI::root(true).'/media/system/swf/uploader.swf';
  194. $opt['height'] = (isset($params['height'])) && $params['height'] ? (int)$params['height'] : null;
  195. $opt['width'] = (isset($params['width'])) && $params['width'] ? (int)$params['width'] : null;
  196. $opt['multiple'] = (isset($params['multiple']) && !($params['multiple'])) ? '\\false' : '\\true';
  197. $opt['queued'] = (isset($params['queued']) && !($params['queued'])) ? (int)$params['queued'] : null;
  198. $opt['target'] = (isset($params['target'])) ? $params['target'] : '\\$(\'upload-browse\')';
  199. $opt['instantStart'] = (isset($params['instantStart']) && ($params['instantStart'])) ? '\\true' : '\\false';
  200. $opt['allowDuplicates'] = (isset($params['allowDuplicates']) && !($params['allowDuplicates'])) ? '\\false' : '\\true';
  201. // limitSize is the old parameter name. Remove in 1.7
  202. $opt['fileSizeMax'] = (isset($params['limitSize']) && ($params['limitSize'])) ? (int)$params['limitSize'] : null;
  203. // fileSizeMax is the new name. If supplied, it will override the old value specified for limitSize
  204. $opt['fileSizeMax'] = (isset($params['fileSizeMax']) && ($params['fileSizeMax'])) ? (int)$params['fileSizeMax'] : $opt['fileSizeMax'];
  205. $opt['fileSizeMin'] = (isset($params['fileSizeMin']) && ($params['fileSizeMin'])) ? (int)$params['fileSizeMin'] : null;
  206. // limitFiles is the old parameter name. Remove in 1.7
  207. $opt['fileListMax'] = (isset($params['limitFiles']) && ($params['limitFiles'])) ? (int)$params['limitFiles'] : null;
  208. // fileListMax is the new name. If supplied, it will override the old value specified for limitFiles
  209. $opt['fileListMax'] = (isset($params['fileListMax']) && ($params['fileListMax'])) ? (int)$params['fileListMax'] : $opt['fileListMax'];
  210. $opt['fileListSizeMax'] = (isset($params['fileListSizeMax']) && ($params['fileListSizeMax'])) ? (int)$params['fileListSizeMax'] : null;
  211. // types is the old parameter name. Remove in 1.7
  212. $opt['typeFilter'] = (isset($params['types'])) ?'\\'.$params['types'] : '\\{\'All Files (*.*)\': \'*.*\'}';
  213. $opt['typeFilter'] = (isset($params['typeFilter'])) ?'\\'.$params['typeFilter'] : $opt['typeFilter'];
  214. // Optional functions
  215. $opt['createReplacement'] = (isset($params['createReplacement'])) ? '\\'.$params['createReplacement'] : null;
  216. $opt['onFileComplete'] = (isset($params['onFileComplete'])) ? '\\'.$params['onFileComplete'] : null;
  217. $opt['onComplete'] = (isset($params['onComplete'])) ? '\\'.$params['onComplete'] : null;
  218. $opt['onFileSuccess'] = (isset($params['onFileSuccess'])) ? '\\'.$params['onFileSuccess'] : null;
  219. if(!isset($params['startButton'])) $params['startButton'] = 'upload-start';
  220. if(!isset($params['clearButton'])) $params['clearButton'] = 'upload-clear';
  221. $opt['onLoad'] =
  222. '\\function() {
  223. document.id(\''.$id.'\').removeClass(\'hide\'); // we show the actual UI
  224. document.id(\'upload-noflash\').destroy(); // ... and hide the plain form
  225. // We relay the interactions with the overlayed flash to the link
  226. this.target.addEvents({
  227. click: function() {
  228. return false;
  229. },
  230. mouseenter: function() {
  231. this.addClass(\'hover\');
  232. },
  233. mouseleave: function() {
  234. this.removeClass(\'hover\');
  235. this.blur();
  236. },
  237. mousedown: function() {
  238. this.focus();
  239. }
  240. });
  241. // Interactions for the 2 other buttons
  242. document.id(\''.$params['clearButton'].'\').addEvent(\'click\', function() {
  243. Uploader.remove(); // remove all files
  244. return false;
  245. });
  246. document.id(\''.$params['startButton'].'\').addEvent(\'click\', function() {
  247. Uploader.start(); // start upload
  248. return false;
  249. });
  250. }';
  251. $options = JHtmlBehavior::_getJSObject($opt);
  252. // Attach tooltips to document
  253. $document = &JFactory::getDocument();
  254. $uploaderInit =
  255. 'window.addEvent(\'domready\', function(){
  256. var Uploader = new FancyUpload2($(\''.$id.'\'), $(\''.$upload_queue.'\'), '.$options.' );
  257. });';
  258. $document->addScriptDeclaration($uploaderInit);
  259. // Set static array
  260. $uploaders[$id] = true;
  261. return;
  262. }
  263. public static function tree($id, $params = array(), $root = array())
  264. {
  265. static $trees;
  266. if (!isset($trees)) {
  267. $trees = array();
  268. }
  269. // Include mootools framework
  270. JHtml::_('behavior.framework');
  271. JHtml::script('mootree.js');
  272. JHtml::stylesheet('mootree.css');
  273. if (isset($trees[$id]) && ($trees[$id])) {
  274. return;
  275. }
  276. // Setup options object
  277. $opt['div'] = (array_key_exists('div', $params)) ? $params['div'] : $id.'_tree';
  278. $opt['mode'] = (array_key_exists('mode', $params)) ? $params['mode'] : 'folders';
  279. $opt['grid'] = (array_key_exists('grid', $params)) ? '\\'.$params['grid'] : '\\true';
  280. $opt['theme'] = (array_key_exists('theme', $params)) ? $params['theme'] : JURI::root(true).'/media/system/images/mootree.gif';
  281. // Event handlers
  282. $opt['onExpand'] = (array_key_exists('onExpand', $params)) ? '\\'.$params['onExpand'] : null;
  283. $opt['onSelect'] = (array_key_exists('onSelect', $params)) ? '\\'.$params['onSelect'] : null;
  284. $opt['onClick'] = (array_key_exists('onClick', $params)) ? '\\'.$params['onClick'] : '\\function(node){ window.open(node.data.url, $chk(node.data.target) ? node.data.target : \'_self\'); }';
  285. $options = JHtmlBehavior::_getJSObject($opt);
  286. // Setup root node
  287. $rt['text'] = (array_key_exists('text', $root)) ? $root['text'] : 'Root';
  288. $rt['id'] = (array_key_exists('id', $root)) ? $root['id'] : null;
  289. $rt['color'] = (array_key_exists('color', $root)) ? $root['color'] : null;
  290. $rt['open'] = (array_key_exists('open', $root)) ? '\\'.$root['open'] : '\\true';
  291. $rt['icon'] = (array_key_exists('icon', $root)) ? $root['icon'] : null;
  292. $rt['openicon'] = (array_key_exists('openicon', $root)) ? $root['openicon'] : null;
  293. $rt['data'] = (array_key_exists('data', $root)) ? $root['data'] : null;
  294. $rootNode = JHtmlBehavior::_getJSObject($rt);
  295. $treeName = (array_key_exists('treeName', $params)) ? $params['treeName'] : '';
  296. $js = ' window.addEvent(\'domready\', function(){
  297. tree'.$treeName.' = new MooTreeControl('.$options.','.$rootNode.');
  298. tree'.$treeName.'.adopt(\''.$id.'\');})';
  299. // Attach tooltips to document
  300. $document = &JFactory::getDocument();
  301. $document->addScriptDeclaration($js);
  302. // Set static array
  303. $trees[$id] = true;
  304. return;
  305. }
  306. public static function calendar()
  307. {
  308. $document = &JFactory::getDocument();
  309. JHtml::stylesheet('calendar-jos.css', 'media/system/css/', array(' title' => JText::_('green') ,' media' => 'all'));
  310. JHtml::script('calendar.js', 'media/system/js/');
  311. JHtml::script('calendar-setup.js', 'media/system/js/');
  312. $translation = JHtmlBehavior::_calendartranslation();
  313. if ($translation) {
  314. $document->addScriptDeclaration($translation);
  315. }
  316. }
  317. /**
  318. * Keep session alive, for example, while editing or creating an article.
  319. */
  320. public static function keepalive()
  321. {
  322. // Include mootools framework
  323. JHtmlBehavior::mootools();
  324. $config = &JFactory::getConfig();
  325. $lifetime = ($config->getValue('lifetime') * 60000);
  326. $refreshTime = ($lifetime <= 60000) ? 30000 : $lifetime - 60000;
  327. //refresh time is 1 minute less than the liftime assined in the configuration.php file
  328. $document = &JFactory::getDocument();
  329. $script = '';
  330. $script .= 'function keepAlive() {';
  331. $script .= ' var myAjax = new Ajax("index.php", { method: "get" }).request();';
  332. $script .= '}';
  333. $script .= ' window.addEvent("domready", function()';
  334. $script .= '{ keepAlive.periodical('.$refreshTime.'); }';
  335. $script .= ');';
  336. $document->addScriptDeclaration($script);
  337. return;
  338. }
  339. /**
  340. * Internal method to get a JavaScript object notation string from an array
  341. *
  342. * @param array $array The array to convert to JavaScript object notation
  343. * @return string JavaScript object notation representation of the array
  344. * @since 1.5
  345. */
  346. protected static function _getJSObject($array=array())
  347. {
  348. // Initialise variables.
  349. $object = '{';
  350. // Iterate over array to build objects
  351. foreach ((array)$array as $k => $v)
  352. {
  353. if (is_null($v)) {
  354. continue;
  355. }
  356. if (!is_array($v) && !is_object($v))
  357. {
  358. $object .= ' '.$k.': ';
  359. $object .= (is_numeric($v) || strpos($v, '\\') === 0) ? (is_numeric($v)) ? $v : substr($v, 1) : "'".$v."'";
  360. $object .= ',';
  361. }
  362. else {
  363. $object .= ' '.$k.': '.JHtmlBehavior::_getJSObject($v).',';
  364. }
  365. }
  366. if (substr($object, -1) == ',') {
  367. $object = substr($object, 0, -1);
  368. }
  369. $object .= '}';
  370. return $object;
  371. }
  372. /**
  373. * Internal method to translate the JavaScript Calendar
  374. *
  375. * @return string JavaScript that translates the object
  376. * @since 1.5
  377. */
  378. protected static function _calendartranslation()
  379. {
  380. static $jsscript = 0;
  381. if ($jsscript == 0)
  382. {
  383. $return = 'Calendar._DN = new Array ("'.JText::_('Sunday').'", "'.JText::_('Monday').'", "'.JText::_('Tuesday').'", "'.JText::_('Wednesday').'", "'.JText::_('Thursday').'", "'.JText::_('Friday').'", "'.JText::_('Saturday').'", "'.JText::_('Sunday').'");Calendar._SDN = new Array ("'.JText::_('Sun').'", "'.JText::_('Mon').'", "'.JText::_('Tue').'", "'.JText::_('Wed').'", "'.JText::_('Thu').'", "'.JText::_('Fri').'", "'.JText::_('Sat').'", "'.JText::_('Sun').'"); Calendar._FD = 0; Calendar._MN = new Array ("'.JText::_('January').'", "'.JText::_('February').'", "'.JText::_('March').'", "'.JText::_('April').'", "'.JText::_('May').'", "'.JText::_('June').'", "'.JText::_('July').'", "'.JText::_('August').'", "'.JText::_('September').'", "'.JText::_('October').'", "'.JText::_('November').'", "'.JText::_('December').'"); Calendar._SMN = new Array ("'.JText::_('January_short').'", "'.JText::_('February_short').'", "'.JText::_('March_short').'", "'.JText::_('April_short').'", "'.JText::_('May_short').'", "'.JText::_('June_short').'", "'.JText::_('July_short').'", "'.JText::_('August_short').'", "'.JText::_('September_short').'", "'.JText::_('October_short').'", "'.JText::_('November_short').'", "'.JText::_('December_short').'");Calendar._TT = {};Calendar._TT["INFO"] = "'.JText::_('ABOUT_THE_CALENDAR').'";
  384. Calendar._TT["ABOUT"] =
  385. "DHTML Date/Time Selector\n" +
  386. "(c) dynarch.com 2002-2005 / Author: Mihai Bazon\n" +
  387. "For latest version visit: http://www.dynarch.com/projects/calendar/\n" +
  388. "Distributed under GNU LGPL. See http://gnu.org/licenses/lgpl.html for details." +
  389. "\n\n" +
  390. "Date selection:\n" +
  391. "- Use the \xab, \xbb buttons to select year\n" +
  392. "- Use the " + String.fromCharCode(0x2039) + ", " + String.fromCharCode(0x203a) + " buttons to select month\n" +
  393. "- Hold mouse button on any of the above buttons for faster selection.";
  394. Calendar._TT["ABOUT_TIME"] = "\n\n" +
  395. "Time selection:\n" +
  396. "- Click on any of the time parts to increase it\n" +
  397. "- or Shift-click to decrease it\n" +
  398. "- or click and drag for faster selection.";
  399. Calendar._TT["PREV_YEAR"] = "'.JText::_('PREV__YEAR__HOLD_FOR_MENU_').'";Calendar._TT["PREV_MONTH"] = "'.JText::_('PREV__MONTH__HOLD_FOR_MENU').'"; Calendar._TT["GO_TODAY"] = "'.JText::_('GO_TODAY').'";Calendar._TT["NEXT_MONTH"] = "'.JText::_('NEXT_MONTH__HOLD_FOR_MENU').'";Calendar._TT["NEXT_YEAR"] = "'.JText::_('NEXT_YEAR__HOLD_FOR_MENU').'";Calendar._TT["SEL_DATE"] = "'.JText::_('SELECT_DATE').'";Calendar._TT["DRAG_TO_MOVE"] = "'.JText::_('DRAG_TO_MOVE').'";Calendar._TT["PART_TODAY"] = "'.JText::_('TODAY').'";Calendar._TT["DAY_FIRST"] = "'.JText::_('DISPLAY__S_FIRST').'";Calendar._TT["WEEKEND"] = "0,6";Calendar._TT["CLOSE"] = "'.JText::_('Close').'";Calendar._TT["TODAY"] = "'.JText::_('Today').'";Calendar._TT["TIME_PART"] = "'.JText::_('SHIFT__CLICK_OR_DRAG_TO_CHANGE_VALUE').'";Calendar._TT["DEF_DATE_FORMAT"] = "'.JText::_('%Y-%m-%d').'"; Calendar._TT["TT_DATE_FORMAT"] = "'.JText::_('%a, %b %e').'";Calendar._TT["WK"] = "'.JText::_('wk').'";Calendar._TT["TIME"] = "'.JText::_('TIME').':";';
  400. $jsscript = 1;
  401. return $return;
  402. }
  403. else {
  404. return false;
  405. }
  406. }
  407. }