src/js/utils.js
changeset 988 eefd336335f9
parent 906 4b6e154ae8de
child 996 c472984db275
equal deleted inserted replaced
987:7b65bf78873a 988:eefd336335f9
    27         }).appendTo('head');
    27         }).appendTo('head');
    28         IriSP._cssCache.push(_cssFile);
    28         IriSP._cssCache.push(_cssFile);
    29     }
    29     }
    30 }
    30 }
    31 
    31 
       
    32 IriSP.textFieldHtml = function(_text, _regexp, _extend) {
       
    33     var list = [],
       
    34         positions = [],
       
    35         text = _text.replace(/(^\s+|\s+$)/g,'');
       
    36     
       
    37     function addToList(_rx, _startHtml, _endHtml) {
       
    38         while(true) {
       
    39             var result = _rx.exec(text);
       
    40             if (!result) {
       
    41                 break;
       
    42             }
       
    43             var end = _rx.lastIndex,
       
    44                 start = result.index;
       
    45             list.push({
       
    46                 start: start,
       
    47                 end: end,
       
    48                 startHtml: (typeof _startHtml === "function" ? _startHtml(result) : _startHtml),
       
    49                 endHtml: (typeof _endHtml === "function" ? _endHtml(result) : _endHtml)
       
    50             });
       
    51             positions.push(start);
       
    52             positions.push(end);
       
    53         }
       
    54     }
       
    55     
       
    56     if (_regexp) {
       
    57         addToList(_regexp, '<span class="Ldt-Highlight">', '</span>');
       
    58     }
       
    59     
       
    60     addToList(/(https?:\/\/)?\w+\.\w+\S+/gm, function(matches) {
       
    61         return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">'
       
    62     }, '</a>');
       
    63     addToList(/@([\d\w]{1,15})/gm, function(matches) {
       
    64         return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">'
       
    65     }, '</a>');
       
    66     addToList(/\*[^*]+\*/gm, '<b>', '</b>');
       
    67     addToList(/[\n\r]+/gm, '', '<br />');
       
    68     
       
    69     IriSP._(_extend).each(function(x) {
       
    70         addToList.apply(null, x);
       
    71     });
       
    72     
       
    73     positions = IriSP._(positions)
       
    74         .chain()
       
    75         .uniq()
       
    76         .sortBy(function(p) { return parseInt(p) })
       
    77         .value();
       
    78     
       
    79     var res = "", lastIndex = 0;
       
    80     
       
    81     for (var i = 0; i < positions.length; i++) {
       
    82         var pos = positions[i];
       
    83         res += text.substring(lastIndex, pos);
       
    84         for (var j = list.length - 1; j >= 0; j--) {
       
    85             var item = list[j];
       
    86             if (item.start < pos && item.end >= pos) {
       
    87                 res += item.endHtml;
       
    88             }
       
    89         }
       
    90         for (var j = 0; j < list.length; j++) {
       
    91             var item = list[j];
       
    92             if (item.start <= pos && item.end > pos) {
       
    93                 res += item.startHtml;
       
    94             }
       
    95         }
       
    96         lastIndex = pos;
       
    97     }
       
    98     
       
    99     res += text.substring(lastIndex);
       
   100     
       
   101     return res;
       
   102     
       
   103 }
       
   104 
    32 IriSP.log = function() {
   105 IriSP.log = function() {
    33     if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) {
   106     if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) {
    34         console.log.apply(console, arguments);
   107         console.log.apply(console, arguments);
    35     }
   108     }
    36 }
   109 }