src/js/utils.js
changeset 1068 7623f9af9272
parent 1014 96ea47146c66
child 1072 ac1eacb3aa33
equal deleted inserted replaced
1067:539c9bee5372 1068:7623f9af9272
    31 
    31 
    32 IriSP.textFieldHtml = function(_text, _regexp, _extend) {
    32 IriSP.textFieldHtml = function(_text, _regexp, _extend) {
    33     var list = [],
    33     var list = [],
    34         positions = [],
    34         positions = [],
    35         text = _text.replace(/(^\s+|\s+$)/g,'');
    35         text = _text.replace(/(^\s+|\s+$)/g,'');
    36     
    36 
    37     function addToList(_rx, _startHtml, _endHtml) {
    37     function addToList(_rx, _startHtml, _endHtml) {
    38         while(true) {
    38         while(true) {
    39             var result = _rx.exec(text);
    39             var result = _rx.exec(text);
    40             if (!result) {
    40             if (!result) {
    41                 break;
    41                 break;
    50             });
    50             });
    51             positions.push(start);
    51             positions.push(start);
    52             positions.push(end);
    52             positions.push(end);
    53         }
    53         }
    54     }
    54     }
    55     
    55 
    56     if (_regexp) {
    56     if (_regexp) {
    57         addToList(_regexp, '<span class="Ldt-Highlight">', '</span>');
    57         addToList(_regexp, '<span class="Ldt-Highlight">', '</span>');
    58     }
    58     }
    59     
    59 
    60     addToList(/(https?:\/\/)?[\w\d\-]+\.[\w\d\-]+\S+/gm, function(matches) {
    60     addToList(/(https?:\/\/)?[\w\d\-]+\.[\w\d\-]+\S+/gm, function(matches) {
    61         return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">';
    61         return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">';
    62     }, '</a>');
    62     }, '</a>');
    63     addToList(/@([\d\w]{1,15})/gm, function(matches) {
    63     addToList(/@([\d\w]{1,15})/gm, function(matches) {
    64         return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">';
    64         return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">';
    65     }, '</a>');
    65     }, '</a>');
    66     addToList(/\*[^*]+\*/gm, '<b>', '</b>');
    66     addToList(/\*[^*]+\*/gm, '<b>', '</b>');
    67     addToList(/[\n\r]+/gm, '', '<br />');
    67     addToList(/[\n\r]+/gm, '', '<br />');
    68     
    68 
    69     IriSP._(_extend).each(function(x) {
    69     IriSP._(_extend).each(function(x) {
    70         addToList.apply(null, x);
    70         addToList.apply(null, x);
    71     });
    71     });
    72     
    72 
    73     positions = IriSP._(positions)
    73     positions = IriSP._(positions)
    74         .chain()
    74         .chain()
    75         .uniq()
    75         .uniq()
    76         .sortBy(function(p) { return parseInt(p); })
    76         .sortBy(function(p) { return parseInt(p); })
    77         .value();
    77         .value();
    78     
    78 
    79     var res = "", lastIndex = 0;
    79     var res = "", lastIndex = 0;
    80     
    80 
    81     for (var i = 0; i < positions.length; i++) {
    81     for (var i = 0; i < positions.length; i++) {
    82         var pos = positions[i];
    82         var pos = positions[i];
    83         res += text.substring(lastIndex, pos);
    83         res += text.substring(lastIndex, pos);
    84         for (var j = list.length - 1; j >= 0; j--) {
    84         for (var j = list.length - 1; j >= 0; j--) {
    85             var item = list[j];
    85             var item = list[j];
    93                 res += item.startHtml;
    93                 res += item.startHtml;
    94             }
    94             }
    95         }
    95         }
    96         lastIndex = pos;
    96         lastIndex = pos;
    97     }
    97     }
    98     
    98 
    99     res += text.substring(lastIndex);
    99     res += text.substring(lastIndex);
   100     
   100 
   101     return res;
   101     return res;
   102     
   102 
   103 };
   103 };
   104 
   104 
   105 IriSP.log = function() {
   105 IriSP.log = function() {
   106     if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) {
   106     if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) {
   107         console.log.apply(console, arguments);
   107         console.log.apply(console, arguments);
   110 
   110 
   111 IriSP.attachDndData = function(jqSel, data) {
   111 IriSP.attachDndData = function(jqSel, data) {
   112 	jqSel.attr("draggable", "true").on("dragstart", function(_event) {
   112 	jqSel.attr("draggable", "true").on("dragstart", function(_event) {
   113 		var d = (typeof data === "function" ? data.call(this) : data);
   113 		var d = (typeof data === "function" ? data.call(this) : data);
   114 		try {
   114 		try {
       
   115             if (d.html === undefined && d.uri && d.text) {
       
   116                 d.html = '<a href="' + d.uri + '">' + d.text + '</a>';
       
   117             }
   115 			IriSP._(d).each(function(v, k) {
   118 			IriSP._(d).each(function(v, k) {
   116 				if (v) {
   119                 if (v && k != 'text' && k != 'html') {
   117 					_event.originalEvent.dataTransfer.setData("text/x-iri-" + k, v);
   120 					_event.originalEvent.dataTransfer.setData("text/x-iri-" + k, v);
   118 				}
   121 				}
   119 			});
   122 			});
       
   123             if (d.uri && d.text) {
       
   124                 _event.originalEvent.dataTransfer.setData("text/x-moz-url", d.uri + "\n" + d.text.replace("\n", " "));
       
   125                 _event.originalEvent.dataTransfer.setData("text/plain", d.text + " " + d.uri);
       
   126             }
       
   127             // Define generic text/html and text/plain last (least
       
   128             // specific types, see
       
   129             // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Drag_operations#Drag_Data)
       
   130             if (d.html !== undefined) {
       
   131                 _event.originalEvent.dataTransfer.setData("text/html", d.html);
       
   132             }
       
   133             if (d.text !== undefined && ! d.uri) {
       
   134                 _event.originalEvent.dataTransfer.setData("text/plain", d.text);
       
   135             }
   120 		} catch(err) {
   136 		} catch(err) {
   121 			_event.originalEvent.dataTransfer.setData("Text", JSON.stringify(d));
   137 			_event.originalEvent.dataTransfer.setData("Text", JSON.stringify(d));
   122 		}
   138 		}
   123 	});
   139 	});
   124 };
   140 };
   128         noop = (function() {});
   144         noop = (function() {});
   129     IriSP._(properties).each(function(p) {
   145     IriSP._(properties).each(function(p) {
   130         _this[p] = noop;
   146         _this[p] = noop;
   131     });
   147     });
   132 };
   148 };
       
   149 
       
   150 IriSP.timestamp2ms = function(t) {
       
   151     // Convert timestamp to numeric value
       
   152     // It accepts the following forms:
       
   153     // [h:mm:ss] [mm:ss] [ss]
       
   154     var s = t.split(":").reverse();
       
   155     while (s.length < 3) {
       
   156         s.push("0");
       
   157     }
       
   158     return 1000 * (3600 * parseInt(s[2], 10) + 60 * parseInt(s[1], 10) + parseInt(s[0], 10));
       
   159 };
       
   160 
       
   161 IriSP.setFullScreen= function(elem, value) {
       
   162     // Set fullscreen on or off
       
   163     if (value) {
       
   164 		if (elem.requestFullscreen) {
       
   165 			elem.requestFullscreen();
       
   166 		} else if (elem.mozRequestFullScreen) {
       
   167 			elem.mozRequestFullScreen();
       
   168 		} else if (elem.webkitRequestFullscreen) {
       
   169 			elem.webkitRequestFullscreen();
       
   170 		} else if (elem.msRequestFullscreen) {
       
   171 			elem.msRequestFullscreen();
       
   172 		}
       
   173 	} else {
       
   174         if (document.exitFullscreen) {
       
   175             document.exitFullscreen();
       
   176         } else if (document.msExitFullscreen) {
       
   177             document.msExitFullscreen();
       
   178         } else if (document.mozCancelFullScreen) {
       
   179             document.mozCancelFullScreen();
       
   180         } else if (document.webkitExitFullscreen) {
       
   181             document.webkitExitFullscreen();
       
   182         }
       
   183     }
       
   184 };
       
   185 
       
   186 IriSP.isFullscreen = function() {
       
   187 	return (document.fullscreenElement ||  document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement);
       
   188 };
       
   189 
       
   190 IriSP.getFullscreenElement = function () {
       
   191     return (document.fullscreenElement
       
   192             || document.webkitFullscreenElement
       
   193             || document.mozFullScreenElement
       
   194             || document.msFullscreenElement
       
   195             || undefined);
       
   196 };
       
   197 
       
   198 IriSP.getFullscreenEventname = function () {
       
   199     return ((document.exitFullscreen && "fullscreenchange")
       
   200             || (document.webkitExitFullscreen && "webkitfullscreenchange")
       
   201             || (document.mozExitFullScreen && "mozfullscreenchange")
       
   202             || (document.msExitFullscreen && "msfullscreenchange")
       
   203             || "");
       
   204 };