Explorar el Código

ZF-4461: apply patch to generate dojo.require statements for Editor plugins

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18595 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew hace 16 años
padre
commit
dfb4789ec9

+ 38 - 0
library/Zend/Dojo/View/Helper/Editor.php

@@ -48,6 +48,19 @@ class Zend_Dojo_View_Helper_Editor extends Zend_Dojo_View_Helper_Textarea
     protected $_module = 'dijit.Editor';
 
     /**
+     * @var array Maps non-core plugin to module basename
+     */
+    protected $_pluginsModules = array(
+        'createLink' => 'LinkDialog',
+        'insertImage' => 'LinkDialog',
+        'fontName' => 'FontChoice',
+        'fontSize' => 'FontChoice',
+        'formatBlock' => 'FontChoice',
+        'foreColor' => 'TextColor',
+        'hiliteColor' => 'TextColor'
+    );
+
+    /**
      * JSON-encoded parameters
      * @var array
      */
@@ -64,6 +77,12 @@ class Zend_Dojo_View_Helper_Editor extends Zend_Dojo_View_Helper_Textarea
      */
     public function editor($id, $value = null, $params = array(), $attribs = array())
     {
+        if (isset($params['plugins'])) {
+            foreach ($this->_getRequiredModules($params['plugins']) as $module) {
+                $this->dojo->requireModule($module);
+            }
+        }
+
         $hiddenName = $id;
         if (array_key_exists('id', $attribs)) {
             $hiddenId = $attribs['id'];
@@ -93,6 +112,25 @@ class Zend_Dojo_View_Helper_Editor extends Zend_Dojo_View_Helper_Textarea
     }
 
     /**
+     * Generates the list of required modules to include, if any is needed. 
+     *
+     * @param array $plugins plugins to include
+     * @return array
+     */
+    protected function _getRequiredModules(array $plugins)
+    {
+        $modules = array();
+        foreach ($plugins as $commandName) {
+            if (isset($this->_pluginsModules[$commandName])) {
+                $pluginName = $this->_pluginsModules[$commandName];
+                $modules[] = 'dijit._editor.plugins.' . $pluginName;
+            }
+        }
+
+        return array_unique($modules);
+    }
+
+    /**
      * Normalize editor element name
      *
      * @param  string $name

+ 17 - 0
tests/Zend/Dojo/View/Helper/EditorTest.php

@@ -179,6 +179,23 @@ class Zend_Dojo_View_Helper_EditorTest extends PHPUnit_Framework_TestCase
         $this->helper->editor('foo');
         $this->assertFalse($this->view->dojo()->registerDojoStylesheet());
     }
+
+    /**
+     * @group ZF-4461
+     */
+    public function testHelperShouldRegisterPluginModulesWithDojo()
+    {
+        $plugins = array(
+            'createLink' => 'LinkDialog', 
+            'fontName' => 'FontChoice', 
+        );
+        $html = $this->helper->editor('foo', '', array('plugins' => array_keys($plugins)));
+
+        $dojo = $this->view->dojo()->__toString();
+        foreach (array_values($plugins) as $plugin) {
+            $this->assertContains('dojo.require("dijit._editor.plugins.' . $plugin . '")', $dojo, $dojo);
+        }
+    }
 }
 
 // Call Zend_Dojo_View_Helper_EditorTest::main() if this source file is executed directly.