Container.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dojo
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Dojo */
  23. require_once 'Zend/Dojo.php';
  24. /**
  25. * Container for Dojo View Helper
  26. *
  27. *
  28. * @package Zend_Dojo
  29. * @subpackage View
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Dojo_View_Helper_Dojo_Container
  34. {
  35. /**
  36. * @var Zend_View_Interface
  37. */
  38. public $view;
  39. /**
  40. * addOnLoad capture lock
  41. * @var bool
  42. */
  43. protected $_captureLock = false;
  44. /**
  45. * addOnLoad object on which to apply lambda
  46. * @var string
  47. */
  48. protected $_captureObj;
  49. /**
  50. * Base CDN url to utilize
  51. * @var string
  52. */
  53. protected $_cdnBase = Zend_Dojo::CDN_BASE_GOOGLE;
  54. /**
  55. * Path segment following version string of CDN path
  56. * @var string
  57. */
  58. protected $_cdnDojoPath = Zend_Dojo::CDN_DOJO_PATH_GOOGLE;
  59. /**
  60. * Dojo version to use from CDN
  61. * @var string
  62. */
  63. protected $_cdnVersion = '1.3.2';
  64. /**
  65. * Has the dijit loader been registered?
  66. * @var bool
  67. */
  68. protected $_dijitLoaderRegistered = false;
  69. /**
  70. * Registered programmatic dijits
  71. * @var array
  72. */
  73. protected $_dijits = array();
  74. /**
  75. * Dojo configuration
  76. * @var array
  77. */
  78. protected $_djConfig = array();
  79. /**
  80. * Whether or not dojo is enabled
  81. * @var bool
  82. */
  83. protected $_enabled = false;
  84. /**
  85. * Are we rendering as XHTML?
  86. * @var bool
  87. */
  88. protected $_isXhtml = false;
  89. /**
  90. * Arbitrary javascript to include in dojo script
  91. * @var array
  92. */
  93. protected $_javascriptStatements = array();
  94. /**
  95. * Dojo layers (custom builds) to use
  96. * @var array
  97. */
  98. protected $_layers = array();
  99. /**
  100. * Relative path to dojo
  101. * @var string
  102. */
  103. protected $_localPath = null;
  104. /**
  105. * Root of dojo where all dojo files are installed
  106. * @var string
  107. */
  108. protected $_localRelativePath = null;
  109. /**
  110. * Modules to require
  111. * @var array
  112. */
  113. protected $_modules = array();
  114. /**
  115. * Registered module paths
  116. * @var array
  117. */
  118. protected $_modulePaths = array();
  119. /**
  120. * Actions to perform on window load
  121. * @var array
  122. */
  123. protected $_onLoadActions = array();
  124. /**
  125. * Register the Dojo stylesheet?
  126. * @var bool
  127. */
  128. protected $_registerDojoStylesheet = false;
  129. /**
  130. * Style sheet modules to load
  131. * @var array
  132. */
  133. protected $_stylesheetModules = array();
  134. /**
  135. * Local stylesheets
  136. * @var array
  137. */
  138. protected $_stylesheets = array();
  139. /**
  140. * Array of onLoad events specific to Zend_Dojo integration operations
  141. * @var array
  142. */
  143. protected $_zendLoadActions = array();
  144. /**
  145. * Set view object
  146. *
  147. * @param Zend_Dojo_View_Interface $view
  148. * @return void
  149. */
  150. public function setView(Zend_View_Interface $view)
  151. {
  152. $this->view = $view;
  153. }
  154. /**
  155. * Enable dojo
  156. *
  157. * @return Zend_Dojo_View_Helper_Dojo_Container
  158. */
  159. public function enable()
  160. {
  161. $this->_enabled = true;
  162. return $this;
  163. }
  164. /**
  165. * Disable dojo
  166. *
  167. * @return Zend_Dojo_View_Helper_Dojo_Container
  168. */
  169. public function disable()
  170. {
  171. $this->_enabled = false;
  172. return $this;
  173. }
  174. /**
  175. * Is dojo enabled?
  176. *
  177. * @return bool
  178. */
  179. public function isEnabled()
  180. {
  181. return $this->_enabled;
  182. }
  183. /**
  184. * Add options for the Dojo Container to use
  185. *
  186. * @param array|Zend_Config Array or Zend_Config object with options to use
  187. * @return Zend_Dojo_View_Helper_Dojo_Container
  188. */
  189. public function setOptions($options)
  190. {
  191. if($options instanceof Zend_Config) {
  192. $options = $options->toArray();
  193. }
  194. foreach($options as $key => $value) {
  195. $key = strtolower($key);
  196. switch($key) {
  197. case 'requiremodules':
  198. $this->requireModule($value);
  199. break;
  200. case 'modulepaths':
  201. foreach($value as $module => $path) {
  202. $this->registerModulePath($module, $path);
  203. }
  204. break;
  205. case 'layers':
  206. $value = (array) $value;
  207. foreach($value as $layer) {
  208. $this->addLayer($layer);
  209. }
  210. break;
  211. case 'cdnbase':
  212. $this->setCdnBase($value);
  213. break;
  214. case 'cdnversion':
  215. $this->setCdnVersion($value);
  216. break;
  217. case 'cdndojopath':
  218. $this->setCdnDojoPath($value);
  219. break;
  220. case 'localpath':
  221. $this->setLocalPath($value);
  222. break;
  223. case 'djconfig':
  224. $this->setDjConfig($value);
  225. break;
  226. case 'stylesheetmodules':
  227. $value = (array) $value;
  228. foreach($value as $module) {
  229. $this->addStylesheetModule($module);
  230. }
  231. break;
  232. case 'stylesheets':
  233. $value = (array) $value;
  234. foreach($value as $stylesheet) {
  235. $this->addStylesheet($stylesheet);
  236. }
  237. break;
  238. case 'registerdojostylesheet':
  239. $this->registerDojoStylesheet($value);
  240. break;
  241. }
  242. }
  243. return $this;
  244. }
  245. /**
  246. * Specify one or multiple modules to require
  247. *
  248. * @param string|array $modules
  249. * @return Zend_Dojo_View_Helper_Dojo_Container
  250. */
  251. public function requireModule($modules)
  252. {
  253. if (!is_string($modules) && !is_array($modules)) {
  254. require_once 'Zend/Dojo/View/Exception.php';
  255. throw new Zend_Dojo_View_Exception('Invalid module name specified; must be a string or an array of strings');
  256. }
  257. $modules = (array) $modules;
  258. foreach ($modules as $mod) {
  259. if (!preg_match('/^[a-z][a-z0-9._-]+$/i', $mod)) {
  260. require_once 'Zend/Dojo/View/Exception.php';
  261. throw new Zend_Dojo_View_Exception(sprintf('Module name specified, "%s", contains invalid characters', (string) $mod));
  262. }
  263. if (!in_array($mod, $this->_modules)) {
  264. $this->_modules[] = $mod;
  265. }
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Retrieve list of modules to require
  271. *
  272. * @return array
  273. */
  274. public function getModules()
  275. {
  276. return $this->_modules;
  277. }
  278. /**
  279. * Register a module path
  280. *
  281. * @param string $module The module to register a path for
  282. * @param string $path The path to register for the module
  283. * @return Zend_Dojo_View_Helper_Dojo_Container
  284. */
  285. public function registerModulePath($module, $path)
  286. {
  287. $path = (string) $path;
  288. if (!in_array($module, $this->_modulePaths)) {
  289. $this->_modulePaths[$module] = $path;
  290. }
  291. return $this;
  292. }
  293. /**
  294. * List registered module paths
  295. *
  296. * @return array
  297. */
  298. public function getModulePaths()
  299. {
  300. return $this->_modulePaths;
  301. }
  302. /**
  303. * Add layer (custom build) path
  304. *
  305. * @param string $path
  306. * @return Zend_Dojo_View_Helper_Dojo_Container
  307. */
  308. public function addLayer($path)
  309. {
  310. $path = (string) $path;
  311. if (!in_array($path, $this->_layers)) {
  312. $this->_layers[] = $path;
  313. }
  314. return $this;
  315. }
  316. /**
  317. * Get registered layers
  318. *
  319. * @return array
  320. */
  321. public function getLayers()
  322. {
  323. return $this->_layers;
  324. }
  325. /**
  326. * Remove a registered layer
  327. *
  328. * @param string $path
  329. * @return Zend_Dojo_View_Helper_Dojo_Container
  330. */
  331. public function removeLayer($path)
  332. {
  333. $path = (string) $path;
  334. $layers = array_flip($this->_layers);
  335. if (array_key_exists($path, $layers)) {
  336. unset($layers[$path]);
  337. $this->_layers = array_keys($layers);
  338. }
  339. return $this;
  340. }
  341. /**
  342. * Clear all registered layers
  343. *
  344. * @return Zend_Dojo_View_Helper_Dojo_Container
  345. */
  346. public function clearLayers()
  347. {
  348. $this->_layers = array();
  349. return $this;
  350. }
  351. /**
  352. * Set CDN base path
  353. *
  354. * @param string $url
  355. * @return Zend_Dojo_View_Helper_Dojo_Container
  356. */
  357. public function setCdnBase($url)
  358. {
  359. $this->_cdnBase = (string) $url;
  360. return $this;
  361. }
  362. /**
  363. * Return CDN base URL
  364. *
  365. * @return string
  366. */
  367. public function getCdnBase()
  368. {
  369. return $this->_cdnBase;
  370. }
  371. /**
  372. * Use CDN, using version specified
  373. *
  374. * @param string $version
  375. * @return Zend_Dojo_View_Helper_Dojo_Container
  376. */
  377. public function setCdnVersion($version = null)
  378. {
  379. $this->enable();
  380. if (preg_match('/^[1-9]\.[0-9](\.[0-9])?$/', $version)) {
  381. $this->_cdnVersion = $version;
  382. }
  383. return $this;
  384. }
  385. /**
  386. * Get CDN version
  387. *
  388. * @return string
  389. */
  390. public function getCdnVersion()
  391. {
  392. return $this->_cdnVersion;
  393. }
  394. /**
  395. * Set CDN path to dojo (relative to CDN base + version)
  396. *
  397. * @param string $path
  398. * @return Zend_Dojo_View_Helper_Dojo_Container
  399. */
  400. public function setCdnDojoPath($path)
  401. {
  402. $this->_cdnDojoPath = (string) $path;
  403. return $this;
  404. }
  405. /**
  406. * Get CDN path to dojo (relative to CDN base + version)
  407. *
  408. * @return string
  409. */
  410. public function getCdnDojoPath()
  411. {
  412. return $this->_cdnDojoPath;
  413. }
  414. /**
  415. * Are we using the CDN?
  416. *
  417. * @return bool
  418. */
  419. public function useCdn()
  420. {
  421. return !$this->useLocalPath();
  422. }
  423. /**
  424. * Set path to local dojo
  425. *
  426. * @param string $path
  427. * @return Zend_Dojo_View_Helper_Dojo_Container
  428. */
  429. public function setLocalPath($path)
  430. {
  431. $this->enable();
  432. $this->_localPath = (string) $path;
  433. return $this;
  434. }
  435. /**
  436. * Get local path to dojo
  437. *
  438. * @return string
  439. */
  440. public function getLocalPath()
  441. {
  442. return $this->_localPath;
  443. }
  444. /**
  445. * Are we using a local path?
  446. *
  447. * @return bool
  448. */
  449. public function useLocalPath()
  450. {
  451. return (null === $this->_localPath) ? false : true;
  452. }
  453. /**
  454. * Set Dojo configuration
  455. *
  456. * @param string $option
  457. * @param mixed $value
  458. * @return Zend_Dojo_View_Helper_Dojo_Container
  459. */
  460. public function setDjConfig(array $config)
  461. {
  462. $this->_djConfig = $config;
  463. return $this;
  464. }
  465. /**
  466. * Set Dojo configuration option
  467. *
  468. * @param string $option
  469. * @param mixed $value
  470. * @return Zend_Dojo_View_Helper_Dojo_Container
  471. */
  472. public function setDjConfigOption($option, $value)
  473. {
  474. $option = (string) $option;
  475. $this->_djConfig[$option] = $value;
  476. return $this;
  477. }
  478. /**
  479. * Retrieve dojo configuration values
  480. *
  481. * @return array
  482. */
  483. public function getDjConfig()
  484. {
  485. return $this->_djConfig;
  486. }
  487. /**
  488. * Get dojo configuration value
  489. *
  490. * @param string $option
  491. * @param mixed $default
  492. * @return mixed
  493. */
  494. public function getDjConfigOption($option, $default = null)
  495. {
  496. $option = (string) $option;
  497. if (array_key_exists($option, $this->_djConfig)) {
  498. return $this->_djConfig[$option];
  499. }
  500. return $default;
  501. }
  502. /**
  503. * Add a stylesheet by module name
  504. *
  505. * @param string $module
  506. * @return Zend_Dojo_View_Helper_Dojo_Container
  507. */
  508. public function addStylesheetModule($module)
  509. {
  510. if (!preg_match('/^[a-z0-9]+\.[a-z0-9_-]+(\.[a-z0-9_-]+)*$/i', $module)) {
  511. require_once 'Zend/Dojo/View/Exception.php';
  512. throw new Zend_Dojo_View_Exception('Invalid stylesheet module specified');
  513. }
  514. if (!in_array($module, $this->_stylesheetModules)) {
  515. $this->_stylesheetModules[] = $module;
  516. }
  517. return $this;
  518. }
  519. /**
  520. * Get all stylesheet modules currently registered
  521. *
  522. * @return array
  523. */
  524. public function getStylesheetModules()
  525. {
  526. return $this->_stylesheetModules;
  527. }
  528. /**
  529. * Add a stylesheet
  530. *
  531. * @param string $path
  532. * @return Zend_Dojo_View_Helper_Dojo_Container
  533. */
  534. public function addStylesheet($path)
  535. {
  536. $path = (string) $path;
  537. if (!in_array($path, $this->_stylesheets)) {
  538. $this->_stylesheets[] = (string) $path;
  539. }
  540. return $this;
  541. }
  542. /**
  543. * Register the dojo.css stylesheet?
  544. *
  545. * With no arguments, returns the status of the flag; with arguments, sets
  546. * the flag and returns the object.
  547. *
  548. * @param null|bool $flag
  549. * @return Zend_Dojo_View_Helper_Dojo_Container|bool
  550. */
  551. public function registerDojoStylesheet($flag = null)
  552. {
  553. if (null === $flag) {
  554. return $this->_registerDojoStylesheet;
  555. }
  556. $this->_registerDojoStylesheet = (bool) $flag;
  557. return $this;
  558. }
  559. /**
  560. * Retrieve registered stylesheets
  561. *
  562. * @return array
  563. */
  564. public function getStylesheets()
  565. {
  566. return $this->_stylesheets;
  567. }
  568. /**
  569. * Add a script to execute onLoad
  570. *
  571. * dojo.addOnLoad accepts:
  572. * - function name
  573. * - lambda
  574. *
  575. * @param string $callback Lambda
  576. * @return Zend_Dojo_View_Helper_Dojo_Container
  577. */
  578. public function addOnLoad($callback)
  579. {
  580. if (!in_array($callback, $this->_onLoadActions, true)) {
  581. $this->_onLoadActions[] = $callback;
  582. }
  583. return $this;
  584. }
  585. /**
  586. * Prepend an onLoad event to the list of onLoad actions
  587. *
  588. * @param string $callback Lambda
  589. * @return Zend_Dojo_View_Helper_Dojo_Container
  590. */
  591. public function prependOnLoad($callback)
  592. {
  593. if (!in_array($callback, $this->_onLoadActions, true)) {
  594. array_unshift($this->_onLoadActions, $callback);
  595. }
  596. return $this;
  597. }
  598. /**
  599. * Retrieve all registered onLoad actions
  600. *
  601. * @return array
  602. */
  603. public function getOnLoadActions()
  604. {
  605. return $this->_onLoadActions;
  606. }
  607. /**
  608. * Start capturing routines to run onLoad
  609. *
  610. * @return bool
  611. */
  612. public function onLoadCaptureStart()
  613. {
  614. if ($this->_captureLock) {
  615. require_once 'Zend/Dojo/View/Exception.php';
  616. throw new Zend_Dojo_View_Exception('Cannot nest onLoad captures');
  617. }
  618. $this->_captureLock = true;
  619. ob_start();
  620. return;
  621. }
  622. /**
  623. * Stop capturing routines to run onLoad
  624. *
  625. * @return bool
  626. */
  627. public function onLoadCaptureEnd()
  628. {
  629. $data = ob_get_clean();
  630. $this->_captureLock = false;
  631. $this->addOnLoad($data);
  632. return true;
  633. }
  634. /**
  635. * Add a programmatic dijit
  636. *
  637. * @param string $id
  638. * @param array $params
  639. * @return Zend_Dojo_View_Helper_Dojo_Container
  640. */
  641. public function addDijit($id, array $params)
  642. {
  643. if (array_key_exists($id, $this->_dijits)) {
  644. require_once 'Zend/Dojo/View/Exception.php';
  645. throw new Zend_Dojo_View_Exception(sprintf('Duplicate dijit with id "%s" already registered', $id));
  646. }
  647. $this->_dijits[$id] = array(
  648. 'id' => $id,
  649. 'params' => $params,
  650. );
  651. return $this;
  652. }
  653. /**
  654. * Set a programmatic dijit (overwrites)
  655. *
  656. * @param string $id
  657. * @param array $params
  658. * @return Zend_Dojo_View_Helper_Dojo_Container
  659. */
  660. public function setDijit($id, array $params)
  661. {
  662. $this->removeDijit($id);
  663. return $this->addDijit($id, $params);
  664. }
  665. /**
  666. * Add multiple dijits at once
  667. *
  668. * Expects an array of id => array $params pairs
  669. *
  670. * @param array $dijits
  671. * @return Zend_Dojo_View_Helper_Dojo_Container
  672. */
  673. public function addDijits(array $dijits)
  674. {
  675. foreach ($dijits as $id => $params) {
  676. $this->addDijit($id, $params);
  677. }
  678. return $this;
  679. }
  680. /**
  681. * Set multiple dijits at once (overwrites)
  682. *
  683. * Expects an array of id => array $params pairs
  684. *
  685. * @param array $dijits
  686. * @return Zend_Dojo_View_Helper_Dojo_Container
  687. */
  688. public function setDijits(array $dijits)
  689. {
  690. $this->clearDijits();
  691. return $this->addDijits($dijits);
  692. }
  693. /**
  694. * Is the given programmatic dijit already registered?
  695. *
  696. * @param string $id
  697. * @return bool
  698. */
  699. public function hasDijit($id)
  700. {
  701. return array_key_exists($id, $this->_dijits);
  702. }
  703. /**
  704. * Retrieve a dijit by id
  705. *
  706. * @param string $id
  707. * @return array|null
  708. */
  709. public function getDijit($id)
  710. {
  711. if ($this->hasDijit($id)) {
  712. return $this->_dijits[$id]['params'];
  713. }
  714. return null;
  715. }
  716. /**
  717. * Retrieve all dijits
  718. *
  719. * Returns dijits as an array of assoc arrays
  720. *
  721. * @return array
  722. */
  723. public function getDijits()
  724. {
  725. return array_values($this->_dijits);
  726. }
  727. /**
  728. * Remove a programmatic dijit if it exists
  729. *
  730. * @param string $id
  731. * @return Zend_Dojo_View_Helper_Dojo_Container
  732. */
  733. public function removeDijit($id)
  734. {
  735. if (array_key_exists($id, $this->_dijits)) {
  736. unset($this->_dijits[$id]);
  737. }
  738. return $this;
  739. }
  740. /**
  741. * Clear all dijits
  742. *
  743. * @return Zend_Dojo_View_Helper_Dojo_Container
  744. */
  745. public function clearDijits()
  746. {
  747. $this->_dijits = array();
  748. return $this;
  749. }
  750. /**
  751. * Render dijits as JSON structure
  752. *
  753. * @return string
  754. */
  755. public function dijitsToJson()
  756. {
  757. require_once 'Zend/Json.php';
  758. return Zend_Json::encode($this->getDijits());
  759. }
  760. /**
  761. * Create dijit loader functionality
  762. *
  763. * @return void
  764. */
  765. public function registerDijitLoader()
  766. {
  767. if (!$this->_dijitLoaderRegistered) {
  768. $js =<<<EOJ
  769. function() {
  770. dojo.forEach(zendDijits, function(info) {
  771. var n = dojo.byId(info.id);
  772. if (null != n) {
  773. dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
  774. }
  775. });
  776. dojo.parser.parse();
  777. }
  778. EOJ;
  779. $this->requireModule('dojo.parser');
  780. $this->_addZendLoad($js);
  781. $this->addJavascript('var zendDijits = ' . $this->dijitsToJson() . ';');
  782. $this->_dijitLoaderRegistered = true;
  783. }
  784. }
  785. /**
  786. * Add arbitrary javascript to execute in dojo JS container
  787. *
  788. * @param string $js
  789. * @return Zend_Dojo_View_Helper_Dojo_Container
  790. */
  791. public function addJavascript($js)
  792. {
  793. $js = preg_replace('/^\s*(.*?)\s*$/s', '$1', $js);
  794. if (!in_array(substr($js, -1), array(';', '}'))) {
  795. $js .= ';';
  796. }
  797. if (in_array($js, $this->_javascriptStatements)) {
  798. return $this;
  799. }
  800. $this->_javascriptStatements[] = $js;
  801. return $this;
  802. }
  803. /**
  804. * Return all registered javascript statements
  805. *
  806. * @return array
  807. */
  808. public function getJavascript()
  809. {
  810. return $this->_javascriptStatements;
  811. }
  812. /**
  813. * Clear arbitrary javascript stack
  814. *
  815. * @return Zend_Dojo_View_Helper_Dojo_Container
  816. */
  817. public function clearJavascript()
  818. {
  819. $this->_javascriptStatements = array();
  820. return $this;
  821. }
  822. /**
  823. * Capture arbitrary javascript to include in dojo script
  824. *
  825. * @return void
  826. */
  827. public function javascriptCaptureStart()
  828. {
  829. if ($this->_captureLock) {
  830. require_once 'Zend/Dojo/View/Exception.php';
  831. throw new Zend_Dojo_View_Exception('Cannot nest captures');
  832. }
  833. $this->_captureLock = true;
  834. ob_start();
  835. return;
  836. }
  837. /**
  838. * Finish capturing arbitrary javascript to include in dojo script
  839. *
  840. * @return true
  841. */
  842. public function javascriptCaptureEnd()
  843. {
  844. $data = ob_get_clean();
  845. $this->_captureLock = false;
  846. $this->addJavascript($data);
  847. return true;
  848. }
  849. /**
  850. * String representation of dojo environment
  851. *
  852. * @return string
  853. */
  854. public function __toString()
  855. {
  856. if (!$this->isEnabled()) {
  857. return '';
  858. }
  859. $this->_isXhtml = $this->view->doctype()->isXhtml();
  860. if (Zend_Dojo_View_Helper_Dojo::useDeclarative()) {
  861. if (null === $this->getDjConfigOption('parseOnLoad')) {
  862. $this->setDjConfigOption('parseOnLoad', true);
  863. }
  864. }
  865. if (!empty($this->_dijits)) {
  866. $this->registerDijitLoader();
  867. }
  868. $html = $this->_renderStylesheets() . PHP_EOL
  869. . $this->_renderDjConfig() . PHP_EOL
  870. . $this->_renderDojoScriptTag() . PHP_EOL
  871. . $this->_renderLayers() . PHP_EOL
  872. . $this->_renderExtras();
  873. return $html;
  874. }
  875. /**
  876. * Retrieve local path to dojo resources for building relative paths
  877. *
  878. * @return string
  879. */
  880. protected function _getLocalRelativePath()
  881. {
  882. if (null === $this->_localRelativePath) {
  883. $localPath = $this->getLocalPath();
  884. $localPath = preg_replace('|[/\\\\]dojo[/\\\\]dojo.js[^/\\\\]*$|i', '', $localPath);
  885. $this->_localRelativePath = $localPath;
  886. }
  887. return $this->_localRelativePath;
  888. }
  889. /**
  890. * Render dojo stylesheets
  891. *
  892. * @return string
  893. */
  894. protected function _renderStylesheets()
  895. {
  896. if ($this->useCdn()) {
  897. $base = $this->getCdnBase()
  898. . $this->getCdnVersion();
  899. } else {
  900. $base = $this->_getLocalRelativePath();
  901. }
  902. $registeredStylesheets = $this->getStylesheetModules();
  903. foreach ($registeredStylesheets as $stylesheet) {
  904. $themeName = substr($stylesheet, strrpos($stylesheet, '.') + 1);
  905. $stylesheet = str_replace('.', '/', $stylesheet);
  906. $stylesheets[] = $base . '/' . $stylesheet . '/' . $themeName . '.css';
  907. }
  908. foreach ($this->getStylesheets() as $stylesheet) {
  909. $stylesheets[] = $stylesheet;
  910. }
  911. if ($this->_registerDojoStylesheet) {
  912. $stylesheets[] = $base . '/dojo/resources/dojo.css';
  913. }
  914. if (empty($stylesheets)) {
  915. return '';
  916. }
  917. array_reverse($stylesheets);
  918. $style = '<style type="text/css">' . PHP_EOL
  919. . (($this->_isXhtml) ? '<!--' : '<!--') . PHP_EOL;
  920. foreach ($stylesheets as $stylesheet) {
  921. $style .= ' @import "' . $stylesheet . '";' . PHP_EOL;
  922. }
  923. $style .= (($this->_isXhtml) ? '-->' : '-->') . PHP_EOL
  924. . '</style>';
  925. return $style;
  926. }
  927. /**
  928. * Render DjConfig values
  929. *
  930. * @return string
  931. */
  932. protected function _renderDjConfig()
  933. {
  934. $djConfigValues = $this->getDjConfig();
  935. if (empty($djConfigValues)) {
  936. return '';
  937. }
  938. require_once 'Zend/Json.php';
  939. $scriptTag = '<script type="text/javascript">' . PHP_EOL
  940. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  941. . ' var djConfig = ' . Zend_Json::encode($djConfigValues) . ';' . PHP_EOL
  942. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  943. . '</script>';
  944. return $scriptTag;
  945. }
  946. /**
  947. * Render dojo script tag
  948. *
  949. * Renders Dojo script tag by utilizing either local path provided or the
  950. * CDN. If any djConfig values were set, they will be serialized and passed
  951. * with that attribute.
  952. *
  953. * @return string
  954. */
  955. protected function _renderDojoScriptTag()
  956. {
  957. if ($this->useCdn()) {
  958. $source = $this->getCdnBase()
  959. . $this->getCdnVersion()
  960. . $this->getCdnDojoPath();
  961. } else {
  962. $source = $this->getLocalPath();
  963. }
  964. $scriptTag = '<script type="text/javascript" src="' . $source . '"></script>';
  965. return $scriptTag;
  966. }
  967. /**
  968. * Render layers (custom builds) as script tags
  969. *
  970. * @return string
  971. */
  972. protected function _renderLayers()
  973. {
  974. $layers = $this->getLayers();
  975. if (empty($layers)) {
  976. return '';
  977. }
  978. $html = array();
  979. foreach ($layers as $path) {
  980. $html[] = sprintf(
  981. '<script type="text/javascript" src="%s"></script>',
  982. htmlentities($path, ENT_QUOTES)
  983. );
  984. }
  985. return implode("\n", $html);
  986. }
  987. /**
  988. * Render dojo module paths and requires
  989. *
  990. * @return string
  991. */
  992. protected function _renderExtras()
  993. {
  994. $js = array();
  995. $modulePaths = $this->getModulePaths();
  996. if (!empty($modulePaths)) {
  997. foreach ($modulePaths as $module => $path) {
  998. $js[] = 'dojo.registerModulePath("' . $this->view->escape($module) . '", "' . $this->view->escape($path) . '");';
  999. }
  1000. }
  1001. $modules = $this->getModules();
  1002. if (!empty($modules)) {
  1003. foreach ($modules as $module) {
  1004. $js[] = 'dojo.require("' . $this->view->escape($module) . '");';
  1005. }
  1006. }
  1007. $onLoadActions = array();
  1008. // Get Zend specific onLoad actions; these will always be first to
  1009. // ensure that dijits are created in the correct order
  1010. foreach ($this->_getZendLoadActions() as $callback) {
  1011. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  1012. }
  1013. // Get all other onLoad actions
  1014. foreach ($this->getOnLoadActions() as $callback) {
  1015. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  1016. }
  1017. $javascript = implode("\n ", $this->getJavascript());
  1018. $content = '';
  1019. if (!empty($js)) {
  1020. $content .= implode("\n ", $js) . "\n";
  1021. }
  1022. if (!empty($onLoadActions)) {
  1023. $content .= implode("\n ", $onLoadActions) . "\n";
  1024. }
  1025. if (!empty($javascript)) {
  1026. $content .= $javascript . "\n";
  1027. }
  1028. if (preg_match('/^\s*$/s', $content)) {
  1029. return '';
  1030. }
  1031. $html = '<script type="text/javascript">' . PHP_EOL
  1032. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  1033. . $content
  1034. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  1035. . PHP_EOL . '</script>';
  1036. return $html;
  1037. }
  1038. /**
  1039. * Add an onLoad action related to ZF dijit creation
  1040. *
  1041. * This method is public, but prefixed with an underscore to indicate that
  1042. * it should not normally be called by userland code. It is pertinent to
  1043. * ensuring that the correct order of operations occurs during dijit
  1044. * creation.
  1045. *
  1046. * @param string $callback
  1047. * @return Zend_Dojo_View_Helper_Dojo_Container
  1048. */
  1049. public function _addZendLoad($callback)
  1050. {
  1051. if (!in_array($callback, $this->_zendLoadActions, true)) {
  1052. $this->_zendLoadActions[] = $callback;
  1053. }
  1054. return $this;
  1055. }
  1056. /**
  1057. * Retrieve all ZF dijit callbacks
  1058. *
  1059. * @return array
  1060. */
  1061. public function _getZendLoadActions()
  1062. {
  1063. return $this->_zendLoadActions;
  1064. }
  1065. }