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