Fixed the unknown component caching problem. Added the unknown component handling to the back-end. AJAX request can now be stopped with preventing default action in the oc.beforeRequest event. Fixes #46.

This commit is contained in:
alekseybobkov 2014-06-26 17:09:25 +11:00
parent aee0777032
commit 5488429fe4
10 changed files with 116 additions and 22 deletions

View File

@ -118,6 +118,15 @@ div.control-componentlist.has-components {
div.control-componentlist div.layout {
width: auto;
}
div.control-componentlist div.components div.layout-cell.error-component {
background: #ab2a1c;
}
div.control-componentlist div.components div.layout-cell.error-component > div {
color: #ffffff;
}
div.control-componentlist div.components div.layout-cell.error-component > div:after {
color: #ab2a1c;
}
div.control-componentlist div.components div.layout-cell:first-child {
border-bottom-left-radius: 3px;
border-top-left-radius: 3px;

View File

@ -87,6 +87,18 @@
setPageTitle('')
})
/*
* Listen for the onBeforeRequest event
*/
$('#cms-master-tabs').on('oc.beforeRequest', function(event) {
var $form = $(event.target)
if ( $('.components .layout-cell.error-component', $form).length > 0) {
if (!confirm('The form contains unknown components. Their properties will be lost on save. Do you want to save the form?'))
event.preventDefault()
}
})
/*
* Listen for the tabs "shown" event to track the current template in the list
*/

View File

