Advanced Search WIP: new mechanism for passing the dictionary to the client side. Hopefully faster than before thanks to the browser's cache.

SVN:b1162[5453]
This commit is contained in:
Denis Flaven
2018-03-16 17:44:55 +00:00
parent 52f56e1bb0
commit f8f6e201b9
5 changed files with 135 additions and 32 deletions

View File

@@ -41,6 +41,7 @@ class NiceWebPage extends WebPage
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery-migrate-1.4.1.min.js'); // Needed since many other plugins still rely on oldies like $.browser
$this->add_linked_stylesheet(utils::GetAbsoluteUrlAppRoot().'css/ui-lightness/jquery-ui-1.11.4.custom.css');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery-ui-1.11.4.custom.min.js');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/utils.js');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/hovertip.js');
// table sorting
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.tablesorter.js');

View File

@@ -58,6 +58,7 @@ class WebPage implements Page
protected $s_deferred_content;
protected $a_scripts;
protected $a_dict_entries;
protected $a_dict_entries_prefixes;
protected $a_styles;
protected $a_include_scripts;
protected $a_include_stylesheets;
@@ -80,6 +81,7 @@ class WebPage implements Page
$this->s_deferred_content = '';
$this->a_scripts = array();
$this->a_dict_entries = array();
$this->a_dict_entries_prefixes = array();
$this->a_styles = array();
$this->a_linked_scripts = array();
$this->a_linked_stylesheets = array();
@@ -243,10 +245,39 @@ class WebPage implements Page
/**
* Add a dictionary entry for the Javascript side
*/
public function add_dict_entry($s_entryId)
{
$this->a_dict_entries[$s_entryId] = Dict::S($s_entryId);
}
public function add_dict_entry($s_entryId)
{
$this->a_dict_entries[] = $s_entryId;
}
/**
* Add a set of dictionary entries (based on the given prefix) for the Javascript side
*/
public function add_dict_entries($s_entriesPrefix)
{
$this->a_dict_entries_prefixes[] = $s_entriesPrefix;
}
protected function get_dict_signature()
{
return str_replace('_', '', Dict::GetUserLanguage()).'-'.md5(implode(',', $this->a_dict_entries).'|'.implode(',', $this->a_dict_entries_prefixes));
}
protected function get_dict_file_content()
{
$aEntries = array();
foreach($this->a_dict_entries as $sCode)
{
$aEntries[$sCode] = Dict::S($sCode);
}
foreach($this->a_dict_entries_prefixes as $sPrefix)
{
$aEntries = array_merge($aEntries, Dict::ExportEntries($sPrefix));
}
$sJSFile = 'var aDictEntries = '.json_encode($aEntries);
return $sJSFile;
}
/**
@@ -501,6 +532,9 @@ class WebPage implements Page
echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, shrink-to-fit=no\" />";
echo "<title>".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</title>\n";
echo $this->get_base_tag();
$this->output_dict_entries();
foreach($this->a_linked_scripts as $s_script)
{
// Make sure that the URL to the script contains the application's version number
@@ -524,7 +558,6 @@ class WebPage implements Page
}
echo "</script>\n";
}
$this->output_dict_entries();
foreach($this->a_linked_stylesheets as $a_stylesheet)
{
if (strpos($a_stylesheet['link'], '?') === false)
@@ -778,36 +811,21 @@ class WebPage implements Page
protected function output_dict_entries($bReturnOutput = false)
{
$sHtml = '';
if (count($this->a_dict_entries)>0)
if ((count($this->a_dict_entries) > 0) || (count($this->a_dict_entries_prefixes) > 0))
{
$sHtml .= "<script type=\"text/javascript\">\n";
$sHtml .= "var Dict = {};\n";
$sHtml .= "Dict._entries = {};\n";
$sHtml .= "Dict.S = function(sEntry) {\n";
$sHtml .= " if (sEntry in Dict._entries)\n";
$sHtml .= " {\n";
$sHtml .= " return Dict._entries[sEntry];\n";
$sHtml .= " }\n";
$sHtml .= " else\n";
$sHtml .= " {\n";
$sHtml .= " return sEntry;\n";
$sHtml .= " }\n";
$sHtml .= "};\n";
foreach($this->a_dict_entries as $s_entry => $s_value)
if (class_exists('Dict'))
{
$sHtml .= "Dict._entries['$s_entry'] = '".addslashes($s_value)."';\n";
// The dictionary may not be available for example during the setup...
// Create a specific dictionary file and load it as a JS script
$sSignature = $this->get_dict_signature();
$sJSFileName = utils::GetCachePath().$sSignature.'.js';
if (!file_exists($sJSFileName) && is_writable(utils::GetCachePath()))
{
file_put_contents($sJSFileName, $this->get_dict_file_content());
}
// Load the dictionary as the first javascript file, so that other JS file benefit from the translations
array_unshift($this->a_linked_scripts, utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php/?operation=dict&s='.$sSignature);
}
$sHtml .= "</script>\n";
}
if ($bReturnOutput)
{
return $sHtml;
}
else
{
echo $sHtml;
}
}
}

View File

@@ -357,5 +357,25 @@ class Dict
// No need to actually load the strings since it's only used to know the list of languages
// at setup time !!
}
/**
* Export all the dictionary entries - of the given language - whose code matches the given prefix
* @param string $sStartingWith
* @return string[]
*/
public static function ExportEntries($sStartingWith)
{
self::InitLangIfNeeded(self::GetUserLanguage());
$aEntries = array();
$iLength = strlen($sStartingWith);
foreach(self::$m_aData[self::GetUserLanguage()] as $sCode => $sEntry)
{
if (substr($sCode, 0, $iLength) == $sStartingWith)
{
$aEntries[$sCode] = $sEntry;
}
}
return $aEntries;
}
}
?>

