Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/common.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations) (download) (as text)
Sat Dec 5 02:36:36 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 9991 byte(s)
ライセンス表記
1
2 // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>
3 // License http://www.opensource.org/licenses/mit-license.html
4
5 function addListener(elm, type, func)
6 {
7 if(!elm) { return false; }
8 if (elm.addEventListener) {
9 elm.addEventListener(type, func, false);
10 }else if (elm.attachEvent) {
11 elm.attachEvent('on' + type, func);
12 }else {
13 return false;
14 }
15 return true;
16 }
17
18 if (!Object.defineProperty) {
19 if (Object.__defineGetter__ && Object.__defineSetter__) {
20 Object.defineProperty = function (obj, propName, desc) {
21 if (desc.get) {
22 obj.__defineGetter__(propName, desc.get);
23 }
24 if (desc.set) {
25 obj.__defineSetter__(propName, desc.set);
26 }
27 }
28 }
29 }
30
31 function clearFormElementsValues(form, id)
32 {
33 var idlen = id.length;
34 if (form && form.elements.length) {
35 var elems = form.elements;
36 var len = elems.length;
37 for (var i=0; i<len; ++i) {
38 var ele = elems[i];
39 if (ele.name.length < idlen) {
40 continue;
41 }
42 if (ele.name.substring(0, idlen) == id) {
43 ele.value = '';
44 }
45 }
46 }
47 }
48
49 function findCheckedElement(elems)
50 {
51 if (elems) {
52 return $A(elems).find( function (val, idx) { return val.checked; });
53 }else {
54 return false;
55 }
56 }
57
58 function searchAncestor(elem, func) {
59 do {
60 if (func(elem.parentNode)) {
61 return elem.parentNode;
62 }
63 }while (elem = elem.parentNode);
64 return false;
65 }
66
67 function selectOptionByText(select, text)
68 {
69 var options = select.options;
70 for (var i=0; i<select.length; ++i) {
71 if (options[i].text == text) {
72 select.selectedIndex = i;
73 break;
74 }
75 }
76 }
77
78 function zip2addr(relativePath, zipInput, prefInputName, addrInputName)
79 {
80 var zipCode = zipInput.value.strip();
81 if (zipCode == '') {
82 return;
83 }
84
85 new Ajax.Request(
86 relativePath + "zip/zip.php",
87 {
88 onSuccess : function(request) {
89 var data = request.responseText.evalJSON();
90 if (data[0]) {
91 var frm = zipInput.form;
92 var prefInput = frm[prefInputName];
93 if (prefInput.type == 'select-one') {
94 selectOptionByText(prefInput, data[6]);
95 }else {
96 }
97 var addrInput = frm[addrInputName];
98 if (addrInput.value.strip() == '') {
99 var str = '';
100 for (var i=6; i<9; ++i) {
101 str += data[i];
102 }
103 addrInput.value = str;
104 }
105 }
106 },
107 onFailure : function() {
108 alert("failed");
109 },
110 "method": "get",
111 "parameters" : "zipcode=" + zipCode
112 }
113 );
114
115 }
116
117 // http://daisuke-watanabe.com/12/
118 function addFigure(str)
119 {
120 var num = new String(str).replace(/,/g, "");
121 while (num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
122 return num;
123 }
124
125 function mergeRepeativeValueRows(table, rowRange, columnIndexs)
126 {
127 columnIndexs.sort(function(a,b) {
128 return a <= b ? 1 : -1;
129 });
130 var beginRowIndex = rowRange[0];
131 var endRowIndex = rowRange[1];
132 if (endRowIndex == -1) {
133 endRowIndex = table.rows.length;
134 }
135 for (i=0; i<columnIndexs.length; ++i) {
136 var ci = columnIndexs[i];
137 // count
138 var spanCounts = new Array();
139 var spanCount = 1;
140 for (k=beginRowIndex+1; k<endRowIndex; ++k) {
141 var prevRow = table.rows[k-1];
142 var curRow = table.rows[k];
143 if (1
144 && ci < prevRow.cells.length
145 && ci < curRow.cells.length
146 ) {
147 var prevCell = prevRow.cells[ci];
148 var curCell = curRow.cells[ci];
149 if (prevCell.tagName == "TD" && curCell.tagName == "TD") {
150 var prevVal = prevCell.innerHTML;
151 var curVal = curCell.innerHTML;
152 if (prevVal == curVal) {
153 ++spanCount;
154 continue;
155 }
156 }
157 }
158 spanCounts.push(spanCount);
159 spanCount = 1;
160 }
161 spanCounts.push(spanCount);
162 // merge
163 var rowIdx = beginRowIndex;
164 for (k=0; k<spanCounts.length; ++k) {
165 var spanCount = spanCounts[k];
166 if (spanCount == 1) {
167 ++rowIdx;
168 }else {
169 for (l=rowIdx+1; l<rowIdx+spanCount; ++l) {
170 table.rows[l].deleteCell(ci);
171 }
172 table.rows[rowIdx].cells[ci].rowSpan = spanCount;
173 rowIdx += spanCount;
174 }
175 }
176 }
177 }
178
179 function copyTableRow(from, to)
180 {
181 if (Prototype.Browser.IE) {
182 for (var i=0; i<from.cells.length; ++i) {
183 var td = to.insertCell(-1);
184 td.innerHTML = from.cells[i].innerHTML;
185 }
186 }else {
187 to.innerHTML = from.innerHTML;
188 }
189 }
190
191 // from : http://mr-goofy.blogspot.com/2009/06/getelementsbyclassname_16.html
192 /***********************************************
193 * ������������������������������������������Element���������
194 ***********************************************/
195 var getElementsByClassName = (function(classname, tagname){
196
197 //������������������������function���������
198 var regClass = function(element, classname){
199 var classElements = new Array();
200 var regexe = new RegExp("\\b" + classname + "\\b");
201 for( i = 0; i < element.length; i++ ) {
202 if( regexe.exec(element[i].className) ) {
203 classElements.push(element[i]);
204 }
205 }
206 return classElements;
207 }
208 //IE���������
209 if( document.all ){
210 return function(classname, tagname){
211 if (arguments.length == 1) {
212 element = document.all;
213 }
214 else{
215 element = document.getElementsByTagName(tagname);
216 }
217 return regClass(element,classname);
218 };
219 }
220 //IE���������������
221 else{
222 return function(classname, tagname){
223 if (arguments.length == 1) {
224 tagname = "*";
225 }
226 var element = document.getElementsByTagName(tagname);
227 return regClass(element,classname);
228 };
229 }
230
231 })();
232
233 /**
234 * Function : dump()
235 * Arguments: The data - array,hash(associative array),object
236 * The level - OPTIONAL
237 * Returns : The textual representation of the array.
238 * This function was inspired by the print_r function of PHP.
239 * This will accept some data as the argument and return a
240 * text that will be a more readable version of the
241 * array/hash/object that is given.
242 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
243 */
244 function dump(arr,level) {
245 var dumped_text = "";
246 if(!level) level = 0;
247
248 //The padding given at the beginning of the line.
249 var level_padding = "";
250 for(var j=0;j<level+1;j++) level_padding += " ";
251
252 if(typeof(arr) == 'object') { //Array/Hashes/Objects
253 for(var item in arr) {
254 var value = arr[item];
255
256 if(typeof(value) == 'object') { //If it is an array,
257 dumped_text += level_padding + "'" + item + "' ...\n";
258 dumped_text += dump(value,level+1);
259 } else {
260 dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
261 }
262 }
263 } else { //Stings/Chars/Numbers etc.
264 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
265 }
266 return dumped_text;
267 }
268
269
270 function array_key_exists ( key, search ) {
271 // http://kevin.vanzonneveld.net
272 // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
273 // + improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
274 // * example 1: array_key_exists('kevin', {'kevin': 'van Zonneveld'});
275 // * returns 1: true
276
277 // input sanitation
278 if( !search || (search.constructor !== Array && search.constructor !== Object) ){
279 return false;
280 }
281
282 return key in search;
283 }
284
285 function radioInputTag($name, $value, $checked)
286 {
287 var txt = "<input type='radio' name='" + $name + "' value='" + $value + "' ";
288 if ($checked) {
289 txt += "checked";
290 }
291 txt += " />";
292 return txt;
293 }
294
295
296 function sortHash(hash, cmp)
297 {
298 var arr = [];
299 var keys = hash.keys();
300 for (var i=0; i<keys.length; ++i) {
301 var key = keys[i];
302 var value = hash.get(key);
303 var obj = {"key":key, "value":value};
304 arr.push(obj);
305 }
306 arr.sort(function (a,b) { return cmp(a.value, b.value); });
307 var ret = new Hash();
308 for (var i=0; i<arr.length; ++i) {
309 ret.set(arr[i].key, arr[i].value);
310 }
311 return ret;
312 }
313
314 // http://doc.infosnel.nl/javascript_trim.html
315 function trim(s)
316 {
317 var l=0; var r=s.length -1;
318 while(l < s.length && s[l] == ' ')
319 { l++; }
320 while(r > l && s[r] == ' ')
321 { r-=1; }
322 return s.substring(l, r+1);
323 }
324
325
326 function splitTrim(str, separator)
327 {
328 var arr = str.split(separator);
329 for (var i=0; i<arr.length; ++i) {
330 arr[i] = trim(arr[i]);
331 }
332 return arr;
333 }
334
335 // original : http://blog.mikuriya.biz/archives/286
336 function moveSelectedOptions(fromSelect, toSelect) {
337 var from_options = fromSelect.options;
338 var to_options = toSelect.options;
339
340 for (var i=0; i<from_options.length; ++i) {
341 if (!from_options[i].selected || !from_options[i].value) {
342 continue;
343 }
344 var addFlag = true;
345 for (var j=0; j<to_options.length; ++j) {
346 if (to_options[j].value == from_options[i].value) {
347 addFlag = false;
348 break;
349 }
350 }
351
352 if (addFlag) {
353 to_options[to_options.length] = from_options[i];
354 }
355 --i;
356 }
357 }
358
359 function relocateSelectedOptions(select, dir) {
360 var options = select.options;
361 if (dir == 0) {
362 return;
363 }
364 if (dir < 0) {
365 for (var i=0, len=options.length; i<len; ++i) {
366 if (options[i].selected) {
367 if (i+dir < 0) {
368 break;
369 }
370 select.insertBefore(options[i], options[i+dir]);
371 }
372 }
373 }else {
374 for (var len=options.length,i=len-1; i>=0; --i) {
375 if (options[i].selected) {
376 if (i+dir >= len) {
377 break;
378 }
379 select.insertBefore(options[i], options[i+dir].nextSibling);
380 }
381 }
382 }
383
384 }
385
386 function toggleVisibility(elem)
387 {
388 if (elem.style.visibility == 'hidden') {
389 elem.style.visibility = 'visible';
390 // setAutoFocus();
391 }else {
392 elem.style.visibility = 'hidden';
393 }
394
395 }
396
397 function a(p)
398 {
399 if (typeof p == "object") {
400 alert(Object.toJSON(p));
401 }else {
402 alert(p);
403 }
404 }
405
406 function collectMember(arr, name) {
407 var values = [];
408 for (var i=0; i<arr.length; ++i) {
409 values.push(arr[i][name]);
410 }
411 return values;
412 }
413

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26