Skip to content

Instantly share code, notes, and snippets.

@Deele Deele/TinyMce.php
Last active Jul 28, 2017

Embed
What would you like to do?
Implemented an option to call widget initialization on demand (for PJAX, AJAX or other purposes).
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\tinymce;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
*
* TinyMCE renders a tinyMCE js plugin for WYSIWYG editing.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
*/
class TinyMce extends InputWidget
{
/**
* @var string the language to use. Defaults to null (en).
*/
public $language;
/**
* @var array the options for the TinyMCE JS plugin.
* Please refer to the TinyMCE JS plugin Web page for possible options.
* @see http://www.tinymce.com/wiki.php/Configuration
*/
public $clientOptions = [];
/**
* @var bool whether to set the on change event for the editor. This is required to be able to validate data.
* @see https://github.com/2amigos/yii2-tinymce-widget/issues/7
*/
public $triggerSaveOnBeforeValidateForm = true;
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
echo Html::textarea($this->name, $this->value, $this->options);
}
$this->registerClientScript();
}
/**
* Registers tinyMCE js plugin
*/
protected function registerClientScript()
{
$js = [];
$view = $this->getView();
TinyMceAsset::register($view);
$id = $this->options['id'];
$callback = 'tinymceInit_' . $id;
$this->clientOptions['selector'] = "#$id";
// @codeCoverageIgnoreStart
if ($this->language !== null && $this->language !== 'en') {
$langFile = "langs/{$this->language}.js";
$langAssetBundle = TinyMceLangAsset::register($view);
$langAssetBundle->js[] = $langFile;
$this->clientOptions['language_url'] = $langAssetBundle->baseUrl . "/{$langFile}";
}
// @codeCoverageIgnoreEnd
$options = Json::encode($this->clientOptions);
$js[] = 'var options = ' . $options . ';';
$js[] = 'tinymce.init(options);';
if ($this->triggerSaveOnBeforeValidateForm) {
$js[] = "$('#{$id}').parents('form').on('beforeValidate', function() { tinymce.triggerSave(); });";
}
$js[] = '$(document).trigger("tinymce:init", ["' . $callback . '", options]);';
$view->registerJs('var ' . $callback . ' = function() { ' . implode("\n", $js) . '}', $view::POS_END);
$view->registerJs($callback . '();');
}
}
@Deele

This comment has been minimized.

Copy link
Owner Author

Deele commented Jul 28, 2017

Your code could reload all tinyMCEs with code like this:

(function() {
var tinymceCallbacks = [];
$(document).on('tinymce:init', function(e, callback, options) {
    console.log('TinyMCE was initialized with callback', callback);
    if (!$.inArray(callback, tinymceCallbacks)) {
        tinymceCallbacks.push(callback);
    }
});
$(document).on('pjax:complete', function() {
    $.each(tinymceCallbacks, function(callback) {
        callback.call(this);
    });
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.