Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/common.js

Parent Directory Parent Directory | Revision Log Revision Log


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

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