Skip to content

Commit

Permalink
Merge pull request 3liz#4333 from nboisteault/move-inline-js-scripts
Browse files Browse the repository at this point in the history
Move inline JS scripts
  • Loading branch information
nboisteault authored Mar 29, 2024
2 parents 9997c79 + 9bad5e6 commit 6acf1b6
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 47 deletions.
17 changes: 15 additions & 2 deletions assets/src/legacy/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4005,12 +4005,24 @@ window.lizMap = function() {
return deactivateMaplayerFilter(layername);
},



/**
* Method: init
*/
init: function() {
// Initialize global variables
const lizmapVariablesJSON = document.getElementById('lizmap-vars')?.innerText;
if (lizmapVariablesJSON) {
let lizmapVariables;
try {
lizmapVariables = JSON.parse(lizmapVariablesJSON);
for (const variable in lizmapVariables) {
window[variable] = lizmapVariables[variable];
}
} catch {
console.warn('JSON for Lizmap global variables is not valid!');
}
}

var self = this;

// Get config
Expand Down Expand Up @@ -4491,6 +4503,7 @@ $(document).ready(function () {
.children('.ui-dialog-titlebar').removeClass('ui-corner-all');
// configurate OpenLayers
OpenLayers.DOTS_PER_INCH = 96;

// initialize LizMap
lizMap.init();
$( "#loading" ).css('min-height','128px');
Expand Down
12 changes: 9 additions & 3 deletions assets/src/legacy/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@ var searchProjects = function(){
// Hide all keywords
$('#search-project-result .project-keyword').addClass('hide');
var visibleProjectEdges = getEdges();
var selectedKeywords = getSelectedKeywords();
var lastSelectedKeyword = selectedKeywords[selectedKeywords.length - 1];

var hiddenKeywords = $('#search-project-result .project-keyword.hide');

for (var index = 0; index < hiddenKeywords.length; index++) {
for (let index = 0; index < hiddenKeywords.length; index++) {
var hiddenKeyword = hiddenKeywords.eq(index);

for (var i = 0; i < visibleProjectEdges.length; i++) {
Expand Down Expand Up @@ -248,7 +247,7 @@ var searchProjects = function(){
}

var keywordsHTML = '';
for (var index = 0; index < uniqueKeywordList.length; index++) {
for (let index = 0; index < uniqueKeywordList.length; index++) {
keywordsHTML += '<span class="project-keyword hide">' + uniqueKeywordList[index].toLowerCase() + '</span>';
}
$('#search-project-result').html(keywordsHTML);
Expand Down Expand Up @@ -281,4 +280,11 @@ var searchProjects = function(){

window.addEventListener('load', function () {
searchProjects();

// Handle description's modal
$('.liz-project-show-desc').click(function () {
const descId = $(this).data('lizmap-modal');
$('#liz-project-modal-' + descId).modal('show');
return false;
});
});
4 changes: 4 additions & 0 deletions assets/src/modules/ExecuteJSFromServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default function executeJSFromServer() {
if (document.body.dataset.lizmapHideLegend) {
document.querySelector('li.switcher.active #button-switcher').click();
}

if (document.body.dataset.lizmapActionWarningOld) {
lizMap.addMessage(document.body.dataset.lizmapActionWarningOld,'info',true).attr('id','lizmap-action-message');
}
}
});

Expand Down
17 changes: 17 additions & 0 deletions lizmap/app/responses/AbstractLizmapHtmlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,21 @@ protected function prepareHeadContent()

$this->addHeadContent('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />');
}

protected $jsVarData = array();

public function addJsVariable($name, $value)
{
$this->jsVarData[$name] = $value;
}

public function addJsVariables(array $variables)
{
$this->jsVarData = array_merge($this->jsVarData, $variables);
}

protected function doAfterActions()
{
$this->addHeadContent('<script id="lizmap-vars" type="application/json">'.json_encode($this->jsVarData).'</script>');
}
}
2 changes: 2 additions & 0 deletions lizmap/app/responses/adminHtmlResponse.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ protected function doAfterActions()
$this->body->assignIfNone('MAIN', '');
$this->body->assignIfNone('adminTitle', '');
$this->body->assign('user', jAuth::getUserSession());

parent::doAfterActions();
}
}
2 changes: 2 additions & 0 deletions lizmap/app/responses/adminLoginHtmlResponse.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ protected function doAfterActions()
$this->title .= ($this->title != '' ? ' - ' : '').'Administration';
$this->body->assignIfNone('MAIN', '');
$this->body->assignIfNone('page_title', jLocale::get('jcommunity~login.login.title'));

parent::doAfterActions();
}
}
2 changes: 2 additions & 0 deletions lizmap/app/responses/myHtmlMapResponse.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ protected function doAfterActions()
$this->body->assignIfNone('user', jAuth::getUserSession());
$this->body->assignIfNone('auth_url_return', '');
$this->body->assignIfNone('googleAnalyticsID', '');

parent::doAfterActions();
}
}
2 changes: 2 additions & 0 deletions lizmap/app/responses/myHtmlResponse.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ protected function doAfterActions()
$this->body->assignIfNone('auth_url_return', '');
$this->body->assignIfNone('googleAnalyticsID', '');
$this->body->assignIfNone('showHomeLink', true);

