1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 09:17:59 +01:00

Moved the template class to its own file, along with some related functions from util.js

This commit is contained in:
Kris Maglione
2008-10-07 06:56:58 +00:00
parent d337ab8c53
commit abd879a3e5
7 changed files with 248 additions and 242 deletions

View File

@@ -34,10 +34,10 @@ liberator.Buffer = function () //{{{
////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
const highlightClasses = ["Boolean", "ErrorMsg", "InfoMsg", "Keyword", "LineNr",
"ModeMsg", "MoreMsg", "Normal", "Null", "Number", "Question", "StatusLine",
"StatusLineBroken", "StatusLineSecure", "String", "Tag", "Title", "URL",
"WarningMsg"];
const highlightClasses = ["Boolean", "ErrorMsg", "Function", "InfoMsg", "Keyword",
"LineNr", "ModeMsg", "MoreMsg", "Normal", "Null", "Number", "Question",
"StatusLine", "StatusLineBroken", "StatusLineSecure", "String", "Tag",
"Title", "URL", "WarningMsg"];
let name = liberator.config.name.toLowerCase();
const highlightDocs = "chrome://" + name + "/content/buffer.xhtml,chrome://browser/content/browser.xul";
@@ -927,7 +927,7 @@ liberator.Buffer = function () //{{{
nFeed++;
var type = feedTypes[feed.type] || feedTypes["application/rss+xml"];
if (verbose)
yield [feed.title, liberator.util.highlightURL(feed.href, true) + <span class="extra-info"> ({type})</span>];
yield [feed.title, liberator.template.highlightURL(feed.href, true) + <span class="extra-info"> ({type})</span>];
}
}
}
@@ -982,11 +982,11 @@ liberator.Buffer = function () //{{{
}
yield ["Title", doc.title];
yield ["URL", liberator.util.highlightURL(doc.location.toString(), true)];
yield ["URL", liberator.template.highlightURL(doc.location.toString(), true)];
var ref = "referrer" in doc && doc.referrer;
if (ref)
yield ["Referrer", liberator.util.highlightURL(ref, true)];
yield ["Referrer", liberator.template.highlightURL(ref, true)];
if (pageSize[0])
yield ["File Size", pageSize[1] ? pageSize[1] + " (" + pageSize[0] + ")"
@@ -1004,7 +1004,7 @@ liberator.Buffer = function () //{{{
// get meta tag data, sort and put into pageMeta[]
var metaNodes = window.content.document.getElementsByTagName("meta");
return Array.map(metaNodes, function (node) [(node.name || node.httpEquiv), liberator.util.highlightURL(node.content)])
return Array.map(metaNodes, function (node) [(node.name || node.httpEquiv), liberator.template.highlightURL(node.content)])
.sort(function (a, b) String.localeCompare(a[0].toLowerCase(), b[0].toLowerCase()));
});
@@ -1977,158 +1977,4 @@ liberator.Marks = function () //{{{
//}}}
}; //}}}
liberator.template = {
add: function (a, b) a + b,
join: function (c) function (a, b) a + c + b,
map: function (iter, fn, sep)
{
if (iter.length) /* Kludge? */
iter = liberator.util.arrayIter(iter);
let ret = <></>;
let n = 0;
for each (let i in iter)
{
let val = fn(i);
if (val == undefined)
continue;
if (sep && n++)
ret += sep;
ret += val;
}
return ret;
},
map2: function (iter, fn, sep)
{
// Could cause performance problems.
return this.map(Iterator(iter), function (x) fn.apply(null, x), sep);
},
maybeXML: function (xml)
{
if (typeof xml == "xml")
return xml;
try
{
return new XMLList(xml);
}
catch (e) {}
return <>{xml}</>;
},
generic: function (xml)
{
return <>:{liberator.commandline.getCommand()}<br/></> + xml;
},
bookmarks: function (header, items)
{
return this.generic(
<table>
<tr align="left" class="hl-Title">
<th>{header}</th><th>URL</th>
</tr>
{
this.map(items, function (item)
<tr>
<td>{liberator.util.clip(item.title, 50)}</td>
<td style="width: 100%">
<a href="#" class="hl-URL">{item.url}</a>&#160;
{
!(item.extra && item.extra.length) ? "" :
<span class="extra-info">
({
liberator.template.map(item.extra, function (e)
<>{e[0]}: <span class={e[2]}>{e[1]}</span></>,
<![CDATA[ ]]>/* Non-breaking space */)
})
</span>
}
</td>
</tr>)
}
</table>);
},
jumps: function (index, elems)
{
return this.generic(
<table>
<tr style="text-align: left;" class="hl-Title">
<th colspan="2">jump</th><th>title</th><th>URI</th>
</tr>
{
this.map2(elems, function (idx, val)
<tr>
<td class="indicator">{idx == index ? ">" : ""}</td>
<td>{Math.abs(idx - index)}</td>
<td style="width: 250px; max-width: 500px; overflow: hidden;">{val.title}</td>
<td><a href="#" class="hl-URL jump-list">{val.URI.spec}</a></td>
</tr>)
}
</table>);
},
options: function (title, opts)
{
return this.generic(
<table>
<tr class="hl-Title" align="left">
<th>--- {title} ---</th>
</tr>
{
this.map(opts, function (opt)
<tr>
<td>
<span style={opt.isDefault ? "" : "font-weight: bold"}>{opt.pre}{opt.name}{opt.value}</span>
{opt.isDefault || opt.default == null ? "" : <span class="extra-info"> (default: {opt.default})</span>}
</td>
</tr>)
}
</table>);
},
table: function (title, data, indent)
{
let table =
<table>
<tr class="hl-Title" align="left">
<th colspan="2">{title}</th>
</tr>
{
this.map(data, function (datum)
<tr>
<td style={"font-weight: bold; min-width: 150px; padding-left: " + (indent || "2ex")}>{datum[0]}</td>
<td>{liberator.template.maybeXML(datum[1])}</td>
</tr>)
}
</table>;
if (table.tr.length() > 1)
return table;
},
tabular: function (headings, style, iter)
{
/* This might be mind-bogglingly slow. We'll see. */
return this.generic(
<table>
<tr class="hl-Title" align="left">
{
this.map(headings, function (h)
<th>{h}</th>)
}
</tr>
{
this.map(iter, function (row)
<tr>
{
liberator.template.map2(row, function (i, d)
<td style={style[i] || ""}>{d}</td>)
}
</tr>)
}
</table>);
},
};
// vim: set fdm=marker sw=4 ts=4 et:

