Merge remote branch 'remotes/git-svn'
[cxgn-jslib.git] / Text / Markup.js
blob55d9beb1dda382c8d6db64014f92b2bdd65c80df
1 if (typeof(JSAN) != 'undefined' ) {
2 JSAN.use("jQuery", []);
6 var Text;
7 if(!Text) Text = {};
9 Text.Markup = function(styles) {
10 if(! (this.markup_styles = styles) ) {
11 console.error('must pass style to new Text.Markup()');
15 Text.Markup.prototype.inflate_regions = function( regions ) {
16 var mu = this;
17 var ordering = 0;
18 return jQuery.map( regions, function(a,i) {
19 var style = mu.markup_styles[a[0]];
20 ordering++;
22 if(! style ) {
23 console.error('markup style "' + a + '" not defined');
24 return;
27 if( typeof style == 'object' ) {
28 return [[[style[0],a[1]],999000-ordering],[[style[1],a[2]],-ordering]];
29 } else {
30 return [[[style,a[1]],999000-ordering]];
32 });
35 Text.Markup.prototype.markup = function( regions, target_string ) {
36 // expand the two-position markups into two one-position markups and decorate with ordering
37 var markup_defs = this.inflate_regions(regions);
39 // sort the markup definitions in reverse order by coordinate and
40 // sequence number
41 markup_defs.sort( function(a,b) {
42 return (b[0][1] - a[0][1]) || (b[1] - a[1]);
43 });
46 // do the string insertions and return a new string
47 for ( var i = 0; i < markup_defs.length; i++ ) {
48 var def = markup_defs[i][0];
49 target_string = target_string.slice( 0, def[1] ) + def[0] + target_string.slice( def[1] );
52 return target_string;