-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
executable file
·195 lines (157 loc) · 4.37 KB
/
lib.js
File metadata and controls
executable file
·195 lines (157 loc) · 4.37 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Array.prototype.uniq=function(){
return this.sort().reduceRight(
function(a,b){
a[0]===b || a.unshift(b);
return a;
}
,[]);
};
jQuery.fn.findAll = function(func){
var result = null;
this.each(function(){
if (func($(this))){
result = result ? result.add($(this)) : $(this);
}
});
return result || $([]);
};
jQuery.fn.attrCopy = function(attr1, attr2){
this.each(function(){
$(this).attr(attr2, $(this).attr(attr1));
});
return this;
}
jQuery.fn.dbg = function(){
debugger;
return this;
}
jQuery.fn.complementURL = function(options){
var setting = $.extend({
protocol: "http",
domain: "",
path: null
}, options);
var url_base = setting.protocol + "://" +
( setting.domain.slice(-1) == "/" ? setting.domain : setting.domain + "/" ) +
( setting.path || "" );
var link_tag = {
a : "href",
img: "src"
};
this.each(function(){
var attr = link_tag[this.tagName.toLowerCase()];
if(attr && $(this).attr(attr).slice(0, 1) == "/"){
$(this).attr(attr, url_base + $(this).attr(attr).substr(1));
}
});
return this;
}
jQuery.fn.attach = function(elmExp, attrs){
var tagInfo;
var tag;
var strAttr = "";
attrs = attrs || [];
if( elmExp.indexOf("#") != -1 ){
tag = elmExp.split("#");
tagInfo = { tag: tag[0], id: tag[1] };
} else if( elmExp.indexOf(".") != -1 ){
tag = elmExp.split(".");
tagInfo = { tag: tag[0], classes: tag[1] };
} else {
tagInfo = { tag: elmExp };
}
attrs.forEach(function(attr){
strAttr += " " + attr.name + "='" + attr.value + "'";
});
return this.append(
"<" + tagInfo.tag + ( tagInfo.id ? " id='" + tagInfo.id + "'" : "") +
( tagInfo.classes ? " class='" + tagInfo.classes + "'" : "") +
strAttr +
">"
).find(tagInfo.tag + ":last");
}
//
// helper function
//
// localStorage.push(String key, String value, Object option)
// function to push 'value' to an array assuming that an object keied with 'key' is
// a JSON string of an array.
//
// default value:
// option : { uniq => false }
//
localStorage.__proto__.push = function(key, value, option){
var setting = $.extend({
uniq : false
}, option);
var array = localStorage
.items(key, { type: "Array" })
.concat(value);
if(setting.uniq){ array = array.uniq(); }
localStorage.setItem(key, JSON.stringify(array));
};
// localStorage.unshift(String key, String value, Object option)
// function to prepend 'value' to an array assuming that an object keied with 'key' is
// a JSON string of an array.
//
// default value:
// option : { uniq => false }
//
localStorage.__proto__.unshift = function(key, value, option){
var setting = $.extend({
uniq : false
}, option);
var array = localStorage
.items(key, { type: "Array" });
if(!setting.only_if_the_gid_is_not_present ||
(setting.only_if_the_gid_is_not_present && !is_the_gid_present(array, value))){
array.unshift(value);
}
if(setting.uniq){ array = array.uniq(); }
localStorage.setItem(key, JSON.stringify(array));
};
function is_the_gid_present(array, value){
var gids = array.map(function(data){ return data.split(',')[0]; });
var new_gid = value.split(',')[0];
return gids.indexOf(new_gid) != -1;
}
//
// helper function
//
// localStorage.items(String key, Object option)
// returns a value associated with the 'key' in the localStorage
//
// option : { type => ("String" as default | "Array") }
//
localStorage.__proto__.items = function(key, option){
var setting = $.extend({
type: "String"
}, option);
var result = localStorage.getItem(key);
if(setting.type == "Array"){
result = JSON.parse(result || "[]");
}
return result;
}
//
// Class BuzzId
// for a permalink of buzz article.
// holding the permalink as google user ID and buzz ID
// recognizing the permalink as:
//
// http://www.google.com/buzz/{gId}/{buzzId}/
//
var BuzzURL = function(permalink){
this.gId = null;
this.buzzId = null;
this.setURL(permalink);
this.__defineSetter__("url", function(value){ this.setURL(value); });
this.__defineGetter__("url",
function(){ return "http://www.google.com/buzz/" + this.gId + "/" + this.buzzId + "/"; }
);
};
BuzzURL.prototype.setURL = function(value){
var match = value.match(/buzz\/(\d+)\/(\w+)/i);
this.gId = match[1];
this.buzzId = match[2];
}