View File

@@ -489,10 +489,12 @@ liberator.Completion = function () //{{{
if (!filter)
return [[a[0], a[1], favicon ? a[2] : null] for each (a in array)];
let result;
if (matchFromBeginning)
return buildLongestStartingSubstring(array, filter, favicon);
result = buildLongestStartingSubstring(array, filter, favicon);
else
return buildLongestCommonSubstring(array, filter, favicon);
result = buildLongestCommonSubstring(array, filter, favicon);
return result;
},
cached: function (key, filter, generate, method)

View File

@@ -49,9 +49,10 @@ the terms of any one of the MPL, the GPL or the LGPL.
<script type="application/x-javascript;version=1.8" src="io.js"/>
<script type="application/x-javascript;version=1.8" src="mappings.js"/>
<script type="application/x-javascript;version=1.8" src="modes.js"/>
<script type="application/x-javascript;version=1.8" src="options.js"/>
<script type="application/x-javascript;version=1.8" src="template.js"/>
<script type="application/x-javascript;version=1.8" src="ui.js"/>
<script type="application/x-javascript;version=1.8" src="util.js"/>
<script type="application/x-javascript;version=1.8" src="options.js"/>
<window id="&liberator.mainWindow;">

View File

@@ -40,21 +40,22 @@ the terms of any one of the MPL, the GPL or the LGPL.
<script type="application/x-javascript;version=1.8" src="liberator.js"/>
<script type="application/x-javascript;version=1.8" src="muttator.js"/>
<script type="application/x-javascript;version=1.8" src="commands.js"/>
<script type="application/x-javascript;version=1.8" src="mappings.js"/>
<script type="application/x-javascript;version=1.8" src="options.js"/>
<script type="application/x-javascript;version=1.8" src="io.js"/>
<script type="application/x-javascript;version=1.8" src="editor.js"/>
<!--script type="application/x-javascript;version=1.8" src="mail.js"/-->
<script type="application/x-javascript;version=1.8" src="util.js"/>
<script type="application/x-javascript;version=1.8" src="modes.js"/>
<script type="application/x-javascript;version=1.8" src="events.js"/>
<script type="application/x-javascript;version=1.8" src="completion.js"/>
<script type="application/x-javascript;version=1.8" src="buffer.js"/>
<script type="application/x-javascript;version=1.8" src="commands.js"/>
<script type="application/x-javascript;version=1.8" src="completion.js"/>
<script type="application/x-javascript;version=1.8" src="editor.js"/>
<script type="application/x-javascript;version=1.8" src="events.js"/>
<script type="application/x-javascript;version=1.8" src="find.js"/>
<script type="application/x-javascript;version=1.8" src="help.js"/>
<script type="application/x-javascript;version=1.8" src="hints.js"/>
<script type="application/x-javascript;version=1.8" src="io.js"/>
<script type="application/x-javascript;version=1.8" src="mappings.js"/>
<script type="application/x-javascript;version=1.8" src="modes.js"/>
<script type="application/x-javascript;version=1.8" src="options.js"/>
<script type="application/x-javascript;version=1.8" src="template.js"/>
<script type="application/x-javascript;version=1.8" src="ui.js"/>
<script type="application/x-javascript;version=1.8" src="util.js"/>
<window id="msgcomposeWindow">

