Advertisement
Guest User

behaviour.php

a guest
Nov 30th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.62 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9.  
  10. defined('JPATH_PLATFORM') or die;
  11.  
  12. /**
  13. * Utility class for javascript behaviors
  14. *
  15. * @package Joomla.Platform
  16. * @subpackage HTML
  17. * @since 11.1
  18. */
  19. abstract class JHtmlBehavior
  20. {
  21. /**
  22. * @var array array containing information for loaded files
  23. */
  24. protected static $loaded = array();
  25.  
  26. /**
  27. * Method to load the MooTools framework into the document head
  28. *
  29. * If debugging mode is on an uncompressed version of MooTools is included for easier debugging.
  30. *
  31. * @param string $extras MooTools file to load
  32. * @param boolean $debug Is debugging mode on? [optional]
  33. *
  34. * @return void
  35. *
  36. * @since 11.1
  37. */
  38. public static function framework($extras = false, $debug = null)
  39. {
  40. $type = $extras ? 'more' : 'core';
  41.  
  42. // Only load once
  43. if (!empty(self::$loaded[__METHOD__][$type]))
  44. {
  45. return;
  46. }
  47.  
  48. // If no debugging value is set, use the configuration setting
  49. if ($debug === null)
  50. {
  51. $config = JFactory::getConfig();
  52. $debug = $config->get('debug');
  53. }
  54.  
  55. if ($type != 'core' && empty(self::$loaded[__METHOD__]['core']))
  56. {
  57. self::framework(false, $debug);
  58. }
  59.  
  60. JHtml::_('script', 'system/mootools-' . $type . '.js', false, true, false, false, $debug);
  61. JHtml::_('script', 'system/core.js', false, true);
  62. self::$loaded[__METHOD__][$type] = true;
  63.  
  64. return;
  65. }
  66.  
  67. /**
  68. * Deprecated. Use JHtmlBehavior::framework() instead.
  69. *
  70. * @param boolean $debug Is debugging mode on? [optional]
  71. *
  72. * @return void
  73. *
  74. * @since 11.1
  75. *
  76. * @deprecated 12.1
  77. */
  78. public static function mootools($debug = null)
  79. {
  80. // Deprecation warning.
  81. JLog::add('JBehavior::mootools is deprecated.', JLog::WARNING, 'deprecated');
  82.  
  83. self::framework(true, $debug);
  84. }
  85.  
  86. /**
  87. * Add unobtrusive javascript support for image captions.
  88. *
  89. * @param string $selector The selector for which a caption behaviour is to be applied.
  90. *
  91. * @return void
  92. *
  93. * @since 11.1
  94. */
  95. public static function caption($selector = 'img.caption')
  96. {
  97. // Only load once
  98. if (isset(self::$loaded[__METHOD__][$selector]))
  99. {
  100. return;
  101. }
  102.  
  103. // Include MooTools framework
  104. self::framework();
  105.  
  106. JHtml::_('script', 'system/caption.js', true, true);
  107.  
  108. // Attach caption to document
  109. JFactory::getDocument()->addScriptDeclaration(
  110. "window.addEvent('load', function() {
  111. new JCaption('" . $selector . "');
  112. });"
  113. );
  114.  
  115. // Set static array
  116. self::$loaded[__METHOD__][$selector] = true;
  117. }
  118.  
  119. /**
  120. * Add unobtrusive javascript support for form validation.
  121. *
  122. * To enable form validation the form tag must have class="form-validate".
  123. * Each field that needs to be validated needs to have class="validate".
  124. * Additional handlers can be added to the handler for username, password,
  125. * numeric and email. To use these add class="validate-email" and so on.
  126. *
  127. * @return void
  128. *
  129. * @since 11.1
  130. */
  131. public static function formvalidation()
  132. {
  133. // Only load once
  134. if (isset(self::$loaded[__METHOD__]))
  135. {
  136. return;
  137. }
  138.  
  139. // Include MooTools framework
  140. self::framework();
  141.  
  142. JHtml::_('script', 'system/validate.js', true, true);
  143. self::$loaded[__METHOD__] = true;
  144. }
  145.  
  146. /**
  147. * Add unobtrusive javascript support for submenu switcher support in
  148. * Global Configuration and System Information.
  149. *
  150. * @return void
  151. *
  152. * @since 11.1
  153. */
  154. public static function switcher()
  155. {
  156. // Only load once
  157. if (isset(self::$loaded[__METHOD__]))
  158. {
  159. return;
  160. }
  161.  
  162. // Include MooTools framework
  163. self::framework();
  164.  
  165. JHtml::_('script', 'system/switcher.js', true, true);
  166.  
  167. $script = "
  168. document.switcher = null;
  169. window.addEvent('domready', function(){
  170. toggler = document.id('submenu');
  171. element = document.id('config-document');
  172. if (element) {
  173. document.switcher = new JSwitcher(toggler, element, {cookieName: toggler.getProperty('class')});
  174. }
  175. });";
  176.  
  177. JFactory::getDocument()->addScriptDeclaration($script);
  178. self::$loaded[__METHOD__] = true;
  179. }
  180.  
  181. /**
  182. * Add unobtrusive javascript support for a combobox effect.
  183. *
  184. * Note that this control is only reliable in absolutely positioned elements.
  185. * Avoid using a combobox in a slider or dynamic pane.
  186. *
  187. * @return void
  188. *
  189. * @since 11.1
  190. */
  191. public static function combobox()
  192. {
  193. if (isset(self::$loaded[__METHOD__]))
  194. {
  195. return;
  196. }
  197. // Include MooTools framework
  198. self::framework();
  199.  
  200. JHtml::_('script', 'system/combobox.js', true, true);
  201. self::$loaded[__METHOD__] = true;
  202. }
  203.  
  204. /**
  205. * Add unobtrusive javascript support for a hover tooltips.
  206. *
  207. * Add a title attribute to any element in the form
  208. * title="title::text"
  209. *
  210. *
  211. * Uses the core Tips class in MooTools.
  212. *
  213. * @param string $selector The class selector for the tooltip.
  214. * @param array $params An array of options for the tooltip.
  215. * Options for the tooltip can be:
  216. * - maxTitleChars integer The maximum number of characters in the tooltip title (defaults to 50).
  217. * - offsets object The distance of your tooltip from the mouse (defaults to {'x': 16, 'y': 16}).
  218. * - showDelay integer The millisecond delay the show event is fired (defaults to 100).
  219. * - hideDelay integer The millisecond delay the hide hide is fired (defaults to 100).
  220. * - className string The className your tooltip container will get.
  221. * - fixed boolean If set to true, the toolTip will not follow the mouse.
  222. * - onShow function The default function for the show event, passes the tip element
  223. * and the currently hovered element.
  224. * - onHide function The default function for the hide event, passes the currently
  225. * hovered element.
  226. *
  227. * @return void
  228. *
  229. * @since 11.1
  230. */
  231. public static function tooltip($selector = '.hasTip', $params = array())
  232. {
  233. $sig = md5(serialize(array($selector, $params)));
  234. if (isset(self::$loaded[__METHOD__][$sig]))
  235. {
  236. return;
  237. }
  238.  
  239. // Include MooTools framework
  240. self::framework(true);
  241.  
  242. // Setup options object
  243. $opt['maxTitleChars'] = (isset($params['maxTitleChars']) && ($params['maxTitleChars'])) ? (int) $params['maxTitleChars'] : 50;
  244. // offsets needs an array in the format: array('x'=>20, 'y'=>30)
  245. $opt['offset'] = (isset($params['offset']) && (is_array($params['offset']))) ? $params['offset'] : null;
  246. if (!isset($opt['offset']))
  247. {
  248. // Supporting offsets parameter which was working in mootools 1.2 (Joomla!1.5)
  249. $opt['offset'] = (isset($params['offsets']) && (is_array($params['offsets']))) ? $params['offsets'] : null;
  250. }
  251. $opt['showDelay'] = (isset($params['showDelay'])) ? (int) $params['showDelay'] : null;
  252. $opt['hideDelay'] = (isset($params['hideDelay'])) ? (int) $params['hideDelay'] : null;
  253. $opt['className'] = (isset($params['className'])) ? $params['className'] : null;
  254. $opt['fixed'] = (isset($params['fixed']) && ($params['fixed'])) ? true : false;
  255. $opt['onShow'] = (isset($params['onShow'])) ? '\\' . $params['onShow'] : null;
  256. $opt['onHide'] = (isset($params['onHide'])) ? '\\' . $params['onHide'] : null;
  257.  
  258. $options = JHtmlBehavior::_getJSObject($opt);
  259.  
  260. // Attach tooltips to document
  261. JFactory::getDocument()->addScriptDeclaration(
  262. "window.addEvent('domready', function() {
  263. $$('$selector').each(function(el) {
  264. var title = el.get('title');
  265. if (title) {
  266. var parts = title.split('::', 2);
  267. el.store('tip:title', parts[0]);
  268. el.store('tip:text', parts[1]);
  269. }
  270. });
  271. var JTooltips = new Tips($$('$selector'), $options);
  272. });"
  273. );
  274.  
  275. // Set static array
  276. self::$loaded[__METHOD__][$sig] = true;
  277.  
  278. return;
  279. }
  280.  
  281. /**
  282. * Add unobtrusive javascript support for modal links.
  283. *
  284. * @param string $selector The selector for which a modal behaviour is to be applied.
  285. * @param array $params An array of parameters for the modal behaviour.
  286. * Options for the modal behaviour can be:
  287. * - ajaxOptions
  288. * - size
  289. * - shadow
  290. * - overlay
  291. * - onOpen
  292. * - onClose
  293. * - onUpdate
  294. * - onResize
  295. * - onShow
  296. * - onHide
  297. *
  298. * @return void
  299. *
  300. * @since 11.1
  301. */
  302. public static function modal($selector = 'a.modal', $params = array())
  303. {
  304. $document = JFactory::getDocument();
  305.  
  306. // Load the necessary files if they haven't yet been loaded
  307. if (!isset(self::$loaded[__METHOD__]))
  308. {
  309. // Include MooTools framework
  310. self::framework();
  311.  
  312. // Load the javascript and css
  313. JHtml::_('script', 'system/modal.js', true, true);
  314. JHtml::_('stylesheet', 'system/modal.css', array(), true);
  315. }
  316.  
  317. $sig = md5(serialize(array($selector, $params)));
  318. if (isset(self::$loaded[__METHOD__][$sig]))
  319. {
  320. return;
  321. }
  322.  
  323. // Setup options object
  324. $opt['ajaxOptions'] = (isset($params['ajaxOptions']) && (is_array($params['ajaxOptions']))) ? $params['ajaxOptions'] : null;
  325. $opt['handler'] = (isset($params['handler'])) ? $params['handler'] : null;
  326. $opt['fullScreen'] = (isset($params['fullScreen'])) ? (bool) $params['fullScreen'] : null;
  327. $opt['parseSecure'] = (isset($params['parseSecure'])) ? (bool) $params['parseSecure'] : null;
  328. $opt['closable'] = (isset($params['closable'])) ? (bool) $params['closable'] : null;
  329. $opt['closeBtn'] = (isset($params['closeBtn'])) ? (bool) $params['closeBtn'] : null;
  330. $opt['iframePreload'] = (isset($params['iframePreload'])) ? (bool) $params['iframePreload'] : null;
  331. $opt['iframeOptions'] = (isset($params['iframeOptions']) && (is_array($params['iframeOptions']))) ? $params['iframeOptions'] : null;
  332. $opt['size'] = (isset($params['size']) && (is_array($params['size']))) ? $params['size'] : null;
  333. $opt['shadow'] = (isset($params['shadow'])) ? $params['shadow'] : null;
  334. $opt['overlay'] = (isset($params['overlay'])) ? $params['overlay'] : null;
  335. $opt['onOpen'] = (isset($params['onOpen'])) ? $params['onOpen'] : null;
  336. $opt['onClose'] = (isset($params['onClose'])) ? $params['onClose'] : null;
  337. $opt['onUpdate'] = (isset($params['onUpdate'])) ? $params['onUpdate'] : null;
  338. $opt['onResize'] = (isset($params['onResize'])) ? $params['onResize'] : null;
  339. $opt['onMove'] = (isset($params['onMove'])) ? $params['onMove'] : null;
  340. $opt['onShow'] = (isset($params['onShow'])) ? $params['onShow'] : null;
  341. $opt['onHide'] = (isset($params['onHide'])) ? $params['onHide'] : null;
  342.  
  343. $options = JHtmlBehavior::_getJSObject($opt);
  344.  
  345. // Attach modal behavior to document
  346. $document
  347. ->addScriptDeclaration(
  348. "
  349. window.addEvent('domready', function() {
  350.  
  351. SqueezeBox.initialize(" . $options . ");
  352. SqueezeBox.assign($$('" . $selector . "'), {
  353. parse: 'rel'
  354. });
  355. });"
  356. );
  357.  
  358. // Set static array
  359. self::$loaded[__METHOD__][$sig] = true;
  360.  
  361. return;
  362. }
  363.  
  364. /**
  365. * JavaScript behavior to allow shift select in grids
  366. *
  367. * @param string $id The id of the form for which a multiselect behaviour is to be applied.
  368. *
  369. * @return void
  370. *
  371. * @since 11.1
  372. */
  373. public static function multiselect($id = 'adminForm')
  374. {
  375. // Only load once
  376. if (isset(self::$loaded[__METHOD__][$id]))
  377. {
  378. return;
  379. }
  380.  
  381. // Include MooTools framework
  382. self::framework();
  383.  
  384. JHtml::_('script', 'system/multiselect.js', true, true);
  385.  
  386. // Attach multiselect to document
  387. JFactory::getDocument()->addScriptDeclaration(
  388. "window.addEvent('domready', function() {
  389. new Joomla.JMultiSelect('" . $id . "');
  390. });"
  391. );
  392.  
  393. // Set static array
  394. self::$loaded[__METHOD__][$id] = true;
  395. return;
  396. }
  397.  
  398. /**
  399. * Add unobtrusive javascript support for the advanced uploader.
  400. *
  401. * @param string $id An index.
  402. * @param array $params An array of options for the uploader.
  403. * @param string $upload_queue The HTML id of the upload queue element (??).
  404. *
  405. * @return void
  406. *
  407. * @since 11.1
  408. */
  409. public static function uploader($id = 'file-upload', $params = array(), $upload_queue = 'upload-queue')
  410. {
  411. // Include MooTools framework
  412. self::framework();
  413.  
  414. JHtml::_('script', 'system/swf.js', true, true);
  415. JHtml::_('script', 'system/progressbar.js', true, true);
  416. JHtml::_('script', 'system/uploader.js', true, true);
  417.  
  418. $document = JFactory::getDocument();
  419.  
  420. if (!isset(self::$loaded[__METHOD__]))
  421. {
  422. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_FILENAME');
  423. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_UPLOAD_COMPLETED');
  424. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_ERROR_OCCURRED');
  425. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_ALL_FILES');
  426. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_PROGRESS_OVERALL');
  427. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_CURRENT_TITLE');
  428. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_REMOVE');
  429. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_REMOVE_TITLE');
  430. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_CURRENT_FILE');
  431. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_CURRENT_PROGRESS');
  432. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_FILE_ERROR');
  433. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_FILE_SUCCESSFULLY_UPLOADED');
  434. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_VALIDATION_ERROR_DUPLICATE');
  435. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_VALIDATION_ERROR_SIZELIMITMIN');
  436. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_VALIDATION_ERROR_SIZELIMITMAX');
  437. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_VALIDATION_ERROR_FILELISTMAX');
  438. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_VALIDATION_ERROR_FILELISTSIZEMAX');
  439. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_ERROR_HTTPSTATUS');
  440. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_ERROR_SECURITYERROR');
  441. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_ERROR_IOERROR');
  442. JText::script('JLIB_HTML_BEHAVIOR_UPLOADER_ALL_FILES');
  443. }
  444.  
  445. if (isset(self::$loaded[__METHOD__][$id]))
  446. {
  447. return;
  448. }
  449.  
  450. $onFileSuccess = '\\function(file, response) {
  451. var json = new Hash(JSON.decode(response, true) || {});
  452.  
  453. if (json.get(\'status\') == \'1\') {
  454. file.element.addClass(\'file-success\');
  455. file.info.set(\'html\', \'<strong>\' + Joomla.JText._(\'JLIB_HTML_BEHAVIOR_UPLOADER_FILE_SUCCESSFULLY_UPLOADED\') + \'</strong>\');
  456. } else {
  457. file.element.addClass(\'file-failed\');
  458. file.info.set(\'html\', \'<strong>\' +
  459. Joomla.JText._(\'JLIB_HTML_BEHAVIOR_UPLOADER_ERROR_OCCURRED\',
  460. \'An Error Occurred\').substitute({ error: json.get(\'error\') }) + \'</strong>\');
  461. }
  462. }';
  463.  
  464. // Setup options object
  465. $opt['verbose'] = true;
  466. $opt['url'] = (isset($params['targetURL'])) ? $params['targetURL'] : null;
  467. $opt['path'] = (isset($params['swf'])) ? $params['swf'] : JURI::root(true) . '/media/system/swf/uploader.swf';
  468. $opt['height'] = (isset($params['height'])) && $params['height'] ? (int) $params['height'] : null;
  469. $opt['width'] = (isset($params['width'])) && $params['width'] ? (int) $params['width'] : null;
  470. $opt['multiple'] = (isset($params['multiple']) && !($params['multiple'])) ? false : true;
  471. $opt['queued'] = (isset($params['queued']) && !($params['queued'])) ? (int) $params['queued'] : null;
  472. $opt['target'] = (isset($params['target'])) ? $params['target'] : '\\document.id(\'upload-browse\')';
  473. $opt['instantStart'] = (isset($params['instantStart']) && ($params['instantStart'])) ? true : false;
  474. $opt['allowDuplicates'] = (isset($params['allowDuplicates']) && !($params['allowDuplicates'])) ? false : true;
  475. // limitSize is the old parameter name. Remove in 1.7
  476. $opt['fileSizeMax'] = (isset($params['limitSize']) && ($params['limitSize'])) ? (int) $params['limitSize'] : null;
  477. // fileSizeMax is the new name. If supplied, it will override the old value specified for limitSize
  478. $opt['fileSizeMax'] = (isset($params['fileSizeMax']) && ($params['fileSizeMax'])) ? (int) $params['fileSizeMax'] : $opt['fileSizeMax'];
  479. $opt['fileSizeMin'] = (isset($params['fileSizeMin']) && ($params['fileSizeMin'])) ? (int) $params['fileSizeMin'] : null;
  480. // limitFiles is the old parameter name. Remove in 1.7
  481. $opt['fileListMax'] = (isset($params['limitFiles']) && ($params['limitFiles'])) ? (int) $params['limitFiles'] : null;
  482. // fileListMax is the new name. If supplied, it will override the old value specified for limitFiles
  483. $opt['fileListMax'] = (isset($params['fileListMax']) && ($params['fileListMax'])) ? (int) $params['fileListMax'] : $opt['fileListMax'];
  484. $opt['fileListSizeMax'] = (isset($params['fileListSizeMax']) && ($params['fileListSizeMax'])) ? (int) $params['fileListSizeMax'] : null;
  485. // types is the old parameter name. Remove in 1.7
  486. $opt['typeFilter'] = (isset($params['types'])) ? '\\' . $params['types']
  487. : '\\{Joomla.JText._(\'JLIB_HTML_BEHAVIOR_UPLOADER_ALL_FILES\'): \'*.*\'}';
  488. $opt['typeFilter'] = (isset($params['typeFilter'])) ? '\\' . $params['typeFilter'] : $opt['typeFilter'];
  489.  
  490. // Optional functions
  491. $opt['createReplacement'] = (isset($params['createReplacement'])) ? '\\' . $params['createReplacement'] : null;
  492. $opt['onFileComplete'] = (isset($params['onFileComplete'])) ? '\\' . $params['onFileComplete'] : null;
  493. $opt['onBeforeStart'] = (isset($params['onBeforeStart'])) ? '\\' . $params['onBeforeStart'] : null;
  494. $opt['onStart'] = (isset($params['onStart'])) ? '\\' . $params['onStart'] : null;
  495. $opt['onComplete'] = (isset($params['onComplete'])) ? '\\' . $params['onComplete'] : null;
  496. $opt['onFileSuccess'] = (isset($params['onFileSuccess'])) ? '\\' . $params['onFileSuccess'] : $onFileSuccess;
  497.  
  498. if (!isset($params['startButton']))
  499. {
  500. $params['startButton'] = 'upload-start';
  501. }
  502.  
  503. if (!isset($params['clearButton']))
  504. {
  505. $params['clearButton'] = 'upload-clear';
  506. }
  507.  
  508. $opt['onLoad'] = '\\function() {
  509. document.id(\'' . $id
  510. . '\').removeClass(\'hide\'); // we show the actual UI
  511. document.id(\'upload-noflash\').destroy(); // ... and hide the plain form
  512.  
  513. // We relay the interactions with the overlayed flash to the link
  514. this.target.addEvents({
  515. click: function() {
  516. return false;
  517. },
  518. mouseenter: function() {
  519. this.addClass(\'hover\');
  520. },
  521. mouseleave: function() {
  522. this.removeClass(\'hover\');
  523. this.blur();
  524. },
  525. mousedown: function() {
  526. this.focus();
  527. }
  528. });
  529.  
  530. // Interactions for the 2 other buttons
  531.  
  532. document.id(\'' . $params['clearButton']
  533. . '\').addEvent(\'click\', function() {
  534. Uploader.remove(); // remove all files
  535. return false;
  536. });
  537.  
  538. document.id(\'' . $params['startButton']
  539. . '\').addEvent(\'click\', function() {
  540. Uploader.start(); // start upload
  541. return false;
  542. });
  543. }';
  544.  
  545. $options = JHtmlBehavior::_getJSObject($opt);
  546.  
  547. // Attach tooltips to document
  548. $uploaderInit = 'window.addEvent(\'domready\', function(){
  549. var Uploader = new FancyUpload2(document.id(\'' . $id . '\'), document.id(\'' . $upload_queue . '\'), ' . $options . ' );
  550. });';
  551. $document->addScriptDeclaration($uploaderInit);
  552.  
  553. // Set static array
  554. self::$loaded[__METHOD__][$id] = true;
  555.  
  556. return;
  557. }
  558.  
  559. /**
  560. * Add unobtrusive javascript support for a collapsible tree.
  561. *
  562. * @param string $id An index
  563. * @param array $params An array of options.
  564. * @param array $root The root node
  565. *
  566. * @return void
  567. *
  568. * @since 11.1
  569. */
  570. public static function tree($id, $params = array(), $root = array())
  571. {
  572. // Include MooTools framework
  573. self::framework();
  574.  
  575. JHtml::_('script', 'system/mootree.js', true, true, false, false);
  576. JHtml::_('stylesheet', 'system/mootree.css', array(), true);
  577.  
  578. if (isset(self::$loaded[__METHOD__][$id]))
  579. {
  580. return;
  581. }
  582.  
  583. // Setup options object
  584. $opt['div'] = (array_key_exists('div', $params)) ? $params['div'] : $id . '_tree';
  585. $opt['mode'] = (array_key_exists('mode', $params)) ? $params['mode'] : 'folders';
  586. $opt['grid'] = (array_key_exists('grid', $params)) ? '\\' . $params['grid'] : true;
  587. $opt['theme'] = (array_key_exists('theme', $params)) ? $params['theme'] : JHtml::_('image', 'system/mootree.gif', '', array(), true, true);
  588.  
  589. // Event handlers
  590. $opt['onExpand'] = (array_key_exists('onExpand', $params)) ? '\\' . $params['onExpand'] : null;
  591. $opt['onSelect'] = (array_key_exists('onSelect', $params)) ? '\\' . $params['onSelect'] : null;
  592. $opt['onClick'] = (array_key_exists('onClick', $params)) ? '\\' . $params['onClick']
  593. : '\\function(node){ window.open(node.data.url, node.data.target != null ? node.data.target : \'_self\'); }';
  594.  
  595. $options = JHtmlBehavior::_getJSObject($opt);
  596.  
  597. // Setup root node
  598. $rt['text'] = (array_key_exists('text', $root)) ? $root['text'] : 'Root';
  599. $rt['id'] = (array_key_exists('id', $root)) ? $root['id'] : null;
  600. $rt['color'] = (array_key_exists('color', $root)) ? $root['color'] : null;
  601. $rt['open'] = (array_key_exists('open', $root)) ? '\\' . $root['open'] : true;
  602. $rt['icon'] = (array_key_exists('icon', $root)) ? $root['icon'] : null;
  603. $rt['openicon'] = (array_key_exists('openicon', $root)) ? $root['openicon'] : null;
  604. $rt['data'] = (array_key_exists('data', $root)) ? $root['data'] : null;
  605. $rootNode = JHtmlBehavior::_getJSObject($rt);
  606.  
  607. $treeName = (array_key_exists('treeName', $params)) ? $params['treeName'] : '';
  608.  
  609. $js = ' window.addEvent(\'domready\', function(){
  610. tree' . $treeName . ' = new MooTreeControl(' . $options . ',' . $rootNode . ');
  611. tree' . $treeName . '.adopt(\'' . $id . '\');})';
  612.  
  613. // Attach tooltips to document
  614. $document = JFactory::getDocument();
  615. $document->addScriptDeclaration($js);
  616.  
  617. // Set static array
  618. self::$loaded[__METHOD__][$id] = true;
  619.  
  620. return;
  621. }
  622.  
  623. /**
  624. * Add unobtrusive javascript support for a calendar control.
  625. *
  626. * @return void
  627. *
  628. * @since 11.1
  629. */
  630. public static function calendar()
  631. {
  632. // Only load once
  633. if (isset(self::$loaded[__METHOD__]))
  634. {
  635. return;
  636. }
  637.  
  638. $document = JFactory::getDocument();
  639. $tag = JFactory::getLanguage()->getTag();
  640.  
  641. JHtml::_('stylesheet', 'system/calendar-jos.css', array(' title' => JText::_('JLIB_HTML_BEHAVIOR_GREEN'), ' media' => 'all'), true);
  642. JHtml::_('script', $tag . '/calendar.js', false, true);
  643. JHtml::_('script', $tag . '/calendar-setup.js', false, true);
  644.  
  645. $translation = JHtmlBehavior::_calendartranslation();
  646. if ($translation)
  647. {
  648. $document->addScriptDeclaration($translation);
  649. }
  650. self::$loaded[__METHOD__] = true;
  651. }
  652.  
  653. /**
  654. * Add unobtrusive javascript support for a color picker.
  655. *
  656. * @return void
  657. *
  658. * @since 11.2
  659. */
  660. public static function colorpicker()
  661. {
  662. // Only load once
  663. if (isset(self::$loaded[__METHOD__]))
  664. {
  665. return;
  666. }
  667.  
  668. // Include MooTools framework
  669. self::framework(true);
  670.  
  671. JHtml::_('stylesheet', 'system/mooRainbow.css', array('media' => 'all'), true);
  672. JHtml::_('script', 'system/mooRainbow.js', false, true);
  673.  
  674. JFactory::getDocument()
  675. ->addScriptDeclaration(
  676. "window.addEvent('domready', function(){
  677. var nativeColorUi = false;
  678. if (Browser.opera && (Browser.version >= 11.5)) {
  679. nativeColorUi = true;
  680. }
  681. $$('.input-colorpicker').each(function(item){
  682. if (nativeColorUi) {
  683. item.type = 'color';
  684. } else {
  685. new MooRainbow(item, {
  686. id: item.id,
  687. imgPath: '" . JURI::root(true) . "/media/system/images/mooRainbow/',
  688. onComplete: function(color) {
  689. this.element.value = color.hex;
  690. },
  691. startColor: item.value.hexToRgb(true) ? item.value.hexToRgb(true) : [0, 0, 0]
  692. });
  693. }
  694. });
  695. });
  696. ");
  697.  
  698. self::$loaded[__METHOD__] = true;
  699. }
  700.  
  701. /**
  702. * Keep session alive, for example, while editing or creating an article.
  703. *
  704. * @return void
  705. *
  706. * @since 11.1
  707. */
  708. public static function keepalive()
  709. {
  710. // Only load once
  711. if (isset(self::$loaded[__METHOD__]))
  712. {
  713. return;
  714. }
  715.  
  716. // Include MooTools framework
  717. self::framework();
  718.  
  719. $config = JFactory::getConfig();
  720. $lifetime = ($config->get('lifetime') * 60000);
  721. $refreshTime = ($lifetime <= 60000) ? 30000 : $lifetime - 60000;
  722. // Refresh time is 1 minute less than the liftime assined in the configuration.php file.
  723.  
  724. // the longest refresh period is one hour to prevent integer overflow.
  725. if ($refreshTime > 3600000 || $refreshTime <= 0)
  726. {
  727. $refreshTime = 3600000;
  728. }
  729.  
  730. $document = JFactory::getDocument();
  731. $script = '';
  732. $script .= 'function keepAlive() {';
  733. $script .= ' var myAjax = new Request({method: "get", url: "index.php"}).send();';
  734. $script .= '}';
  735. $script .= ' window.addEvent("domready", function()';
  736. $script .= '{ keepAlive.periodical(' . $refreshTime . '); }';
  737. $script .= ');';
  738.  
  739. $document->addScriptDeclaration($script);
  740. self::$loaded[__METHOD__] = true;
  741.  
  742. return;
  743. }
  744.  
  745. /**
  746. * Highlight some words via Javascript.
  747. *
  748. * @param array $terms Array of words that should be highlighted.
  749. * @param string $start ID of the element that marks the begin of the section in which words
  750. * should be highlighted. Note this element will be removed from the DOM.
  751. * @param string $end ID of the element that end this section.
  752. * Note this element will be removed from the DOM.
  753. * @param string $className Class name of the element highlights are wrapped in.
  754. * @param string $tag Tag that will be used to wrap the highlighted words.
  755. *
  756. * @return void
  757. *
  758. * @since 11.4
  759. */
  760. public static function highlighter(array $terms, $start = 'highlighter-start', $end = 'highlighter-end', $className = 'highlight', $tag = 'span')
  761. {
  762. $sig = md5(serialize(array($terms, $start, $end)));
  763. if (isset(self::$loaded[__METHOD__][$sig]))
  764. {
  765. return;
  766. }
  767.  
  768. JHtml::_('script', 'system/highlighter.js', true, true);
  769.  
  770. $terms = str_replace('"', '\"', $terms);
  771.  
  772. $document = JFactory::getDocument();
  773. $document->addScriptDeclaration("
  774. window.addEvent('domready', function () {
  775. var start = document.id('" . $start . "');
  776. var end = document.id('" . $end . "');
  777. if (!start || !end || !Joomla.Highlighter) {
  778. return true;
  779. }
  780. highlighter = new Joomla.Highlighter({
  781. startElement: start,
  782. endElement: end,
  783. className: '" . $className . "',
  784. onlyWords: false,
  785. tag: '" . $tag . "'
  786. }).highlight([\"" . implode('","', $terms) . "\"]);
  787. start.dispose();
  788. end.dispose();
  789. });
  790. ");
  791.  
  792. self::$loaded[__METHOD__][$sig] = true;
  793.  
  794. return;
  795. }
  796.  
  797. /**
  798. * Break us out of any containing iframes
  799. *
  800. * @param string $location Location to display in
  801. *
  802. * @return void
  803. *
  804. * @since 11.1
  805. */
  806. public static function noframes($location = 'top.location.href')
  807. {
  808. // Only load once
  809. if (isset(self::$loaded[__METHOD__]))
  810. {
  811. return;
  812. }
  813.  
  814. // Include MooTools framework
  815. self::framework();
  816.  
  817. $js = "window.addEvent('domready', function () {if (top == self) {document.documentElement.style.display = 'block'; }" .
  818. " else {top.location = self.location; }});";
  819. $document = JFactory::getDocument();
  820. $document->addStyleDeclaration('html { display:none }');
  821. $document->addScriptDeclaration($js);
  822.  
  823. JResponse::setHeader('X-Frames-Options', 'SAME-ORIGIN');
  824.  
  825. self::$loaded[__METHOD__] = true;
  826. }
  827.  
  828. /**
  829. * Internal method to get a JavaScript object notation string from an array
  830. *
  831. * @param array $array The array to convert to JavaScript object notation
  832. *
  833. * @return string JavaScript object notation representation of the array
  834. *
  835. * @since 11.1
  836. */
  837. protected static function _getJSObject($array = array())
  838. {
  839. // Initialise variables.
  840. $object = '{';
  841.  
  842. // Iterate over array to build objects
  843. foreach ((array) $array as $k => $v)
  844. {
  845. if (is_null($v))
  846. {
  847. continue;
  848. }
  849.  
  850. if (is_bool($v))
  851. {
  852. if ($k === 'fullScreen')
  853. {
  854. $object .= 'size: { ';
  855. $object .= 'x: ';
  856. $object .= 'window.getSize().x-80';
  857. $object .= ',';
  858. $object .= 'y: ';
  859. $object .= 'window.getSize().y-80';
  860. $object .= ' }';
  861. $object .= ',';
  862. }
  863. else
  864. {
  865. $object .= ' ' . $k . ': ';
  866. $object .= ($v) ? 'true' : 'false';
  867. $object .= ',';
  868. }
  869. }
  870. elseif (!is_array($v) && !is_object($v))
  871. {
  872. $object .= ' ' . $k . ': ';
  873. $object .= (is_numeric($v) || strpos($v, '\\') === 0) ? (is_numeric($v)) ? $v : substr($v, 1) : "'" . $v . "'";
  874. $object .= ',';
  875. }
  876. else
  877. {
  878. $object .= ' ' . $k . ': ' . JHtmlBehavior::_getJSObject($v) . ',';
  879. }
  880. }
  881.  
  882. if (substr($object, -1) == ',')
  883. {
  884. $object = substr($object, 0, -1);
  885. }
  886.  
  887. $object .= '}';
  888.  
  889. return $object;
  890. }
  891.  
  892. /**
  893. * Internal method to translate the JavaScript Calendar
  894. *
  895. * @return string JavaScript that translates the object
  896. *
  897. * @since 11.1
  898. */
  899. protected static function _calendartranslation()
  900. {
  901. static $jsscript = 0;
  902.  
  903. if ($jsscript == 0)
  904. {
  905. $return = 'Calendar._DN = new Array ("' . JText::_('SUNDAY', true) . '", "' . JText::_('MONDAY', true) . '", "'
  906. . JText::_('TUESDAY', true) . '", "' . JText::_('WEDNESDAY', true) . '", "' . JText::_('THURSDAY', true) . '", "'
  907. . JText::_('FRIDAY', true) . '", "' . JText::_('SATURDAY', true) . '", "' . JText::_('SUNDAY', true) . '");'
  908. . ' Calendar._SDN = new Array ("' . JText::_('SUN', true) . '", "' . JText::_('MON', true) . '", "' . JText::_('TUE', true) . '", "'
  909. . JText::_('WED', true) . '", "' . JText::_('THU', true) . '", "' . JText::_('FRI', true) . '", "' . JText::_('SAT', true) . '", "'
  910. . JText::_('SUN', true) . '");' . ' Calendar._FD = 0;' . ' Calendar._MN = new Array ("' . JText::_('JANUARY', true) . '", "'
  911. . JText::_('FEBRUARY', true) . '", "' . JText::_('MARCH', true) . '", "' . JText::_('APRIL', true) . '", "' . JText::_('MAY', true)
  912. . '", "' . JText::_('JUNE', true) . '", "' . JText::_('JULY', true) . '", "' . JText::_('AUGUST', true) . '", "'
  913. . JText::_('SEPTEMBER', true) . '", "' . JText::_('OCTOBER', true) . '", "' . JText::_('NOVEMBER', true) . '", "'
  914. . JText::_('DECEMBER', true) . '");' . ' Calendar._SMN = new Array ("' . JText::_('JANUARY_SHORT', true) . '", "'
  915. . JText::_('FEBRUARY_SHORT', true) . '", "' . JText::_('MARCH_SHORT', true) . '", "' . JText::_('APRIL_SHORT', true) . '", "'
  916. . JText::_('MAY_SHORT', true) . '", "' . JText::_('JUNE_SHORT', true) . '", "' . JText::_('JULY_SHORT', true) . '", "'
  917. . JText::_('AUGUST_SHORT', true) . '", "' . JText::_('SEPTEMBER_SHORT', true) . '", "' . JText::_('OCTOBER_SHORT', true) . '", "'
  918. . JText::_('NOVEMBER_SHORT', true) . '", "' . JText::_('DECEMBER_SHORT', true) . '");'
  919. . ' Calendar._TT = {};Calendar._TT["INFO"] = "' . JText::_('JLIB_HTML_BEHAVIOR_ABOUT_THE_CALENDAR', true) . '";'
  920. . ' Calendar._TT["ABOUT"] =
  921. "DHTML Date/Time Selector\n" +
  922. "(c) dynarch.com 2002-2005 / Author: Mihai Bazon\n" +
  923. "For latest version visit: http://www.dynarch.com/projects/calendar/\n" +
  924. "Distributed under GNU LGPL. See http://gnu.org/licenses/lgpl.html for details." +
  925. "\n\n" +
  926. "' . JText::_('JLIB_HTML_BEHAVIOR_DATE_SELECTION', false, false) . '" +
  927. "' . JText::_('JLIB_HTML_BEHAVIOR_YEAR_SELECT', false, false) . '" +
  928. "' . JText::_('JLIB_HTML_BEHAVIOR_MONTH_SELECT', false, false) . '" +
  929. "' . JText::_('JLIB_HTML_BEHAVIOR_HOLD_MOUSE', false, false)
  930. . '";
  931. Calendar._TT["ABOUT_TIME"] = "\n\n" +
  932. "Time selection:\n" +
  933. "- Click on any of the time parts to increase it\n" +
  934. "- or Shift-click to decrease it\n" +
  935. "- or click and drag for faster selection.";
  936.  
  937. Calendar._TT["PREV_YEAR"] = "' . JText::_('JLIB_HTML_BEHAVIOR_PREV_YEAR_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["PREV_MONTH"] = "'
  938. . JText::_('JLIB_HTML_BEHAVIOR_PREV_MONTH_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["GO_TODAY"] = "'
  939. . JText::_('JLIB_HTML_BEHAVIOR_GO_TODAY', true) . '";' . ' Calendar._TT["NEXT_MONTH"] = "'
  940. . JText::_('JLIB_HTML_BEHAVIOR_NEXT_MONTH_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["NEXT_YEAR"] = "'
  941. . JText::_('JLIB_HTML_BEHAVIOR_NEXT_YEAR_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["SEL_DATE"] = "'
  942. . JText::_('JLIB_HTML_BEHAVIOR_SELECT_DATE', true) . '";' . ' Calendar._TT["DRAG_TO_MOVE"] = "'
  943. . JText::_('JLIB_HTML_BEHAVIOR_DRAG_TO_MOVE', true) . '";' . ' Calendar._TT["PART_TODAY"] = "'
  944. . JText::_('JLIB_HTML_BEHAVIOR_TODAY', true) . '";' . ' Calendar._TT["DAY_FIRST"] = "'
  945. . JText::_('JLIB_HTML_BEHAVIOR_DISPLAY_S_FIRST', true) . '";' . ' Calendar._TT["WEEKEND"] = "0,6";' . ' Calendar._TT["CLOSE"] = "'
  946. . JText::_('JLIB_HTML_BEHAVIOR_CLOSE', true) . '";' . ' Calendar._TT["TODAY"] = "' . JText::_('JLIB_HTML_BEHAVIOR_TODAY', true)
  947. . '";' . ' Calendar._TT["TIME_PART"] = "' . JText::_('JLIB_HTML_BEHAVIOR_SHIFT_CLICK_OR_DRAG_TO_CHANGE_VALUE', true) . '";'
  948. . ' Calendar._TT["DEF_DATE_FORMAT"] = "%Y-%m-%d";' . ' Calendar._TT["TT_DATE_FORMAT"] = "'
  949. . JText::_('JLIB_HTML_BEHAVIOR_TT_DATE_FORMAT', true) . '";' . ' Calendar._TT["WK"] = "' . JText::_('JLIB_HTML_BEHAVIOR_WK', true) . '";'
  950. . ' Calendar._TT["TIME"] = "' . JText::_('JLIB_HTML_BEHAVIOR_TIME', true) . '";';
  951. $jsscript = 1;
  952. return $return;
  953. }
  954. else
  955. {
  956. return false;
  957. }
  958. }
  959. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement