Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refs #4360, BP from #4183) url_for, app_url_for for javascript. #568

Open
wants to merge 9 commits into
base: stable-3.6.x
Choose a base branch
from
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: []
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 <[email protected]>
*/
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);
}
}
21 changes: 21 additions & 0 deletions apps/pc_frontend/templates/_layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@
<?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_javascript('jquery.min.js');
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.';');
?>
<?php include_javascripts() ?>
<?php echo $op_config->get('pc_html_head') ?>
</head>
<body id="<?php printf('page_%s_%s', $view->getModuleName(), $view->getActionName()) ?>" class="<?php echo opToolkit::isSecurePage() ? 'secure_page' : 'insecure_page' ?>">
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 <[email protected]>
*/

/**
* 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 @@ -32,4 +32,21 @@ if(!urlstr)
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=!0;}})}
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);}};
67 changes: 67 additions & 0 deletions web/js/util.js.src
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,70 @@ 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);
}
};