Skip to content

Commit

Permalink
Refacotring wysiwyg config mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Jun 7, 2013
1 parent d527c27 commit 3b89403
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 90 deletions.
5 changes: 0 additions & 5 deletions framework/applications/noviusos_page/static/config/form.js
Expand Up @@ -94,11 +94,6 @@ define(
$wysiwyg.append(bloc);
// The bottom row from TinyMCE is roughly 21px
$wysiwyg.find('[name="wysiwyg[' + i + ']"]').wysiwyg($.extend({}, wysiwyg_options, {
urlEnhancers : true,
container: {
model: 'Nos\\Page\\Model_Page',
id: $page_id.val()
},
height: (coords[3] / data.rows * ratio) - 21,
content_css: data.content_css || ''
}));
Expand Down
Expand Up @@ -8,17 +8,13 @@
* @link http://www.novius-os.org
*/

\Config::load('wysiwyg', true);
empty($options) and $options = \Config::get('wysiwyg.default_setup', 'default');
is_string($options) and $options = \Config::get('wysiwyg.setups.'.$options, array());
$wysiwyg_options = array_merge(\Config::get('wysiwyg.default', array()), $options);

$item->event('wysiwygOptions', array(&$wysiwyg_options));
$wysiwyg_options = \Nos\Tools_Wysiwyg::jsOptions(null, $fieldset->getInstance(), true);
?>
<script type="text/javascript">
require(['jquery-nos', 'static/apps/noviusos_page/config/form.js'], function ($, callback_fn) {
$(function () {
callback_fn.call($('#<?= $fieldset->form()->get_attribute('id') ?>'), <?= \Format::forge($wysiwyg_options)->to_json() ?>);
callback_fn.call($('#<?= $fieldset->form()->get_attribute('id') ?>'),
<?= \Format::forge($wysiwyg_options)->to_json() ?>);
});
});
</script>
27 changes: 0 additions & 27 deletions framework/classes/controller/admin/wysiwyg.ctrl.php
Expand Up @@ -26,31 +26,4 @@ public function action_link($edit = false)

return $view;
}

public function action_enhancers()
{
$urlEnhancers = \Input::get('urlEnhancers', false);
$container = \Input::get('container', array());

$enhancers = \Nos\Config_Data::get('enhancers', array());

if (!$urlEnhancers) {
$enhancers = array_filter($enhancers, function($enhancer) {
return empty($enhancer['urlEnhancer']);
});
}

foreach ($enhancers as $key => $enhancer) {
if (empty($enhancer['iconUrl']) && !empty($enhancer['application'])) {
$enhancers[$key]['iconUrl'] = \Config::icon($enhancer['application'], 16);
}
if (!empty($enhancer['check_container']) && is_callable($enhancer['check_container']) &&
!call_user_func($enhancer['check_container'], $enhancer, $container)) {

unset($enhancers[$key]);
}
}

\Response::json($enhancers);
}
}
24 changes: 3 additions & 21 deletions framework/classes/renderer/wysiwyg.php
Expand Up @@ -12,14 +12,7 @@

