]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/url.js
add spinner to connect dialog
[irc/quakenet/qwebirc.git] / js / ui / url.js
CommitLineData
2cd9e32d 1qwebirc.ui.urlificate = function(element, text, execfn, cmdfn, window) {
117aa54f 2 var punct_re = /[[\)|\]>]?(\.*|[\,;])$/;
f121b688
CP
3 var addedText = [];
4
8e327eaa
CP
5 var txtprocess = function(text, regex, appendfn, matchfn) {
6 for(;;) {
7 var index = text.search(regex);
8 if(index == -1) {
9 appendfn(text);
10 break;
11 }
12 var match = text.match(regex);
dbd61667 13
8e327eaa
CP
14 var before = text.substring(0, index);
15 var matched = match[0];
16 var after = text.substring(index + matched.length);
83f9b8b2 17
8e327eaa 18 appendfn(before);
841a451d 19 var more = matchfn(matched, appendfn);
8e327eaa
CP
20 if(!more)
21 more = "";
22 text = more + after;
23 }
24 };
25
26 var appendText = function(text) {
f121b688 27 addedText.push(text);
1d6756bc 28 qwebirc.util.NBSPCreate(text, element);
8e327eaa
CP
29 };
30
31 var appendChan = function(text) {
32 var newtext = text.replace(punct_re, "");
f121b688 33 addedText.push(newtext);
8e327eaa
CP
34 var punct = text.substring(newtext.length);
35
7cb09779 36 var a = new Element("span");
7cb09779 37 a.addClass("hyperlink-channel");
dbd61667
CP
38 a.addEvent("click", function(e) {
39 new Event(e).stop();
8e327eaa 40 execfn("/JOIN " + newtext);
dbd61667 41 });
8e327eaa
CP
42 a.appendChild(document.createTextNode(newtext));
43 element.appendChild(a);
44
45 return punct;
46 };
47
841a451d
CP
48 var appendURL = function(text, appendfn) {
49 var url = text.replace(punct_re, "");
50 var punct = text.substring(url.length);
51
52 var href = "";
53 var fn = null;
24913278 54 var target = "_blank";
841a451d 55 var disptext = url;
925fc357
CP
56 var elementType = "a";
57 var addClass;
841a451d
CP
58
59 var ma = url.match(/^qwebirc:\/\/(.*)$/);
60 if(ma) {
925fc357 61 var m = ma[1].match(/^([^\/]+)\/([^\/]+)\/?(.*)$/);
841a451d
CP
62 if(!m) {
63 appendfn(text);
64 return;
65 }
66
2cd9e32d 67 var cmd = cmdfn(m[1], window);
841a451d 68 if(cmd) {
925fc357
CP
69 addClass = m[1];
70 elementType = cmd[0];
71 if(cmd[0] != "a") {
72 url = null;
73 } else {
74 url = "#";
75 }
76 fn = cmd[1];
841a451d
CP
77 disptext = unescape(m[2]);
78 target = null;
79 } else {
80 appendfn(text);
81 return;
82 }
925fc357
CP
83 if(m[3])
84 punct = m[3] + punct;
841a451d
CP
85 } else {
86 if(url.match(/^www\./))
87 url = "http://" + url;
88 }
dbd61667 89
925fc357
CP
90 var a = new Element(elementType);
91 if(addClass)
92 a.addClass("hyperlink-" + addClass);
93
94 if(url) {
95 a.href = url;
f7dc5bbf
CP
96
97 if(target) {
925fc357 98 a.target = target;
f7dc5bbf
CP
99 a.setAttribute("rel", "noopener noreferrer");
100 }
925fc357 101 }
f121b688 102 addedText.push(disptext);
841a451d
CP
103 a.appendChild(document.createTextNode(disptext));
104
dbd61667 105 element.appendChild(a);
841a451d
CP
106 if($defined(fn))
107 a.addEvent("click", function(e) { new Event(e).stop(); fn(disptext); });
8e327eaa
CP
108
109 return punct;
110 };
111
841a451d 112 txtprocess(text, /\b((https?|ftp|qwebirc):\/\/|www\.)[^ ]+/, function(text) {
8e327eaa
CP
113 txtprocess(text, /\B#[^ ,]+/, appendText, appendChan);
114 }, appendURL);
f121b688
CP
115
116 return addedText.join("");
83f9b8b2 117}