From 1f2ee8faf2f3adba81ffa860a2dafc0a26d9be60 Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Wed, 6 Jun 2012 04:13:19 +0000 Subject: [PATCH] Add ckeditor as htmlarea widget --- ...lass.etemplate_widget_customfields.inc.php | 25 ++++- .../class.etemplate_widget_htmlarea.inc.php | 23 +++++ etemplate/js/et2_extension_customfields.js | 25 ++++- etemplate/js/et2_widget_htmlarea.js | 96 +++++++++++++++++++ etemplate/js/etemplate2.js | 1 + 5 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 etemplate/js/et2_widget_htmlarea.js diff --git a/etemplate/inc/class.etemplate_widget_customfields.inc.php b/etemplate/inc/class.etemplate_widget_customfields.inc.php index b6989a8f29..31bca4fd5d 100644 --- a/etemplate/inc/class.etemplate_widget_customfields.inc.php +++ b/etemplate/inc/class.etemplate_widget_customfields.inc.php @@ -98,6 +98,17 @@ class etemplate_widget_customfields extends etemplate_widget_transformer unset(self::$request->modifications[$form_name]['app']); } + if($this->getElementAttribute($form_name, 'customfields')) + { + $customfields =& $this->getElementAttribute($form_name, 'customfields'); + } + elseif($app) + { + // Checking creates it even if it wasn't there + unset(self::$request->modifications[$form_name]['customfields']); + $customfields =& $this->getElementAttribute(self::GLOBAL_VALS, 'customfields'); + } + if(!$app) { $app =& $this->setElementAttribute(self::GLOBAL_VALS, 'app', $GLOBALS['egw_info']['flags']['currentapp']); @@ -124,10 +135,12 @@ class etemplate_widget_customfields extends etemplate_widget_transformer $field_filter = explode(',', $this->attrs['field_names']); } $fields = $customfields; + foreach((array)$fields as $key => $field) { // remove private or non-private cf's, if only one kind should be displayed - if ((string)$this->attrs['use-private'] !== '' && (boolean)$field['private'] != (boolean)$this->attrs['use-private']) + if ((string)self::expand_name($this->attrs['use-private'],0,0) !== '' && + (boolean)$field['private'] != (boolean)$this->attrs['use-private']) { unset($fields[$key]); } @@ -190,12 +203,22 @@ class etemplate_widget_customfields extends etemplate_widget_transformer parent::beforeSendToClient($cname); // Re-format date custom fields from Y-m-d + $field_settings =& self::get_array(self::$request->modifications, "{$this->id}[customfields]",true); + $field_settings = array(); foreach($fields as $fname => $field) { if($field['type'] == 'date' && self::$request->content[self::$prefix.$fname]) { self::$request->content[self::$prefix.$fname] = strtotime(self::$request->content[self::$prefix.$fname]); } + + // Run beforeSendToClient for each field + $widget = self::factory($field['type'], '<'.$field['type'].' type="'.$field['type'].'"/>', self::$prefix.$fname); + if(method_exists($widget, 'beforeSendToClient')) + { + $widget->id = "customfields[{$fname}]"; + $widget->beforeSendToClient($this->id, $fname); + } } } diff --git a/etemplate/inc/class.etemplate_widget_htmlarea.inc.php b/etemplate/inc/class.etemplate_widget_htmlarea.inc.php index ccbd497fe0..d4821a7e8f 100644 --- a/etemplate/inc/class.etemplate_widget_htmlarea.inc.php +++ b/etemplate/inc/class.etemplate_widget_htmlarea.inc.php @@ -16,6 +16,29 @@ */ class etemplate_widget_htmlarea extends etemplate_widget { + + protected $legacy_options = 'mode,height,width,expand_toolbar,base_href'; + + public $attrs = array( + 'height' => '400px', + ); + + /** + * Fill config options + * + * @param string $cname + */ + public function beforeSendToClient($cname) + { + $form_name = self::form_name($cname, $this->id); + + + $config = egw_ckeditor_config::get_ckeditor_config_array($this->attrs['mode'], $this->attrs['height'], + $this->attrs['expand_toolbar'],$this->attrs['base_href'] + ); + self::$request->modifications[$form_name]['config'] = $config; + } + /** * Validate input * diff --git a/etemplate/js/et2_extension_customfields.js b/etemplate/js/et2_extension_customfields.js index bd195bf186..bad48b3868 100644 --- a/etemplate/js/et2_extension_customfields.js +++ b/etemplate/js/et2_extension_customfields.js @@ -216,7 +216,16 @@ var et2_customfields_list = et2_baseWidget.extend([et2_IDetachedDOM], { var data = this.getArrayMgr("modifications").getEntry(this.id); // Check for global settings var global_data = this.getArrayMgr("modifications").getRoot().getEntry('~custom_fields~', true); - if(global_data) data = jQuery.extend({}, data, global_data); + if(global_data) + { + for(var key in data) + { + if(global_data[key]) + { + data[key] = jQuery.extend(true, {}, data[key], global_data[key]); + } + } + } for(var key in data) { if(typeof data[key] === 'object' && ! _attrs[key]) _attrs[key] = data[key]; @@ -303,7 +312,19 @@ var et2_customfields_list = et2_baseWidget.extend([et2_IDetachedDOM], { attrs.select_options = field.values; return true; }, - + _setup_htmlarea: function(field_name, field, attrs) { + attrs.config = field.config ? field.config : {}; + attrs.config.toolbarStartupExpanded = false; + if(field.len) + { + var options = field.len.split(','); + if(options.length) attrs.config.width = options[0]; + if(options.length > 1) attrs.config.mode = options[1]; + if(options.length > 2) attrs.config.toolbarStartupExpanded = options[2]; + } + attrs.config.height = ((field.rows ? field.rows : 5) *16) +'px'; + return true; + }, _setup_radio: function(field_name, field, attrs) { // No label on the widget itself delete(attrs.label); diff --git a/etemplate/js/et2_widget_htmlarea.js b/etemplate/js/et2_widget_htmlarea.js new file mode 100644 index 0000000000..497b62e467 --- /dev/null +++ b/etemplate/js/et2_widget_htmlarea.js @@ -0,0 +1,96 @@ +/** + * eGroupWare eTemplate2 - JS widget for HTML editing + * + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @package etemplate + * @subpackage api + * @link http://www.egroupware.org + * @author Nathan Gray + * @copyright Nathan Gray 2012 + * @version $Id$ + */ + +"use strict"; + +/*egw:uses + jsapi.jsapi; // Needed for egw_seperateJavaScript + jquery.jquery; + /phpgwapi/js/ckeditor3/ckeditor.js; + /phpgwapi/js/ckeditor3/config.js; + /phpgwapi/js/ckeditor3/adapters/jquery.js; + et2_core_baseWidget; +*/ + +var et2_htmlarea = et2_inputWidget.extend({ + + attributes: { + 'mode': { + 'name': 'Mode', + 'description': 'One of {ascii|simple|extended|advanced}', + 'default': 'simple', + 'type': 'string' + }, + 'height': { + 'name': 'Height', + 'default': et2_no_init, + 'type': 'string' + }, + 'width': { + 'name': 'Width', + 'default': et2_no_init, + 'type': 'string' + }, + 'expand_toolbar': { + 'name': 'Expand Toolbar', + 'default': true, + 'type':'any', + }, + 'base_href': { + 'name': 'Image base path', + 'default': et2_no_init, + 'type': 'string', + 'description': 'activates the browser for images at the path (relative to the docroot)' + }, + 'config': { + // internal default configuration + 'type':'any', + 'default': et2_no_init + }, + }, + + legacyOptions: ['mode','height','width','expand_toolbar','base_href'], + + ck_props: {}, + init: function(_parent, _attrs) { + this.ck_props = _attrs['config'] ? _attrs['config'] : {}; + + this._super.apply(this, arguments); + + // Allow no child widgets + this.supportedWidgetClasses = []; + + + this.htmlNode = $j(document.createElement("div")); + this.setDOMNode(this.htmlNode[0]); + }, + + doLoadingFinished: function() { + this._super.apply(this, arguments); + this.htmlNode.ckeditor(function() {},this.ck_props); + }, + + destroy: function() { + this.htmlNode.ckeditorGet().destroy(true); + }, + set_value: function(_value) { + this.htmlNode.val(_value); + }, + + getValue: function() { + return this.htmlNode.val(); + } +}); + +et2_register_widget(et2_htmlarea, ["htmlarea"]); + + diff --git a/etemplate/js/etemplate2.js b/etemplate/js/etemplate2.js index 1a7b51b67e..1a03b1566d 100644 --- a/etemplate/js/etemplate2.js +++ b/etemplate/js/etemplate2.js @@ -30,6 +30,7 @@ et2_widget_diff; et2_widget_styles; et2_widget_html; + et2_widget_htmlarea; et2_widget_tabs; et2_widget_tree; et2_widget_historylog;