Skip to content

Commit

Permalink
Remove dependency on t3jquery and handle assets
Browse files Browse the repository at this point in the history
The jQuery library now needs to be provided by the site rather than
being automatically included by t3jquery.

Stylesheet assets can now be included through fluid viewhelpers (similar to vhs assets):

```xml
{namespace s=Subugoe\Find\ViewHelpers}
<s:page.linkCSS file="{CSSPath}"/>
```

Same for script assets:

 ```xml
 <s:page.script name="findJs{idx}" file="EXT:myext/foo/bar.js"/>
 ```

 and

 ```xml
  <s:page.script name="myInlineScript">
    var foo = 'bar';
  </s:page.script>
 ```

Existing templates have been migrated.
  • Loading branch information
cedricziel authored and ipf committed Jan 9, 2017
1 parent f479a54 commit 897a6f6
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 23 deletions.
106 changes: 106 additions & 0 deletions Classes/ViewHelpers/Page/ScriptViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace Subugoe\Find\ViewHelpers\Page;

/*******************************************************************************
* Copyright notice
* Copyright 2013 Sven-S. Porst, Göttingen State and University Library
* <[email protected]>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;

/**
* View Helper to dynamically add script resources to the output.
*
* Usage examples are available in Private/Partials/Test.html.
*/
class ScriptViewHelper extends AbstractViewHelper implements CompilableInterface
{
/**
* @return PageRenderer
*/
protected static function getPageRenderer()
{
return GeneralUtility::makeInstance(PageRenderer::class);
}

/**
* @return TemplateService
*/
protected static function getTypoScriptTemplateService()
{
return $GLOBALS['TSFE']->tmpl;
}

/**
* @return void
*/
public function initializeArguments()
{
$this->registerArgument('file', 'string', 'File to append as script');
$this->registerArgument('name', 'string', 'Name to use', true);
}

/**
* @return string
*/
public function render()
{
return self::renderStatic(
[
'file' => $this->arguments['file'],
'name' => $this->arguments['name'],
],
$this->buildRenderChildrenClosure(),
$this->renderingContext
);
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*
* @return string
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$scriptPath = static::getTypoScriptTemplateService()->getFileName($arguments['file']);
$name = $arguments['name'];

$pageRenderer = self::getPageRenderer();
if ($scriptPath) {
$pageRenderer->addJsFooterLibrary($name, $scriptPath);

return '';
}

$content = $renderChildrenClosure();
$pageRenderer->addJsFooterInlineCode($name, $content);

return '';
}
}
10 changes: 8 additions & 2 deletions Documentation/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,14 @@ Settings include the ``JSPaths`` and ``CSSPaths`` arrays which can be used to co
JSPaths.20 = EXT:find/Projects/test/Resources/test.js
}

jQuery has to be included manually on your TypoScript ``PAGE``-object of choice. Example::

page.includeJSFooterlibs.jquery = https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js
page.includeJSFooterlibs.jquery.external = 1
page.includeJSFooterlibs.jquery.integrity = sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=
page.includeJSFooterlibs.jqueryUi = https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js
page.includeJSFooterlibs.jqueryUi.external = 1
page.includeJSFooterlibs.jqueryUi.integrity = sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=

Localisation
::::::::::::
Expand Down Expand Up @@ -712,8 +720,6 @@ Prerequisites

* TYPO3 6.2 or higher
* PHP 5.5 or higher
* t3jquery Extension


Contact
-------
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Layouts/Default.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="tx_find" id="{settings.jumpToID}">
<f:flashMessages/>
<f:render section="main"/>
</div>
</div>
10 changes: 6 additions & 4 deletions Resources/Private/Partials/Facets/Facet/Histogram.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{namespace s=Subugoe\Find\ViewHelpers}
{namespace t3jquery=Tx_T3jquery_ViewHelpers}

<f:comment>
Uses facet data to draw a histogram of the distribution of the (numeric) facet terms.
Includes the required JavaScript libraries (jQuery.flot) and hooks up the script
functions to enable interaction.
Adds a link to deselect the date range used for faceting if there is a selection.
</f:comment>
<t3jquery:addJQueryAndScript jsfile="EXT:find/Resources/Public/JavaScript/flot/jquery.flot.js"/>
<t3jquery:addJQueryAndScript jsfile="EXT:find/Resources/Public/JavaScript/flot/jquery.flot.selection.js"/>

<s:page.script file="EXT:find/Resources/Public/JavaScript/flot/jquery.flot.js" name="find-flot-selection"/>
<s:page.script file="EXT:find/Resources/Public/JavaScript/flot/jquery.flot.selection.js" name="find-flot-selection"/>

