Complete pagelinks plugin for redactor

This commit is contained in:
Samuel Georges 2015-06-25 19:26:47 +10:00
parent 87e9996193
commit 0da649551b
5 changed files with 138 additions and 14 deletions

View File

@ -1,5 +1,7 @@
<?php namespace Backend\FormWidgets;
use Event;
use Request;
use Backend\Classes\FormWidgetBase;
/**
@ -60,14 +62,49 @@ class RichEditor extends FormWidgetBase
$this->vars['value'] = $this->getLoadValue();
}
/**
* Returns a single collection of available page links.
* This implementation has room to place links under
* different groups based on the link type.
* @return array
*/
public function onGetPageLinks()
{
$links = [
['name' => 'Select a page...', 'url' => false],
['name' => 'Some url', 'url' => 'some/url'],
['name' => 'Other thing', 'url' => 'else/thing'],
['name' => 'More', 'url' => 'more/thing']
];
$links = [];
$types = $this->getPageLinkTypes();
$links[] = ['name' => 'Select a page...', 'url' => false];
$iterator = function($links, $level = 0) use (&$iterator) {
$result = [];
foreach ($links as $linkUrl => $link) {
/*
* Remove scheme and host from URL
*/
$baseUrl = Request::getSchemeAndHttpHost();
if (strpos($linkUrl, $baseUrl) === 0) {
$linkUrl = substr($linkUrl, strlen($baseUrl));
}
$linkName = str_repeat('&nbsp;', $level * 4);
$linkName .= is_array($link) ? array_get($link, 'title', '') : $link;
$result[] = ['name' => $linkName, 'url' => $linkUrl];
if (is_array($link)) {
$result = array_merge(
$result,
$iterator(array_get($link, 'links', []), $level + 1)
);
}
}
return $result;
};
foreach ($types as $typeCode => $typeName) {
$links = array_merge($links, $iterator($this->getPageLinks($typeCode)));
}
return ['links' => $links];
}
@ -80,4 +117,48 @@ class RichEditor extends FormWidgetBase
$this->addCss('css/richeditor.css', 'core');
$this->addJs('js/build-min.js', 'core');
}
/**
* Returns a list of registered page link types.
* This is reserved functionality for separating the links by type.
* @return array Returns an array of registered page link types
*/
protected function getPageLinkTypes()
{
$result = [];
$apiResult = Event::fire('backend.richeditor.listTypes');
if (is_array($apiResult)) {
foreach ($apiResult as $typeList) {
if (!is_array($typeList)) {
continue;
}
foreach ($typeList as $typeCode => $typeName) {
$result[$typeCode] = $typeName;
}
}
}
return $result;
}
protected function getPageLinks($type)
{
$result = [];
$apiResult = Event::fire('backend.richeditor.getTypeInfo', [$type]);
if (is_array($apiResult)) {
foreach ($apiResult as $typeInfo) {
if (!is_array($typeInfo)) {
continue;
}
foreach ($typeInfo as $name => $value) {
$result[$name] = $value;
}
}
}
return $result;
}
}

View File

@ -1939,7 +1939,7 @@ else
{return{init:function()
{if(!this.opts.pageLinksHandler)return
this.modal.addCallback('link',$.proxy(this.pagelinks.load,this))},load:function()
{return;var $select=$('<select id="redactor-page-links" />')
{var $select=$('<select id="redactor-page-links" />')
$('#redactor-modal-link-insert').prepend($select)
this.pagelinks.storage={};this.$editor.request(this.opts.pageLinksHandler,{success:$.proxy(function(data){$.each(data.links,$.proxy(function(key,val){this.pagelinks.storage[key]=val
$select.append($('<option>').val(key).html(val.name))},this))
@ -1950,7 +1950,7 @@ if(key!==0){name=this.pagelinks.storage[key].name
url=this.pagelinks.storage[key].url}
$('#redactor-link-url').val(url)
var $el=$('#redactor-link-url-text')
if($el.val()==='')$el.val(name)}};};})(jQuery);+function($){"use strict";var Base=$.oc.foundation.base,BaseProto=Base.prototype
if($el.val()===''){$el.val($.trim($('<span />').html(name).text()))}}};};})(jQuery);+function($){"use strict";var Base=$.oc.foundation.base,BaseProto=Base.prototype
var RichEditor=function(element,options){this.options=options
this.$el=$(element)
this.$textarea=this.$el.find('>textarea:first')

View File

@ -13,8 +13,6 @@ if (!RedactorPlugins) var RedactorPlugins = {};
},
load: function()
{
return; // Disabled for now
var $select = $('<select id="redactor-page-links" />')
$('#redactor-modal-link-insert').prepend($select)
@ -45,7 +43,9 @@ if (!RedactorPlugins) var RedactorPlugins = {};
$('#redactor-link-url').val(url)
var $el = $('#redactor-link-url-text')
if ($el.val() === '') $el.val(name)
if ($el.val() === '') {
$el.val($.trim($('<span />').html(name).text()))
}
}
};
};

View File

@ -46,6 +46,7 @@ class ServiceProvider extends ModuleServiceProvider
parent::boot('cms');
$this->bootMenuItemEvents();
$this->bootRichEditorEvents();
}
/**
@ -254,4 +255,23 @@ class ServiceProvider extends ModuleServiceProvider
}
});
}
/**
* Registers events for rich editor page links.
*/
protected function bootRichEditorEvents()
{
Event::listen('backend.richeditor.listTypes', function () {
return [
'cms-page' => 'CMS Page'
];
});
Event::listen('backend.richeditor.getTypeInfo', function ($type) {
if ($type == 'cms-page') {
return CmsPage::getRichEditorTypeInfo($type);
}
});
}
}

View File

@ -1,9 +1,9 @@
<?php namespace Cms\Classes;
use Cms\Classes\Theme;
use ApplicationException;
use Cms\Classes\Layout;
use Lang;
use Cms\Classes\Theme;
use Cms\Classes\Layout;
use ApplicationException;
/**
* The CMS page class.
@ -206,4 +206,27 @@ class Page extends CmsCompoundObject
return $result;
}
/**
* Handler for the backend.richeditor.getTypeInfo event.
* Returns a menu item type information. The type information is returned as array
* @param string $type Specifies the page link type
* @return array
*/
public static function getRichEditorTypeInfo($type)
{
$result = [];
if ($type == 'cms-page') {
$theme = Theme::getActiveTheme();
$pages = self::listInTheme($theme, true);
foreach ($pages as $page) {
$url = self::url($page->getBaseFileName());
$result[$url] = $page->title;
}
}
return $result;
}
}