@ -11,6 +11,8 @@
@color-component-hover-text: #ffffff;
@color-component-placeholder: #e0e0e0;
@color-group-bg: #f1f3f4;
@color-error-component-bg: #ab2a1c;
@color-error-component-text: #ffffff;
.component-lego-icon() {
position: absolute;
@ -117,6 +119,18 @@ div.control-componentlist {
div.components {
div.layout-cell {
&.error-component {
background: @color-error-component-bg;
> div {
color: @color-error-component-text;
&:after {
color: @color-error-component-bg;
}
}
}
&:first-child {
.border-left-radius(3px);
}

View File

@ -133,8 +133,8 @@ class CmsCompoundObject extends CmsObject
$settingParts = explode(' ', $setting);
$settingName = $settingParts[0];
if (!$manager->hasComponent($settingName))
continue;
// if (!$manager->hasComponent($settingName))
// continue;
$components[$setting] = $value;
unset($this->settings[$setting]);

View File

@ -39,6 +39,18 @@ abstract class ComponentBase extends Extendable
*/
public $pluginIcon;
/**
* @var string Component CSS class name for the back-end page/layout component list.
* This field is used by the CMS internally.
*/
public $componentCssClass;
/**
* @var boolean Determines whether Inspector can be used with the component.
* This field is used by the CMS internally.
*/
public $inspectorEnabled = true;
/**
* @var string Specifies the component directory name.
*/

View File

@ -3,6 +3,7 @@
use Str;
use Illuminate\Container\Container;
use System\Classes\PluginManager;
use System\Classes\SystemException;
/**
* Component manager
@ -179,10 +180,10 @@ class ComponentManager
{
$className = $this->resolve($name);
if (!$className)
return null;
throw new SystemException(sprintf('Class name is not registered for the component %s. Check the component plugin.', $name));
if (!class_exists($className))
throw new \Exception('Component class not found '.$className);
throw new SystemException(sprintf('Component class not found %s.Check the component plugin.', $className));
$component = new $className($cmsObject, $properties);
$component->name = $name;

View File

@ -0,0 +1,29 @@
<?php namespace Cms\Classes;
use Cms\Classes\ComponentBase;
use Cms\Classes\CodeBase;
class UnknownComponent extends ComponentBase
{
protected $errorMessage;
/**
* {@inheritDoc}
*/
public function __construct($cmsObject, $properties, $errorMessage)
{
$this->errorMessage = $errorMessage;
$this->componentCssClass = 'error-component';
$this->inspectorEnabled = false;
parent::__construct($cmsObject, $properties);
}
public function componentDetails()
{
return [
'name' => 'Uknown component',
'description' => $this->errorMessage
];
}
}

View File

@ -3,7 +3,9 @@
use Backend\Classes\FormWidgetBase;
use Cms\Classes\ComponentManager;
use Cms\Classes\ComponentHelpers;
use Cms\Classes\UnknownComponent;
use Lang;
use Exception;
/**
* Component Builder
@ -36,15 +38,22 @@ class Components extends FormWidgetBase
foreach ($this->model->settings['components'] as $name=>$properties) {
list($name, $alias) = strpos($name, ' ') ? explode(' ', $name) : [$name, $name];
$componentObj = $manager->makeComponent($name, null, $properties);
$componentObj->alias = $alias;
$componentObj->pluginIcon = 'icon-puzzle-piece';
try {
$componentObj = $manager->makeComponent($name, null, $properties);
$plugin = $manager->findComponentPlugin($componentObj);
if ($plugin) {
$pluginDetails = $plugin->pluginDetails();
if (isset($pluginDetails['icon']))
$componentObj->pluginIcon = $pluginDetails['icon'];
$componentObj->alias = $alias;
$componentObj->pluginIcon = 'icon-puzzle-piece';
$plugin = $manager->findComponentPlugin($componentObj);
if ($plugin) {
$pluginDetails = $plugin->pluginDetails();
if (isset($pluginDetails['icon']))
$componentObj->pluginIcon = $pluginDetails['icon'];
}
} catch (Exception $ex) {
$componentObj = new UnknownComponent(null, $properties, $ex->getMessage());
$componentObj->alias = $alias;
$componentObj->pluginIcon = 'icon-bug';
}
$result[] = $componentObj;

View File

@ -2,15 +2,21 @@
<div class="components" data-control="toolbar">
<div class="layout">
<?php foreach ($components as $component): ?>
<div class="layout-cell">
<div class="<?= 'oc-'.$component->pluginIcon ?> layout-relative" data-inspectable data-inspector-title="<?= $name = e($this->getComponentName($component)) ?>" data-inspector-description="<?= $description = e($this->getComponentDescription($component)) ?>" data-inspector-config="<?= e($this->getComponentsPropertyConfig($component)) ?>" data-inspector-class="<?= get_class($component) ?>">
<span class="name"><?= $name ?></span>
<span class="description"><?= $description ?></span>
<span class="alias oc-icon-code"><?= e($component->alias) ?></span>
<input type="hidden" name="component_properties[]" data-inspector-values value="<?= e($this->getComponentPropertyValues($component)) ?>"/>
<input type="hidden" name="component_names[]" value="<?= e($component->name) ?>"></input>
<input type="hidden" name="component_aliases[]" value="<?= e($component->alias) ?>"></input>
<a href="#" class="remove">&times;</a>
<div class="layout-cell <?= e($component->componentCssClass) ?>">
<div
class="<?= 'oc-'.$component->pluginIcon ?> layout-relative"
<?php if ($component->inspectorEnabled): ?>data-inspectable<?php endif ?>
data-inspector-title="<?= $name = e($this->getComponentName($component)) ?>"
data-inspector-description="<?= $description = e($this->getComponentDescription($component)) ?>"
data-inspector-config="<?= e($this->getComponentsPropertyConfig($component)) ?>"
data-inspector-class="<?= get_class($component) ?>">
<span class="name"><?= $name ?></span>
<span class="description"><?= $description ?></span>
<span class="alias oc-icon-code"><?= e($component->alias) ?></span>
<input type="hidden" name="component_properties[]" data-inspector-values value="<?= e($this->getComponentPropertyValues($component)) ?>"/>
<input type="hidden" name="component_names[]" value="<?= e($component->name) ?>"></input>
<input type="hidden" name="component_aliases[]" value="<?= e($component->alias) ?>"></input>
<a href="#" class="remove">&times;</a>
</div>
</div>
<?php endforeach ?>

View File

@ -45,7 +45,9 @@ if (window.jQuery === undefined)
loading = options.loading !== undefined && options.loading.length ? $(options.loading) : null,
isRedirect = options.redirect !== undefined && options.redirect.length
form.trigger('oc.beforeRequest', context)
var _event = jQuery.Event('oc.beforeRequest')
form.trigger(_event, context)
if (_event.isDefaultPrevented()) return
var data = [form.serialize()]