View File

@@ -958,7 +958,7 @@ liberator.Options = function () //{{{
}
else
{
option.value = <>={liberator.util.colorize(opt.value, false)}</>;
option.value = <>={liberator.template.highlight(opt.value)}</>;
}
yield option;
}
@@ -989,7 +989,7 @@ liberator.Options = function () //{{{
let option = {
isDefault: !userValue,
default: loadPreference(pref, null, true),
value: <>={liberator.util.colorize(value, false)}</>,
value: <>={liberator.template.highlight(value)}</>,
name: pref,
pre: "  ", /* Unicode nonbreaking space. */
};

217
content/template.js Normal file
View File

@@ -0,0 +1,217 @@
liberator.template = {
add: function (a, b) a + b,
join: function (c) function (a, b) a + c + b,
map: function (iter, fn, sep)
{
if (iter.length) /* Kludge? */
iter = liberator.util.arrayIter(iter);
let ret = <></>;
let n = 0;
for each (let i in iter)
{
let val = fn(i);
if (val == undefined)
continue;
if (sep && n++)
ret += sep;
ret += val;
}
return ret;
},
map2: function (iter, fn, sep)
{
// Could cause performance problems.
return this.map(Iterator(iter), function (x) fn.apply(null, x), sep);
},
maybeXML: function (xml)
{
if (typeof xml == "xml")
return xml;
try
{
return new XMLList(xml);
}
catch (e) {}
return <>{xml}</>;
},
// if "processStrings" is true, any passed strings will be surrounded by " and
// any line breaks are displayed as \n
highlight: function (arg, processStrings)
{
// some objects like window.JSON or getBrowsers()._browsers need the try/catch
try
{
switch (arg == null ? "undefined" : typeof arg)
{
case "number":
return <span class="hl-Number">{arg}</span>;
case "string":
if (processStrings)
arg = <>{liberator.util.escapeString(arg)}</>;
return <span class="hl-String">{arg}</span>;
case "boolean":
return <span class="hl-Boolean">{arg}</span>;
case "function":
return <span class="hl-Function">{arg}</span>;
return <span class="hl-Function">{String(arg).replace(/\{(.|\n)*/, "{ ... }")}</span>; /* } vim */
case "undefined":
return <span class="hl-Null">{arg}</span>;
case "object":
// for java packages value.toString() would crash so badly
// that we cannot even try/catch it
if (/^\[JavaPackage.*\]$/.test(arg))
return <>[JavaPackage]</>;
return <>{arg}</>;
default:
return <![CDATA[<unknown type>]]>;
}
}
catch (e)
{
return<![CDATA[<unknown>]]>;
}
},
highlightFilter: function (str, filter)
{
let lcstr = str.toLowerCase();
let lcfilter = filter.toLowerCase();
let s = <></>;
let start = 0;
let i;
while ((i = lca.indexOf(lcfilter, start)) > -1)
{
s += <>{str.substring(start, i)}</>;
s += <span style="font-weight: bold">{str.substr(i, filter.length)}</span>;
start = i + filter.length;
}
return s + <>{str.substr(start)}</>;
},
highlightURL: function (str, force)
{
if (force || /^[a-zA-Z]+:\/\//.test(str))
return <a class="hl-URL" href="#">{str}</a>;
else
return str;
},
generic: function (xml)
{
return <>:{liberator.commandline.getCommand()}<br/></> + xml;
},
bookmarks: function (header, items)
{
return this.generic(
<table>
<tr align="left" class="hl-Title">
<th>{header}</th><th>URL</th>
</tr>
{
this.map(items, function (item)
<tr>
<td>{liberator.util.clip(item.title, 50)}</td>
<td style="width: 100%">
<a href="#" class="hl-URL">{item.url}</a>&#160;
{
!(item.extra && item.extra.length) ? "" :
<span class="extra-info">
({
liberator.template.map(item.extra, function (e)
<>{e[0]}: <span class={e[2]}>{e[1]}</span></>,
<![CDATA[ ]]>/* Non-breaking space */)
})
</span>
}
</td>
</tr>)
}
</table>);
},
jumps: function (index, elems)
{
return this.generic(
<table>
<tr style="text-align: left;" class="hl-Title">
<th colspan="2">jump</th><th>title</th><th>URI</th>
</tr>
{
this.map2(elems, function (idx, val)
<tr>
<td class="indicator">{idx == index ? ">" : ""}</td>
<td>{Math.abs(idx - index)}</td>
<td style="width: 250px; max-width: 500px; overflow: hidden;">{val.title}</td>
<td><a href="#" class="hl-URL jump-list">{val.URI.spec}</a></td>
</tr>)
}
</table>);
},
options: function (title, opts)
{
return this.generic(
<table>
<tr class="hl-Title" align="left">
<th>--- {title} ---</th>
</tr>
{
this.map(opts, function (opt)
<tr>
<td>
<span style={opt.isDefault ? "" : "font-weight: bold"}>{opt.pre}{opt.name}{opt.value}</span>
{opt.isDefault || opt.default == null ? "" : <span class="extra-info"> (default: {opt.default})</span>}
</td>
</tr>)
}
</table>);
},
table: function (title, data, indent)
{
let table =
<table>
<tr class="hl-Title" align="left">
<th colspan="2">{title}</th>
</tr>
{
this.map(data, function (datum)
<tr>
<td style={"font-weight: bold; min-width: 150px; padding-left: " + (indent || "2ex")}>{datum[0]}</td>
<td>{liberator.template.maybeXML(datum[1])}</td>
</tr>)
}
</table>;
if (table.tr.length() > 1)
return table;
},
tabular: function (headings, style, iter)
{
/* This might be mind-bogglingly slow. We'll see. */
return this.generic(
<table>
<tr class="hl-Title" align="left">
{
this.map(headings, function (h)
<th>{h}</th>)
}
</tr>
{
this.map(iter, function (row)
<tr>
{
liberator.template.map2(row, function (i, d)
<td style={style[i] || ""}>{d}</td>)
}
</tr>)
}
</table>);
},
};

View File

@@ -91,57 +91,6 @@ liberator.util = { //{{{
return str.length <= length ? str : str.substr(0, length - 3) + "...";
},
// TODO: use :highlight color groups
// if "processStrings" is true, any passed strings will be surrounded by " and
// any line breaks are displayed as \n
colorize: function (arg, processStrings)
{
var type = typeof arg;
// some objects like window.JSON or getBrowsers()._browsers need the try/catch
try
{
if (type == "number")
{
return <span class="hl-Number">{arg}</span>;
}
else if (type == "string")
{
if (processStrings)
arg = <>"{arg.replace(/\n/, "\\n")}"</>;
return <span class="hl-String">{arg}</span>;
}
else if (type == "boolean")
{
return <span class="hl-Boolean">{arg}</span>;
}
else if (arg == null || arg == "undefined")
{
return <span class="hl-Null">{arg}</span>;
}
else if (type == "object" || type == "function")
{
// for java packages value.toString() would crash so badly
// that we cannot even try/catch it
if (/^\[JavaPackage.*\]$/.test(arg))
return <>[JavaPackage]</>;
var str = arg.toString();
if (typeof str == "string") // can be "undefined"
return <>{str}</>;
else
return <>undefined</>;
}
}
catch (e)
{
return <><![CDATA[<unknown>]]></>;
}
return <><![CDATA[<unknown type>]]></>;
},
copyToClipboard: function (str, verbose)
{
var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
@@ -208,7 +157,8 @@ liberator.util = { //{{{
}
for (let u = strNum[0].length - 3; u > 0; u -= 3) // make a 10000 a 10,000
strNum[0] = strNum[0].substring(0, u) + "," + strNum[0].substring(u, strNum[0].length);
strNum[0] = strNum[0].substring(0, u)
+ "," + strNum[0].substring(u, strNum[0].length);
if (unitIndex) // decimalPlaces only when > Bytes
strNum[0] += "." + strNum[1];
@@ -268,14 +218,6 @@ liberator.util = { //{{{
return ret;
},
highlightURL: function (str, force)
{
if (force || /^[a-zA-Z]+:\/\//.test(str))
return <a class="hl-URL" href="#">{str}</a>;
else
return str;
},
// if color = true it uses HTML markup to color certain items
objectToString: function (object, color)
{
@@ -311,19 +253,16 @@ liberator.util = { //{{{
{
for (let i in object)
{
var value;
let value = "<no value>";
try
{
value = object[i];
}
catch (e)
{
value = "<no value>";
}
catch (e) {}
if (color)
{
value = this.colorize(value, true).toXMLString();
value = liberator.template.highlight(value, true).toXMLString();
i = <span style="font-weight: bold;">{i}</span>.toXMLString();
}
string += i + ": " + value + "\n";