parent::doAfterActions();
}
}
2 changes: 2 additions & 0 deletions lizmap/app/responses/simpleHtmlResponse.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ protected function doAfterActions()
// main template, the settings of the response etc..
// $this->bodyTagAttributes = array('onload'=>'init()');
$this->body->assignIfNone('MAIN', '<p>Pas de contenu</p>');

parent::doAfterActions();
}
}
27 changes: 11 additions & 16 deletions lizmap/modules/action/classes/action.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ public function ongetMapAdditions($event)
{
$basePath = jApp::config()->urlengine['basePath'];

// Add JS and CSS for module
$jsCode = array();
// Add JS variables, CSS and body attribute for module
$jsVars = array();
$css = array();
$bodyattr = array();

// Check config
jClasses::inc('action~actionConfig');
Expand All @@ -25,10 +26,9 @@ public function ongetMapAdditions($event)
),
);

$jsCode = array(
'var actionConfig = '.json_encode($actionConfig),
'var actionConfigData = '.json_encode($actionConfigData),
);
$jsVars['actionConfig'] = $actionConfig;
$jsVars['actionConfigData'] = $actionConfigData;

$css = array(
$basePath.'assets/css/action.css',
);
Expand All @@ -40,22 +40,17 @@ public function ongetMapAdditions($event)
if ($serverInfoAccess && $actionConfigInstance->oldConfigConversionDone) {
$url = 'https://docs.lizmap.com/current/en/publish/lizmap_plugin/actions.html';
$message = \jLocale::get('action~action.warning.converted.from.old.configuration',array($url));
$jsCode[] = "
lizMap.events.on(
{
'uicreated':function(evt){
lizMap.addMessage('$message','info',true).attr('id','lizmap-action-message');
}
}
);
";

$bodyattr[] = array('data-lizmap-action-warning-old' => $message);

\jLog::log("$event->repository/$event->project : action module - " . strip_tags($message), 'lizmapadmin');
}

$event->add(
array(
'jscode' => $jsCode,
'jsvars' => $jsVars,
'css' => $css,
'bodyattr' => $bodyattr,
)
);
}
Expand Down
8 changes: 3 additions & 5 deletions lizmap/modules/dataviz/classes/dataviz.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function ongetMapAdditions($event)

// Add JS and CSS for dataviz module
$js = array();
$jscode = array();
$jsVars = array();
$css = array();

// Check dataviz config
Expand All @@ -27,17 +27,15 @@ public function ongetMapAdditions($event)
$datavizConfig = array(
'url' => jUrl::get('dataviz~service:index', array('repository' => $event->repository, 'project' => $event->project)),
);
$jscode = array(
'var datavizConfig = '.json_encode($datavizConfig),
);
$jsVars['datavizConfig'] = $datavizConfig;
$css = array(
$bp.'assets/css/dataviz/dataviz.css',
);
}
$event->add(
array(
'js' => $js,
'jscode' => $jscode,
'jsvars' => $jsVars,
'css' => $css,
)
);
Expand Down
11 changes: 5 additions & 6 deletions lizmap/modules/filter/classes/filter.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function ongetMapAdditions($event)

// Add JS and CSS for module
$js = array();
$jscode = array();
$jsVars = array();
$css = array();

// Check config
Expand All @@ -18,9 +18,8 @@ public function ongetMapAdditions($event)
$filterConfigData = array(
'url' => jUrl::get('filter~service:index', array('repository' => $event->repository, 'project' => $event->project)),
);
$jscode = array(
'var filterConfigData = '.json_encode($filterConfigData).';',
);
$jsVars['filterConfigData'] = $filterConfigData;

// Filter config may be an empty array
// This means no layers have been set up with the filter by form tool
// BUT : we still need to return data so that other tools can use the filter methods
Expand All @@ -36,12 +35,12 @@ public function ongetMapAdditions($event)
);