View File

@@ -694,4 +694,59 @@ function DisplayHistory(sSelector, sFilter, iCount, iStart)
$(sSelector).html(data).unblock();
}
);
}
// Very simple equivalent to format: placeholders are %1$s %2$d ...
function Format()
{
var args = [];
var str = '';
if (arguments[0] instanceof Array)
{
str = arguments[0][0].toString();
args = arguments[0];
}
else
{
str = arguments[0].toString();
if (arguments.length > 1)
{
var t = typeof arguments[1];
args = ("string" === t || "number" === t) ? Array.prototype.slice.call(arguments) : arguments[1];
}
}
var key;
for (key in args)
{
str = str.replace(new RegExp("\\%" + key + "\\$.", "gi"), args[key]);
}
return str;
}
var Dict = {};
if (aDictEntries == undefined)
{
Dict._entries = {}; // Entries have not been loaded (we are in the setup ?)
}
else
{
Dict._entries = aDictEntries; // Entries were loaded asynchronously via their own js files
}
Dict.S = function(sEntry)
{
if (sEntry in Dict._entries)
{
return Dict._entries[sEntry];
}
else
{
return sEntry;
}
};
Dict.Format = function()
{
var args = Array.from(arguments);
args[0] = Dict.S(arguments[0]);
return Format(args);
}

View File

@@ -2528,6 +2528,15 @@ EOF
$oPage->add(json_encode($aResult));
break;
case 'dict':
$sSignature = Utils::ReadParam('s', ''); // Sanitization prevents / and ..
$oPage = new ajax_page(""); // New page to cleanup the no_cache done above
$oPage->SetContentType('text/javascript');
$oPage->add_header('Cache-control: public, max-age=86400'); // Cache for 24 hours
$oPage->add_header("Pragma: cache"); // Reset the value set .... where ?
$oPage->add(file_get_contents(Utils::GetCachePath().$sSignature.'.js'));
break;
default:
$oPage->p("Invalid query.");
}