Active.Formats.Anchor = Active.Formats.String.subclass();
Active.Formats.Anchor.create = function(){
var obj = this.prototype;
var _link = null;
obj.setLink = function( l ) {
_link = l;
}
obj.getLink = function(src,data) {
var l = _link;
if (typeof(l)=="function") {
l = l.call(this,src,data);
}
return l;
}
obj.dataToText = function(data){
var html = new Active.System.HTML();
html.setTag("a");
html.setContent(data);
if (_link) {
html.setAttribute( "href", obj.getLink(this,data) );
}
return html;
};
obj.dataToValue = function( data ) {
return data;
}
// plus some regex code to extract and replace
// column references with their values from the result of
// _link
};
Active.Formats.Anchor.create();
var resolveFromModel = function( raw ) {
var str = raw;
var result = str;
// define search pattern
var pattern = /\{(\d+)\}/g;
var results = pattern.exec( str );
while (results) {
str = str.replace( results[0], getColumnValue( results[1] ) );
results = pattern.exec( str );
}
return str;
}
/**
* this function still needs to be written
*/
var getColumnValue = function( colIndex ) {
return "cv_"+colIndex;
}
// then
...
var anc0 = new Active.Formats.Anchor();
var model = new Active.XML.Table;
model.setFormat( anc0, 0 );
anc0.setLink( function( src, data ){
return "http://www.google.com?q={1}&bar={2}";
} );
...
// create xml data model
var table = new Active.XML.Table;
// save the default getText method
var getText = table.getText;
// define new custom getText method
table.getText = function(i, j){
if (j != 4) {
return getText.call(this, i, j);
}
else {
var link = "<a href='{2}'>{1}</a>";
var param1 = getText.call(this, i, 1);
var param2 = getText.call(this, i, 2);
return link.replace("{1}", param1).replace("{2}", param2);
}
}
// define new custom getValue method
table.getValue = function(i, j){
// do something similar for getValue method, which defines sorting
}
...
var obj = new Active.Controls.Grid;
obj.setModel("data", table);
// hide columns 1 and 2
obj.setProperty("column/values", [0,3,4]);
Active.Templates.Anchor = Active.System.Template.subclass();
Active.Templates.Anchor.create = function(){
/****************************************************************
Anchor Cell template. If this Template is a child of an
Active.Templates.Row Object, it will automatically
convert {colIndex} (eg {0},{1}, etc) to the values from the
respective columns on the row. For example, if column 0
contains the value 1234 then {0} will be replaced by 1234
in the href hyperlink.
*****************************************************************/
var obj = this.prototype;
var _super = this.superclass.prototype;
// ------------------------------------------------------------
obj.setClass("templates", "item");
obj.setClass("box", "anchor");
// ------------------------------------------------------------
var anchor = new Active.System.HTML;
anchor.setTag("a");
anchor.setAttribute("href", function() {
var rawLink = this.getHref();
var link = this.processLink( rawLink );
return link;
});
// ------------------------------------------------------------
anchor.setClass("item", "anchor");
anchor.setClass("anchor", function(){return this.getItemProperty("index")});
obj.setContent("anchor", anchor);
obj.setContent("anchor/text", function(){return this.getItemProperty("text")});
/****************************************************************
Allows specification of custom anchor href attribute
*****************************************************************/
var href = "javascript:void();";
obj.setHref = function( link ) { href = link; }
obj.getHref = function() {
var a = href;
if (typeof(a)=='function') a = a.call(this);
return a;
}
/****************************************************************
Performs search and replace on the regular expression /\{(\d+)\}/g;
(eg {0} -> 0 ) and replaces the value with it's referenced column's
text value.
todo: how do you get the unformatted column value???
*****************************************************************/
obj.processLink = function( link ) {
if (this.$owner) {
var row = this.$owner;
var pattern = /\{(\d+)\}/g;
var str = link;
var results = pattern.exec( str );
while (results) {
var colIndex = results[1];
var colValue = row.getDataProperty( "text", colIndex );
str = str.replace( results[0], colValue ? colValue : colIndex );
results = pattern.exec( str );
}
link = str;
}
return link;
}
};
Active.Templates.Anchor.create();
This topic is archived.