Skip to content

Commit

Permalink
Merge pull request #569 from nishizoe/t-4361
Browse files Browse the repository at this point in the history
(refs #4361, BP from #4183) url_for, app_url_for for javascript.
  • Loading branch information
nishizoe authored Jun 10, 2020
2 parents d5bea92 + ff0d7da commit dbe811f
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 17 deletions.
4 changes: 4 additions & 0 deletions apps/pc_frontend/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ global_user_agreement:
url: /userAgreement
param: { module: default, action: userAgreement }

url_for:
url: /default/urlFor.txt
param: { module: default, action: urlFor }

# member rules
member_config_image:
url: /member/image/config
Expand Down
2 changes: 1 addition & 1 deletion apps/pc_frontend/config/view.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ default:

stylesheets: []

javascripts: ['util.js']
javascripts: ['util.js', 'op_url.js']

has_layout: true
layout: layoutC
62 changes: 62 additions & 0 deletions apps/pc_frontend/modules/default/actions/urlForAction.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/

/**
* default actions.
*
* @package OpenPNE
* @subpackage default
* @author Shinichi Urabe <urabe@tejimaya.com>
*/
class urlForAction extends sfAction
{
/**
* Executes urlFor action
*
* @param opWebRequest $request A request object
*/
public function execute($request)
{
// for javascript.
$this->forward404Unless($request->isXmlHttpRequest());
$this->getResponse()->setContentType('text/plain');
$this->getResponse()->setHttpHeader('X-Content-Type-Options', 'nosniff', true);

$this->getContext()->getConfiguration()->loadHelpers(array('Url', 'opUtil'));

$application = $request['application'];
$params = (array)$request['params'];
$function = 'url_for';

if ($application)
{
$params = array_merge(array($application), $params);
$function = 'app_url_for';
}

try
{
return $this->renderText(call_user_func_array($function, $params));
}
catch (Exception $e)
{
$this->logMessage($e->getMessage(), 'err');
}

return $this->set422Status('Can not find routing.');
}

protected function set422Status($message)
{
$this->getResponse()->setStatusCode(422, 'Unprocessable Entity');

return $this->renderText($message);
}
}
29 changes: 14 additions & 15 deletions apps/pc_frontend/templates/_layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
<?php if (Doctrine::getTable('SnsConfig')->get('customizing_css')): ?>
<link rel="stylesheet" type="text/css" href="<?php echo url_for('@customizing_css') ?>" />
<?php endif; ?>
<?php use_helper('Javascript'); ?>
<?php if (opConfig::get('enable_jsonapi') && opToolkit::isSecurePage()): ?>
<?php

use_javascript('jquery.min.js');
use_javascript('jquery.tmpl.min.js');
use_javascript('jquery.notify.js');
use_javascript('op_notify.js');
$jsonData = array(
'apiKey' => $sf_user->getMemberApiKey(),
'apiBase' => app_url_for('api', 'homepage'),
'baseUrl' => $sf_request->getRelativeUrlRoot().'/',
);
use_helper('Javascript');
$jsonData = array('urlForUrl' => url_for('@url_for'));
if (opConfig::get('enable_jsonapi') && opToolkit::isSecurePage())
{
use_javascript('jquery.tmpl.min.js');
use_javascript('jquery.notify.js');
use_javascript('op_notify.js');
$jsonData = array_merge(array(
'apiKey' => $sf_user->getMemberApiKey(),
'apiBase' => app_url_for('api', 'homepage'),
'baseUrl' => $sf_request->getRelativeUrlRoot().'/',
), $jsonData);
}

$json = defined('JSON_PRETTY_PRINT') ? json_encode($jsonData, JSON_PRETTY_PRINT) : json_encode($jsonData);

echo javascript_tag('
var openpne = '.$json.';
');
echo javascript_tag('var openpne = '.$json.';');
?>
<?php endif ?>
<?php include_javascripts() ?>
<?php echo javascript_tag(<<<JS
document.addEventListener('DOMContentLoaded', function() {
Expand Down
84 changes: 84 additions & 0 deletions web/js/op_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/

/**
* OpenPNE url JavaScript helper library
*
* @author Shinichi Urabe <urabe@tejimaya.com>
*/

/**
* opUrl class
*/
var opUrl = {

/**
* method app_url_for().
*
* @param {strina} application ex: api ...
* @param {string} internalUri 'module/action' or '@rule' of the action
* @param {boolean} absolute return absolute path?
* @returns {Deferred} Return a Deferred's Promise object. opUrl.url_for(... snip ...).done(function(url) {... snip ...}).fail(function(xhr, textStatus, errorThrown) {... snip ...})
*/
app_url_for: function(application, internalUri, absolute)
{
return this.call(application, internalUri, absolute);
},

/**
* method url_for().
*
* @param {string} internalUri 'module/action' or '@rule' of the action
* @param {boolean} absolute return absolute path?
* @returns {Deferred} Return a Deferred's Promise object. opUrl.url_for(... snip ...).done(function(url) {... snip ...}).fail(function(xhr, textStatus, errorThrown) {... snip ...})
*/
url_for: function(internalUri, absolute)
{
return this.call('', internalUri, absolute);
},

call: function(application, internalUri, absolute)
{
var key = this.generate_cache_key(application, internalUri, absolute);
var result = opLocalStorage.get(key);
if (typeof result === 'string')
{
var deferred = $.Deferred();
deferred.resolve(result);

return deferred.promise();
}

return this.request(application, internalUri, absolute).done(function(data) {
opLocalStorage.set(key, data);
});
},

request: function(application, internalUri, absolute)
{
var deferred = $.Deferred();

$.ajax({
type: 'POST',
url: openpne.urlForUrl,
data: { application: application, params: [internalUri, Number(absolute)] },
dataType: 'text',
success: deferred.resolve,
error: function(xhr, textStatus, errorThrown) {
deferred.reject(xhr, textStatus, errorThrown);
}
});

return deferred.promise();
},

generate_cache_key: function(application, internalUri, absolute)
{
return 'opUrlKey:' + encodeURIComponent(application) + '&' + encodeURIComponent(internalUri) + '&' + encodeURIComponent(String(absolute));
}
};
19 changes: 18 additions & 1 deletion web/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,24 @@ document.write('<a href="'+url+'" target="_blank">'+urlstr+'</a>');}
function preventDoubleSubmission(form)
{var submitted=false;form.addEventListener('submit',function(ev){if(submitted){ev.preventDefault();return;}
submitted=true;var submitButtons=form.querySelectorAll('input[type="submit"],button[type="submit"]');for(var i=0;i<submitButtons.length;i++){submitButtons[i].disabled=true;}});}
var opLocalStorage={isEnabledVar:null,isEnabled:function()
{if(typeof this.isEnabledVar==='boolean')
{return this.isEnabledVar;}
try
{if(typeof window.localStorage==='undefined')
{return this.isEnabledVar=false;}
else if(window.localStrage)
{var testString='opTest';localStorage.setItem(testString,testString);localStorage.removeItem(testString);}}
catch(e)
{return this.isEnabledVar=false;}
return this.isEnabledVar=true;},set:function(name,value)
{if(!opLocalStorage.isEnabled())
{return false;}
localStorage.setItem(name,value);},get:function(name)
{if(!opLocalStorage.isEnabled())
{return false;}
return localStorage.getItem(name);}};
var smtSwitch={key:'disable_smt',datePeriod:30,elem:null,initialize:function(){this.updateExpires(false);this.elem=document.getElementById('smt-switch');if(this.elem)
{this.elem.addEventListener('click',function(){smtSwitch.switchPc();});}
var $toSmt=document.getElementById('SmtSwitchLink');if($toSmt)
{$toSmt.addEventListener('click',function(){smtSwitch.switchSmt();},false);}},isSwitchPc:function(){return'1'===opCookie.get(this.key);},switchPc:function(){smtSwitch.updateExpires(true);location.href=smtSwitch.elem.getAttribute('href');},switchSmt:function(){opCookie.set(this.key);location.reload();},getExpiresDate:function(){var expiresDate=new Date();expiresDate.setTime(expiresDate.getTime()+this.datePeriod*24*60*60*1000);return expiresDate;},updateExpires:function(force){if(force||this.isSwitchPc()){opCookie.set(this.key,'1',this.getExpiresDate(),openpne.baseUrl);}}};
{$toSmt.addEventListener('click',function(){smtSwitch.switchSmt();},false);}},isSwitchPc:function(){return'1'===opCookie.get(this.key);},switchPc:function(){smtSwitch.updateExpires(true);location.href=smtSwitch.elem.getAttribute('href');},switchSmt:function(){opCookie.set(this.key);location.reload();},getExpiresDate:function(){var expiresDate=new Date();expiresDate.setTime(expiresDate.getTime()+this.datePeriod*24*60*60*1000);return expiresDate;},updateExpires:function(force){if(force||this.isSwitchPc()){opCookie.set(this.key,'1',this.getExpiresDate(),openpne.baseUrl);}}};
67 changes: 67 additions & 0 deletions web/js/util.js.src
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,73 @@ function preventDoubleSubmission(form)
});
}

/**
* opLocalStrage class
*/
var opLocalStorage = {

isEnabledVar: null,

isEnabled: function()
{
if (typeof this.isEnabledVar === 'boolean')
{
return this.isEnabledVar;
}

try
{
if (typeof window.localStorage === 'undefined')
{
return this.isEnabledVar = false;
}
else if (window.localStrage)
{
var testString = 'opTest';
localStorage.setItem(testString, testString);
localStorage.removeItem(testString);
}
}
catch(e)
{
return this.isEnabledVar = false;
}

return this.isEnabledVar = true;
},

/**
* Sets a localStrage data
*
* @params string name
* @params string value
*/
set: function(name, value)
{
if (!opLocalStorage.isEnabled())
{
return false;
}

localStorage.setItem(name, value);
},

/**
* Gets a localStrage data
*
* @params string name
*/
get: function(name)
{
if (!opLocalStorage.isEnabled())
{
return false;
}

return localStorage.getItem(name);
}
};

var smtSwitch = {

key: 'disable_smt',
Expand Down

0 comments on commit dbe811f

Please sign in to comment.