<div class="facetHistogram-container">
<f:if condition="{s:find.facetIsActive(facetID:facetInfo.id, activeFacets:config.activeFacets)}">
<f:for each="{s:data.valueForKey(array:config.activeFacets, key:facetInfo.id)}" as="termInfo" key="term">
Expand Down Expand Up @@ -58,4 +60,4 @@
</script>
</div>
</f:alias>
</div>
</div>
10 changes: 6 additions & 4 deletions Resources/Private/Partials/Facets/Facet/List/Autocomplete.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{namespace s=Subugoe\Find\ViewHelpers}
{namespace t3jquery=Tx_T3jquery_ViewHelpers}

<f:comment>
Uses the facet items to create a popup menu with an autocomplete field
(using jQuery.chosen) to let the user easily find a specific item in the
facet and use it as a filter.
</f:comment>
<s:page.linkCSS file="EXT:find/Resources/Public/JavaScript/jquery.chosen/public/chosen.min.css"/>
<t3jquery:addJQueryAndScript jsfile="EXT:find/Resources/Public/JavaScript/jquery.chosen/public/chosen.jquery.min.js"/>

<s:page.linkCSS file="EXT:find/Resources/Public/JavaScript/jquery.chosen/public/chosen.css"/>
<s:page.script name="chosen" file="EXT:find/Resources/Public/JavaScript/jquery.chosen/public/chosen.jquery.js"/>

<f:alias map="{translateArguments:'{0:\"{f:translate(key:\'LLL:{settings.languageRootPath}locallang-facets.xml:facet.{facetInfo.id}\', default:facetInfo.id)}\"}'}">
<form>
<f:form.select
Expand All @@ -20,4 +22,4 @@
class="facetSearch"
additionalAttributes="{data-placeholder:'{f:translate(key:\"Filter by %s\", arguments:translateArguments)}'}"/>
</form>
</f:alias>
</f:alias>
14 changes: 9 additions & 5 deletions Resources/Private/Partials/Facets/Facet/Map.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{namespace s=Subugoe\Find\ViewHelpers}
{namespace t3jquery=Tx_T3jquery_ViewHelpers}

<f:comment>
Creates a map facet.

Expand All @@ -25,7 +25,7 @@
of facet values that are fetched the more granular the displayed results will be.

Sample TypoScript configuration:

plugin.tx_find.settings.facet {
10 {
id = map
Expand All @@ -36,7 +36,10 @@
}
}
</f:comment>
<t3jquery:addJQueryAndScript jsfile="EXT:find/Resources/Public/JavaScript/js-geohash/geohash.js">

<s:page.script file="EXT:find/Resources/Public/JavaScript/js-geohash/geohash.js" name="find_geohash"/>

<s:page.script name="find_geohash">
tx_find_facetMap.init ({
container: jQuery('.facet-id-{facetInfo.id} .mapContainer')[0],
queryURLTemplate: {s:format.json(data:"{f:uri.action(
Expand All @@ -53,7 +56,8 @@
facetData: {s:format.json(data:facetData.values)},
facetFetchMaximum: {facetInfo.fetchMaximum}
});
</t3jquery:addJQueryAndScript>
</s:page.script>

<div class="facetMap-container">
<div class="mapContainer"></div>
</div>
</div>
4 changes: 3 additions & 1 deletion Resources/Private/Partials/Page/CSS.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{namespace s=Subugoe\Find\ViewHelpers}

<f:comment>
Adds the CSS files configured in the CSSPaths settings to the page’s head.
</f:comment>

<f:for each="{settings.CSSPaths}" as="CSSPath">
<s:page.linkCSS file="{CSSPath}"/>
</f:for>
</f:for>
8 changes: 5 additions & 3 deletions Resources/Private/Partials/Page/JavaScript.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{namespace t3jquery=Tx_T3jquery_ViewHelpers}
{namespace s=Subugoe\Find\ViewHelpers}

<f:comment>
Adds the JavaScript files configured in the CSSPaths settings to the page’s head.
</f:comment>

<f:for each="{settings.JSPaths}" as="JSPath">
<t3jquery:addJQueryAndScript jsfile="{JSPath}"/>
</f:for>
<s:page.script name="findJs{idx}" file="{script}"/>
</f:for>
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'author' => 'Sven-S. Porst, Ingo Pfennigstorf',
'author_email' => '[email protected]',
'author_company' => 'SUB Göttingen',
'dependencies' => 't3jquery',
'dependencies' => '',
'conflicts' => '',
'constraints' => [
'depends' => [
Expand Down
2 changes: 0 additions & 2 deletions t3jquery.txt

This file was deleted.

0 comments on commit 897a6f6

Please sign in to comment.