class Renderer_Wysiwyg extends \Fieldset_Field
{
protected static $DEFAULT_RENDERER_OPTIONS = array();

public static function _init()
{
\Config::load('wysiwyg', true);
static::$DEFAULT_RENDERER_OPTIONS = \Config::get('wysiwyg.default');
parent::_init();
}
protected static $DEFAULT_RENDERER_OPTIONS = 'default';

public function __construct($name, $label = '', array $renderer = array(), array $rules = array(), \Fuel\Core\Fieldset $fieldset = null)
{
Expand Down Expand Up @@ -63,15 +56,7 @@ public function build()
parent::build();
$this->fieldset()->append(static::js_init($this->get_attribute('id')));

$item = $this->fieldset()->getInstance();
$model = get_class($item);
$pk = \Arr::get($model::primary_key(), 0);
$this->options['container'] = array(
'model' => $model,
'id' => $item->{$pk},
);

$item->event('wysiwygOptions', array(&$this->options));
$this->options = Tools_Wysiwyg::jsOptions($this->options, $this->fieldset()->getInstance());

$this->value = Tools_Wysiwyg::prepare_renderer($this->value);
$this->set_attribute('data-wysiwyg-options', htmlspecialchars(\Format::forge()->to_json($this->options)));
Expand All @@ -95,11 +80,8 @@ protected static function parse_options($renderer = array())
$renderer['id'] = uniqid('wysiwyg_');
}

empty($options) and $options = \Config::get('wysiwyg.default_setup', 'default');
is_string($options) and $options = \Config::get('wysiwyg.setups.'.$options, array());

// Default options of the renderer
$renderer_options = array_merge(static::$DEFAULT_RENDERER_OPTIONS, $options);
$renderer_options = Tools_Wysiwyg::jsOptions(static::$DEFAULT_RENDERER_OPTIONS);

if (!empty($renderer['renderer_options'])) {
$renderer_options = \Arr::merge($renderer_options, $renderer['renderer_options']);
Expand Down
58 changes: 58 additions & 0 deletions framework/classes/tools/wysiwyg.php
Expand Up @@ -12,6 +12,14 @@

class Tools_Wysiwyg
{
protected static $_options = array();

public static function _init()
{
\Config::load('wysiwyg', true);
static::$_options = \Config::get('wysiwyg.default');
}

public static function prepare_renderer($content)
{
$replaces = array();
Expand Down Expand Up @@ -57,4 +65,54 @@ public static function parse_medias(&$content, $closure)
}
}
}

/**
* Return an array of options for the initialisation of wysiwyg
*
* @param mixed $options Can be a string (the name of the default setup) or an array of options to merge with.
* @param Orm\Model $item Model instance of the container of the wysiwyg
* @param bool $urlEnhancers If true, the wysiwyg will accept URL enhancers in applications selector.
* @return array Options for wysiwyg
*/
public static function jsOptions($options = null, Orm\Model $item = null, $urlEnhancers = false)
{
empty($options) and $options = \Config::get('wysiwyg.default_setup', 'default');
is_string($options) and $options = \Config::get('wysiwyg.setups.'.$options, array());

$options = array_merge(static::$_options, $options);

if (!empty($item)) {
$model = get_class($item);
$pk = \Arr::get($model::primary_key(), 0);
$options['container'] = array(
'model' => $model,
'id' => $item->{$pk},
);

$enhancers = Config_Data::get('enhancers', array());

if (!$urlEnhancers) {
$enhancers = array_filter($enhancers, function($enhancer) {
return empty($enhancer['urlEnhancer']);
});
}

foreach ($enhancers as $key => $enhancer) {
if (empty($enhancer['iconUrl']) && !empty($enhancer['application'])) {
$enhancers[$key]['iconUrl'] = \Config::icon($enhancer['application'], 16);
}
if (!empty($enhancer['check_container']) && is_callable($enhancer['check_container']) &&
!call_user_func($enhancer['check_container'], $enhancer, $item)) {

unset($enhancers[$key]);
}
}

$options['theme_nos_enhancers'] = $enhancers;

$item->event('wysiwygOptions', array(&$options));
}

return $options;
}
}
7 changes: 6 additions & 1 deletion framework/config/wysiwyg.config.php
Expand Up @@ -10,7 +10,12 @@

return array(
'default' => array(
'language' => '',
// IE 10 bugfix to make div:hover working in CSS (to show enhancer actions)
'doctype' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
'theme' => 'nos',
'skin' => 'bootstrap',
'plugins' => 'spellchecker,xhtmlxtras,style,table,advlist,inlinepopups,media,searchreplace,paste,noneditable,visualchars,nonbreaking',
'paste_text_use_dialog' => true,
),

'default_setup' => 'default',
Expand Down
2 changes: 1 addition & 1 deletion static/admin/bundle/nos.min.js

Large diffs are not rendered by default.

35 changes: 7 additions & 28 deletions static/admin/novius-os/js/jquery.novius-os.wysiwyg.js
Expand Up @@ -16,37 +16,16 @@ define('jquery-nos-wysiwyg',
function(module, $) {
$.fn.wysiwyg = function(options) {
var self = $(this);

var base_url = $('base').attr('href');
options = $.extend({
urlEnhancers: null,
container: null
// Location of TinyMCE script
document_base_url : base_url,
language : $.nosLang.substr(0, 2),
script_url : base_url + 'static/novius-os/admin/vendor/tinymce/tiny_mce_jquery' + (module.config().minified ? '' : '_src') + '.js'
}, options || {});
$.ajax({
dataType: 'json',
url: 'admin/nos/wysiwyg/enhancers',
data : {
urlEnhancers: options.urlEnhancers,
container: options.container
},
success: function(enhancers) {
var base_url = $('base').attr('href');
options = $.extend({
// Location of TinyMCE script
document_base_url : base_url,
language : $.nosLang.substr(0, 2),
script_url : base_url + 'static/novius-os/admin/vendor/tinymce/tiny_mce_jquery' + (module.config().minified ? '' : '_src') + '.js',
// IE 10 bugfix to make div:hover working in CSS (to show enhancer actions)
doctype : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
theme : 'nos',
skin : "bootstrap",
plugins : 'spellchecker,xhtmlxtras,style,table,advlist,inlinepopups,media,searchreplace,paste,noneditable,visualchars,nonbreaking',
paste_text_use_dialog : true,
theme_nos_enhancers : enhancers
}, options || {});

$(self).tinymce(options);

}
});
$(self).tinymce(options);
};
return $;
});

0 comments on commit 3b89403

Please sign in to comment.