forked from leachiM2k/vcard.js
/
vcard.js
159 lines (132 loc) · 5.63 KB
/
vcard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// ____________
// | | A Javascript parser for vCards
// | vCard.js | Created by Mattt Thompson, 2008
// | | Released under the MIT License
// ̅̅̅̅̅̅̅̅̅̅̅̅
// Adding Javascript 1.6 Compatibility
if(!Array.prototype.forEach){Array.prototype.forEach=function(d,c){c=c||this;for(var b=0,a=this.length;b<a;b++){d.call(c,this[b],b,this)}}}if(typeof Prototype!="undefined"||!Array.prototype.map){Array.prototype.map=function(d,c){c=c||this;var e=[];for(var b=0,a=this.length;b<a;b++){e.push(d.call(c,this[b],b,this))}return e}}if(typeof Prototype!="undefined"||!Array.prototype.filter){Array.prototype.filter=function(d,c){c=c||this;var e=[];for(var b=0,a=this.length;b<a;b++){if(d.call(c,this[b],b,this)){e.push(this[b])}}return e}}["forEach","map","filter","slice","concat"].forEach(function(a){if(!Array[a]){Array[a]=function(b){return this.prototype[a].apply(b,Array.prototype.slice.call(arguments,1))}}});Date.ISO8601PartMap={Year:1,Month:3,Date:5,Hours:7,Minutes:8,Seconds:9};Date.matchISO8601=function(a){return a.match(/^(\d{4})(-?(\d{2}))?(-?(\d{2}))?(T(\d{2}):?(\d{2})(:?(\d{2}))?)?(Z?(([+\-])(\d{2}):?(\d{2})))?$/)};Date.parseISO8601=function(e){var b=this.matchISO8601(e);if(b){var a=new Date,c,d=0;for(var f in this.ISO8601PartMap){if(part=b[this.ISO8601PartMap[f]]){a["set"+f]((f=="Month")?parseInt(part)-1:parseInt(part))}else{a["set"+f]((f=="Date")?1:0)}}if(b[11]){d=(parseInt(b[14])*60)+parseInt(b[15]);d*=((parseInt[13]=="-")?1:-1)}d-=a.getTimezoneOffset();a.setTime(a.getTime()+(d*60*1000));return a}};
vCard = {
initialize: function(_input){
var vc = {};
this.parse(_input, vc);
vc.prototype = vCard.Base;
return vCard.extend(vc, vCard.SingletonMethods);
},
parse: function(_input, fields) {
var regexps = {
simple: /^(version|fn|title|org)\:(.+)$/i,
complex: /^([^\:\;]+);([^\:]+)\:(.+)$/,
key: /item\d{1,2}\./,
properties: /((type=)?(.+);?)+/
}
var lines = _input.split(/\r?\n/);
for (n in lines) {
line = lines[n];
if(regexps['simple'].test(line))
{
results = line.match(regexps['simple']);
key = results[1].toLowerCase();
value = results[2];
fields[key] = value;
}
else if(regexps['complex'].test(line))
{
results = line.match(regexps['complex']);
key = results[1].replace(regexps['key'], '').toLowerCase();
properties = results[2].split(';');
properties = Array.filter(properties, function(p) { return ! p.match(/[a-z]+=[a-z]+/) });
properties = Array.map(properties, function(p) { return p.replace(/type=/g, '') });
type = properties.pop() || 'default';
type = type.toLowerCase();
value = results[3];
value = /;/.test(value) ? [value.split(';')] : value;
fields[key] = fields[key] || {};
fields[key][type] = fields[key][type] || [];
fields[key][type] = fields[key][type].concat(value);
}
}
},
SingletonMethods: {
to_html: function() {
var output = '<div class="vcard">';
if(this.photo)
{
output += '<img class="photo" src="data:image/png;base64,' + this.photo['base64'][0] + '" />';
}
output += '<span class="fn">' + this.fn + '</span>'; // Required
if(this.title)
{
output += '<span class="title">' + this.title + '</span>';
}
if(this.org)
{
output += '<span class="org">' + this.org + '</span>';
}
output += '<hr/>'
for(type in this.adr)
{
for(n in this.adr[type])
{
value = this.adr[type][n];
output += '<address class="adr">';
output += '<span class="type">' + type + '</span>';
output += '<div class="content">';
adr_fields = ['post-office-box', 'extended-address', 'street-address',
'locality', 'region', 'postal-code', 'country-name' ]
for(field in adr_fields)
{
if(value[field])
{
output += '<span class="' + adr_fields[field] + '">';
output += value[field];
output += '</span>';
}
}
output += '</div>';
output += '</address>';
}
}
for(type in this.tel)
{
for(n in this.tel[type])
{
value = this.tel[type][n];
output += '<span class="tel">';
output += '<span class="type">' + type + '</span>';
output += '<span class="value">' + value + '</span>';
output += '</span>';
}
}
for(type in this.email)
{
for(n in this.email[type])
{
value = this.email[type][n];
output += '<span class="email">';
output += '<span class="type">' + type + '</span>';
output += '<a class="value" href="mailto:' + value + '">' + value + '</a>';
output += '</span>';
}
}
for(type in this.url)
{
for(n in this.url[type])
{
value = this.url[type][n];
output += '<span class="url">';
output += '<span class="type">' + type + '</span>';
output += '<a class="value" href="' + value + '">' + value + '</a>';
output += '</span>';
}
}
output += '</div>';
output = output.replace(/\\n/g, '<br/>');
return output;
}
},
extend : function(dest, source) {
for (var prop in source) dest[prop] = source[prop];
return dest;
},
Base: {}
}