$filterConfig = $dv->getConfig();
$jscode[] = 'var filterConfig = '.json_encode($filterConfig).';';
$jsVars['filterConfig'] = $filterConfig;
}
$event->add(
array(
'js' => $js,
'jscode' => $jscode,
'jsvars' => $jsVars,
'css' => $css,
)
);
Expand Down
10 changes: 3 additions & 7 deletions lizmap/modules/view/classes/view.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,26 @@ public function onmasteradminGetInfoBoxContent($event)

public function ongetMapAdditions($event)
{
$bp = jApp::config()->urlengine['basePath'];

/*
Use this listener in your modules to make Lizmap
load javascripts and css files in the map
You should pass an array like this
$js = array(
$bp.'js/example/example.js'
);
$jscode = array(
' var somevar = "something"; '
);
$jsVars['somevar'] = "something");
$css = array(
$bp.'css/example/example.css'
);
The listed files must be added by the module in lizmap/www by the installation script
*/
$js = array();
$jscode = array();
$jsVars = array();
$css = array();
$event->add(
array(
'js' => $js,
'jscode' => $jscode,
'jsvars' => $jsVars,
'css' => $css,
)
);
Expand Down
24 changes: 17 additions & 7 deletions lizmap/modules/view/controllers/lizMap.classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function index()
}

// the html response
/** @var jResponseHtml $rep */
/** @var AbstractLizmapHtmlResponse $rep */
$rep = $this->getResponse('htmlmap');
$rep->addJSLink((jUrl::get('view~translate:index')).'?lang='.jApp::config()->locale, array('defer' => ''));

Expand Down Expand Up @@ -230,8 +230,9 @@ function f($x)
$lizUrls['resourceUrlReplacement']['webdav'] = 'dav/';
}

$rep->addJSCode('var lizUrls = '.json_encode($lizUrls).';');
$rep->addJSCode('var lizProj4 = '.json_encode($lproj->getAllProj4()).';');
$rep->addJsVariable('lizUrls', $lizUrls);
$rep->addJsVariable('lizProj4', $lproj->getAllProj4());

$rep->addStyle('#map', 'background-color:'.$lproj->getCanvasColor().';');

// Get the WMS information
Expand Down Expand Up @@ -259,7 +260,7 @@ function f($x)
)
),
);
$rep->addJSCode('var filterConfigData = '.json_encode($filterConfigData));
$rep->addJsVariable('filterConfigData', $filterConfigData);
}

// Add atlas.js for atlas feature and additionnal CSS for right-dock max-width
Expand Down Expand Up @@ -325,16 +326,25 @@ function f($x)
$rep->addJSLink($js, array('defer' => ''));
}
}
if (array_key_exists('jscode', $addition)) {
if (array_key_exists('jsvars', $addition) && is_array($addition['jsvars'])) {
$rep->addJsVariables($addition['jsvars']);
} elseif (array_key_exists('jscode', $addition)) {
foreach ($addition['jscode'] as $jscode) {
$rep->addJSCode($jscode);
}
}

if (array_key_exists('css', $addition)) {
foreach ($addition['css'] as $css) {
$rep->addCssLink($css);
}
}

if (array_key_exists('bodyattr', $addition)) {
foreach ($addition['bodyattr'] as $bodyattr) {
$rep->setBodyAttributes($bodyattr);
}
}
}
}

Expand Down Expand Up @@ -517,7 +527,7 @@ function f($x)
}
}
if (count($filter) > 0) {
$rep->addJSCode('var lizLayerFilter = '.json_encode($filter).';');
$rep->addJsVariable('lizLayerFilter', $filter);
}
}

Expand All @@ -533,7 +543,7 @@ function f($x)
}
}
if (count($styles) > 0) {
$rep->addJSCode('var lizLayerStyles = '.json_encode($styles).';');
$rep->addJsVariable('lizLayerStyles', $styles);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lizmap/modules/view/templates/view.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<h5 class="liz-project-title">{$p->title}</h5>
<p>
<a class="btn liz-project-view" href="{$p->url}{if $hide_header}&h=0{/if}">{@default.project.open.map@}</a>
<a class="btn liz-project-show-desc" href="#link-projet-{$idm}" onclick="$('#liz-project-modal-{$idm}').modal('show'); return false;">{@default.project.open.map.metadata@}</a>
<a class="btn liz-project-show-desc" href="#link-projet-{$idm}" data-lizmap-modal="{$idm}">{@default.project.open.map.metadata@}</a>
</p>
</div>

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:
bootstrapFiles:
- lizmap/vendor/autoload.php
- lizmap/vendor/jelix/jelix/lib/jelix/init.php
- lizmap/app/responses/AbstractLizmapHtmlResponse.php
scanDirectories:
- lizmap/vendor/
level: 4
Expand Down

0 comments on commit 6acf1b6

Please sign in to comment.