* added String.NULL as constant containing the empty string ""
* added global- and HopObject-related macros to core
This commit is contained in:
parent
50d4eb0d93
commit
7860cbdde2
4 changed files with 212 additions and 17 deletions
67
core/Global.js
Normal file
67
core/Global.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2005 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: Date.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.2 $
|
||||
* $Date: 2006/04/24 07:02:17 $
|
||||
*/
|
||||
|
||||
|
||||
app.addRepository("modules/core/String.js");
|
||||
|
||||
|
||||
/**
|
||||
* write out a property contained in app.properties
|
||||
* @param Object containing the name of the property
|
||||
*/
|
||||
function property_macro(param) {
|
||||
res.write(app.properties[param.name] || String.NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* wrapper to output a string from within a skin
|
||||
* just to be able to use different encodings
|
||||
* @param Object containing the string as text property
|
||||
*/
|
||||
function write_macro(param) {
|
||||
res.write(param.text || String.NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* renders the current datetime
|
||||
* @param Object containing a formatting string as format property
|
||||
*/
|
||||
function now_macro(param) {
|
||||
var d = new Date();
|
||||
if (param.format) {
|
||||
res.write(d.format(param.format));
|
||||
} else if (param.as == "timestamp") {
|
||||
res.write(d.getTime());
|
||||
} else {
|
||||
res.write(d);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* renders a global skin
|
||||
*/
|
||||
function skin_macro(param) {
|
||||
if (param.name) {
|
||||
renderSkin(param.name);
|
||||
}
|
||||
return;
|
||||
}
|
125
core/HopObject.js
Normal file
125
core/HopObject.js
Normal file
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2005 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: Date.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.2 $
|
||||
* $Date: 2006/04/24 07:02:17 $
|
||||
*/
|
||||
|
||||
|
||||
app.addRepository("modules/core/Number.js");
|
||||
app.addRepository("modules/core/String.js");
|
||||
|
||||
|
||||
/**
|
||||
* macro returns the id of a HopObject
|
||||
*/
|
||||
HopObject.prototype.id_macro = function(param) {
|
||||
res.write(this._id);
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* macro returns the url for any hopobject
|
||||
*/
|
||||
HopObject.prototype.href_macro = function(param) {
|
||||
res.write(this.href(param.action || String.NULLSTR));
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* macro rendering a skin or displaying
|
||||
* its source (param.as == "source")
|
||||
*/
|
||||
HopObject.prototype.skin_macro = function(param) {
|
||||
if (param.name) {
|
||||
if (param.as == "source") {
|
||||
var str = app.skinfiles[this._prototype][param.name];
|
||||
if (str && param.unwrap == "true") {
|
||||
str = str.unwrap();
|
||||
}
|
||||
} else {
|
||||
var str = this.renderSkinAsString(param.name, param);
|
||||
}
|
||||
res.write(str);
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* this macro renders a text depending on
|
||||
* the value of a given property
|
||||
*/
|
||||
HopObject.prototype.switch_macro = function(param) {
|
||||
if (param.name) {
|
||||
res.write(this[param.name] ? param.on : param.off);
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* generic macro that loops over the childobjects
|
||||
* and renders a specified skin for each of them
|
||||
* @param Object providing the following properties:
|
||||
* skin: the skin to render for each item (required)
|
||||
* collection: the collection containing the items
|
||||
* limit: max. number of items per page
|
||||
* (req.data.page determines the page number)
|
||||
* sort: property name to use for sorting
|
||||
* order: sort order (either "asc" or "desc")
|
||||
*/
|
||||
HopObject.prototype.loop_macro = function(param) {
|
||||
if (!param.skin) {
|
||||
return;
|
||||
}
|
||||
var items = param.collection ? this[param.collection] : this;
|
||||
if (!items || !items.size || items.size() < 1) {
|
||||
return;
|
||||
}
|
||||
// set default values
|
||||
var min = 0, max = items.size();
|
||||
var pagesize = max;
|
||||
if (param.limit) {
|
||||
var n = parseInt(param.limit, 10);
|
||||
if (!isNaN(n)) {
|
||||
pagesize = n;
|
||||
}
|
||||
var pagenr = parseInt(req.data.page, 10);
|
||||
if (isNaN(pagenr)) {
|
||||
pagenr = 0;
|
||||
}
|
||||
min = Math.min(max, pagenr * pagesize);
|
||||
max = Math.min(max, min + pagesize);
|
||||
}
|
||||
if (param.sort) {
|
||||
var allitems = items.list();
|
||||
var test = allitems[0][param.sort];
|
||||
if (!test || isNaN(test)) {
|
||||
var Sorter = String.Sorter;
|
||||
} else {
|
||||
var Sorter = Number.Sorter;
|
||||
}
|
||||
allitems.sort(new Sorter(param.sort, Sorter[param.order.toUpperCase()]));
|
||||
var itemlist = allitems.slice(min, max);
|
||||
} else {
|
||||
var itemlist = items.list(min, max);
|
||||
}
|
||||
var skinParam = {};
|
||||
for (var i=0; i<itemlist.length; i+=1) {
|
||||
skinParam.index = pagenr * pagesize + i + 1;
|
||||
itemlist[i].renderSkin(param.skin, skinParam);
|
||||
}
|
||||
return;
|
||||
};
|
|
@ -9,9 +9,9 @@
|
|||
* Copyright 1998-2006 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: String.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.2 $
|
||||
* $Date: 2006/04/24 07:02:17 $
|
||||
* $Author: tobi $
|
||||
* $Revision: 1.3 $
|
||||
* $Date: 2006/07/24 16:03:57 $
|
||||
*/
|
||||
|
||||
|
||||
|
@ -26,6 +26,7 @@ String.LEFT = -1
|
|||
String.BALANCE = 0
|
||||
String.RIGHT = 1
|
||||
String.ISOFORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||
String.NULL = "";
|
||||
|
||||
|
||||
/**
|
||||
|
@ -40,8 +41,8 @@ String.Sorter = function(field, order) {
|
|||
var key = field + ":" + order;
|
||||
if (!String.Sorter.cache[key]) {
|
||||
String.Sorter.cache[key] = function(a, b) {
|
||||
var str1 = String(a[field] || "").toLowerCase();
|
||||
var str2 = String(b[field] || "").toLowerCase();
|
||||
var str1 = String(a[field] || String.NULL).toLowerCase();
|
||||
var str2 = String(b[field] || String.NULL).toLowerCase();
|
||||
if (str1 > str2)
|
||||
return order * 1;
|
||||
if (str1 < str2)
|
||||
|
@ -83,7 +84,7 @@ String.random = function(len, mode) {
|
|||
var x = Math.random() * Math.pow(10,len);
|
||||
return Math.floor(x);
|
||||
}
|
||||
var keystr = "";
|
||||
var keystr = String.NULL;
|
||||
for (var i=0; i<len; i++) {
|
||||
var x = Math.floor((Math.random() * 36));
|
||||
if (mode == 1) {
|
||||
|
@ -113,7 +114,7 @@ String.random = function(len, mode) {
|
|||
*/
|
||||
String.join = function(str1, str2, glue) {
|
||||
if (glue == null)
|
||||
glue = "";
|
||||
glue = String.NULL;
|
||||
if (str1 && str2)
|
||||
return str1 + glue + str2;
|
||||
else if (str2)
|
||||
|
@ -195,7 +196,7 @@ String.prototype.isFileName = function() {
|
|||
* @return Boolean
|
||||
*/
|
||||
String.prototype.toFileName = function() {
|
||||
return this.replace(new RegExp(String.FILEPATTERN.source, "g"), "");
|
||||
return this.replace(new RegExp(String.FILEPATTERN.source, "g"), String.NULL);
|
||||
};
|
||||
|
||||
|
||||
|
@ -224,7 +225,7 @@ String.prototype.isHexColor = function() {
|
|||
String.prototype.toHexColor = function() {
|
||||
if (this.startsWith("rgb")) {
|
||||
res.push();
|
||||
var col = this.replace(/[^0-9,]/g,"");
|
||||
var col = this.replace(/[^0-9,]/g, String.NULL);
|
||||
var parts = col.split(",");
|
||||
for (var i in parts) {
|
||||
var num = parseInt(parts[i], 10);
|
||||
|
@ -233,7 +234,7 @@ String.prototype.toHexColor = function() {
|
|||
}
|
||||
return res.pop();
|
||||
}
|
||||
var col = this.replace(new RegExp(String.HEXPATTERN.source), "");
|
||||
var col = this.replace(new RegExp(String.HEXPATTERN.source), String.NULL);
|
||||
return col.toLowerCase().pad("0", 6, String.LEFT);
|
||||
};
|
||||
|
||||
|
@ -256,7 +257,7 @@ String.prototype.isAlphanumeric = function() {
|
|||
* @return cleaned string
|
||||
*/
|
||||
String.prototype.toAlphanumeric = function() {
|
||||
return this.replace(new RegExp(String.ANUMPATTERN.source, "g"), "");
|
||||
return this.replace(new RegExp(String.ANUMPATTERN.source, "g"), String.NULL);
|
||||
};
|
||||
|
||||
|
||||
|
@ -342,14 +343,14 @@ String.prototype.entitize = function() {
|
|||
String.prototype.embody = function(limit, clipping, delimiter) {
|
||||
if (typeof limit == "string")
|
||||
limit = parseInt(limit, 10);
|
||||
var result = {head: this, tail: ""};
|
||||
var result = {head: this, tail: String.NULL};
|
||||
if (!limit || limit < 1)
|
||||
return result;
|
||||
if (!delimiter || delimiter =="")
|
||||
if (!delimiter || delimiter == String.NULL)
|
||||
result.head= this.substring(0, limit);
|
||||
else {
|
||||
var re = new RegExp ("(" + delimiter + "+)");
|
||||
result.head = this.split(re, 2*limit - 1).join("");
|
||||
result.head = this.split(re, 2*limit - 1).join(String.NULL);
|
||||
}
|
||||
if (result.head != this) {
|
||||
result.tail = this.substring(result.head.length).trim();
|
||||
|
@ -423,7 +424,7 @@ String.prototype.group = function(interval, str, ignoreWhiteSpace) {
|
|||
*/
|
||||
String.prototype.unwrap = function(removeTags, replacement) {
|
||||
if (replacement == null)
|
||||
replacement = "";
|
||||
replacement = String.NULL;
|
||||
var str = this.replace(/[\n|\r]/g, replacement);
|
||||
if (removeTags)
|
||||
str = str.replace(/<[w]?br *\/?>/g, replacement);
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
*
|
||||
* $RCSfile: all.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.3 $
|
||||
* $Date: 2006/04/24 08:27:00 $
|
||||
* $Revision: 1.4 $
|
||||
* $Date: 2006/05/30 18:34:31 $
|
||||
*/
|
||||
|
||||
// convenience SingleFileRepository to load all the
|
||||
|
@ -22,3 +22,5 @@ app.addRepository('modules/core/Date.js');
|
|||
app.addRepository('modules/core/Number.js');
|
||||
app.addRepository('modules/core/Object.js');
|
||||
app.addRepository('modules/core/String.js');
|
||||
app.addRepository('modules/core/HopObject.js');
|
||||
app.addRepository('modules/core/Global.js');
|
||||
|
|
Loading…
Add table
Reference in a new issue