source: branches/version-2_13-dev/html/js/eccube.js @ 23135

Revision 23135, 19.3 KB checked in by pineray, 11 years ago (diff)

#2342 JavaScript?のグローバルな宣言を減らす
ページ移動の関数でモード指定の扱いに不備があった。

Line 
1/*
2 * This file is part of EC-CUBE
3 *
4 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
5 *
6 * http://www.lockon.co.jp/
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 */
22
23(function( window, undefined ){
24
25
26    // 名前空間の重複を防ぐ
27    if (window.eccube === undefined) {
28        window.eccube = {};
29    }
30
31    var eccube = window.eccube;
32
33    eccube.defaults = {
34        formId:'form1',
35        windowFeatures:{
36            scrollbars:'yes',
37            resizable:'yes',
38            toolbar:'no',
39            location:'no',
40            directories:'no',
41            status:'no',
42            focus:true,
43            formTarget:''
44        }
45    };
46
47    eccube.openWindow = function(URL,name,width,height,option) {
48        var features = "width="+width+",height="+height;
49        if (option === undefined) {
50            option = eccube.defaults.windowFeatures;
51        } else {
52            option = $.extend(eccube.defaults.windowFeatures, option);
53        }
54        features = features + ",scrollbars=" + option.scrollbars +
55                ",resizable=" + option.resizable +
56                ",toolbar=" + option.toolbar +
57                ",location=" + option.location +
58                ",directories=" + option.directories +
59                ",status=" + option.status;
60        if (option.hasOwnProperty('menubar')) {
61            features = features + ",menubar=" + option['menubar'];
62        }
63        var WIN = window.open(URL,name,features);
64        if (option.formTarget !== "") {
65            document.forms[option.formTarget].target = name;
66        }
67        if (option.focus) {
68            WIN.focus();
69        }
70    };
71
72    // 親ウィンドウの存在確認.
73    eccube.isOpener = function() {
74        var ua = navigator.userAgent;
75        if( window.opener ) {
76            if( ua.indexOf('MSIE 4') !== -1 && ua.indexOf('Win') !== -1 ) {
77                if (window.opener.hasOwnProperty('closed')) {
78                    return !window.opener['closed'];
79                } else {
80                    return false;
81                }
82            } else {
83                return typeof window.opener.document === 'object';
84            }
85        } else {
86            return false;
87        }
88    };
89
90    // 郵便番号入力呼び出し.
91    eccube.getAddress = function(php_url, tagname1, tagname2, input1, input2) {
92        var zip1 = document['form1'][tagname1].value;
93        var zip2 = document['form1'][tagname2].value;
94
95        if(zip1.length === 3 && zip2.length === 4) {
96            $.get(
97                php_url,
98                {zip1: zip1, zip2: zip2, input1: input1, input2: input2},
99                function(data) {
100                    var arrData = data.split("|");
101                    if (arrData.length > 1) {
102                        eccube.putAddress(input1, input2, arrData[0], arrData[1], arrData[2]);
103                    } else {
104                        window.alert(data);
105                    }
106                }
107            );
108        } else {
109            window.alert("郵便番号を正しく入力して下さい。");
110        }
111    };
112
113    // 郵便番号から検索した住所を渡す.
114    eccube.putAddress = function(input1, input2, state, city, town) {
115        if(state !== "") {
116            // 項目に値を入力する.
117            document['form1'][input1].selectedIndex = state;
118            document['form1'][input2].value = city + town;
119        }
120    };
121
122    eccube.setFocus = function(name) {
123        if(document['form1'].hasOwnProperty(name)) {
124            document['form1'][name].focus();
125        }
126    };
127
128    // モードとキーを指定してSUBMITを行う。
129    eccube.setModeAndSubmit = function(mode, keyname, keyid) {
130        switch(mode) {
131            case 'delete_category':
132                if(!window.confirm('選択したカテゴリとカテゴリ内の全てのカテゴリを削除します')){
133                    return;
134                }
135                break;
136            case 'delete':
137                if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか?')){
138                    return;
139                }
140                break;
141            case 'confirm':
142                if(!window.confirm('登録しても宜しいですか')){
143                    return;
144                }
145                break;
146            case 'delete_all':
147                if(!window.confirm('検索結果を全て削除しても宜しいですか')){
148                    return;
149                }
150                break;
151            default:
152                break;
153        }
154        document['form1']['mode'].value = mode;
155        if(keyname !== undefined && keyname !== "" && keyid !== undefined && keyid !== "") {
156            document['form1'][keyname].value = keyid;
157        }
158        document['form1'].submit();
159    };
160
161    eccube.fnFormModeSubmit = function(form, mode, keyname, keyid) {
162        var formElement = $("form#" + form);
163        switch(mode) {
164            case 'delete':
165                if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか?')){
166                    return;
167                }
168                break;
169            case 'confirm':
170                if(!window.confirm('登録しても宜しいですか')){
171                    return;
172                }
173                break;
174            case 'regist':
175                if(!window.confirm('登録しても宜しいですか')){
176                    return;
177                }
178                break;
179            default:
180                break;
181        }
182        formElement.find("input[name='mode']").val(mode);
183        if(keyname !== undefined && keyname !== "" && keyid !== undefined && keyid !== "") {
184            formElement.find("*[name=" + keyname + "]").val(keyid);
185        }
186        formElement.submit();
187    };
188
189    eccube.setValueAndSubmit = function(form, key, val, msg) {
190        var ret;
191        if (msg !== undefined) {
192            ret = window.confirm(msg);
193        } else {
194            ret = true;
195        }
196        if (ret) {
197            var formElement = $("form#" + form);
198            formElement.find("*[name=" + key + "]").val(val);
199            formElement.submit();
200        }
201        return false;
202    };
203
204    eccube.setValue = function(key, val, form) {
205        if (typeof form === 'undefined') {
206            form = eccube.defaults.formId;
207        }
208        var formElement = $("form#" + form);
209        formElement.find("*[name=" + key + "]").val(val);
210    };
211
212    eccube.changeAction = function(url) {
213        document['form1'].action = url;
214    };
215
216    // ページナビで使用する。
217    eccube.movePage = function(pageno, mode, form) {
218        if (form === undefined) {
219            form = eccube.defaults.formId;
220        }
221        var formElement = $("form#" + form);
222        formElement.find("input[name=pageno]").val(pageno);
223        if (mode !== undefined) {
224            formElement.find("input[name='mode']").val(mode);
225        }
226        formElement.submit();
227    };
228
229    eccube.submitForm = function(form){
230        if (typeof form === 'undefined') {
231            form = eccube.defaults.formId;
232        }
233        $("form#" + form).submit();
234    };
235
236    // ポイント入力制限。
237    eccube.togglePointForm = function() {
238        if(document['form1']['point_check']) {
239            var list = ['use_point'];
240            var color;
241            var flag;
242
243            if(!document['form1']['point_check'][0].checked) {
244                color = "#dddddd";
245                flag = true;
246            } else {
247                color = "";
248                flag = false;
249            }
250
251            var len = list.length;
252            for(var i = 0; i < len; i++) {
253                if(document['form1'][list[i]]) {
254                    var current_color = document['form1'][list[i]].style.backgroundColor;
255                    if (color !== "#dddddd" && (current_color === "#ffe8e8" || current_color === "rgb(255, 232, 232)"))
256                    {
257                        continue;
258                    }
259                    document['form1'][list[i]].disabled = flag;
260                    document['form1'][list[i]].style.backgroundColor = color;
261                }
262            }
263        }
264    };
265
266    // 別のお届け先入力制限。
267    eccube.toggleDeliveryForm = function() {
268        if(!document['form1']) {
269            return;
270        }
271        if(document['form1']['deliv_check']) {
272            var list = [
273                'shipping_name01',
274                'shipping_name02',
275                'shipping_kana01',
276                'shipping_kana02',
277                'shipping_pref',
278                'shipping_zip01',
279                'shipping_zip02',
280                'shipping_addr01',
281                'shipping_addr02',
282                'shipping_tel01',
283                'shipping_tel02',
284                'shipping_tel03'
285            ];
286
287            if(!document['form1']['deliv_check'].checked) {
288                eccube.changeDisabled(list, '#dddddd');
289            } else {
290                eccube.changeDisabled(list, '');
291            }
292        }
293    };
294
295    // 最初に設定されていた色を保存しておく。
296    eccube.savedColor = [];
297
298    eccube.changeDisabled = function(list, color) {
299        var len = list.length;
300
301        for(var i = 0; i < len; i++) {
302            if(document['form1'][list[i]]) {
303                if(color === "") {
304                    // 有効にする。
305                    document['form1'][list[i]].removeAttribute('disabled');
306                    document['form1'][list[i]].style.backgroundColor = eccube.savedColor[list[i]];
307                } else {
308                    // 無効にする。
309                    document['form1'][list[i]].setAttribute('disabled', 'disabled');
310                    eccube.savedColor[list[i]] = document['form1'][list[i]].style.backgroundColor;
311                    document['form1'][list[i]].style.backgroundColor = color;//"#f0f0f0";
312                }
313            }
314        }
315    };
316
317    // ログイン時の入力チェック
318    eccube.checkLoginFormInputted = function(form, emailKey, passKey) {
319        var formElement = $("form#" + form);
320        var checkItems = [];
321
322        if (typeof emailKey === 'undefined') {
323            checkItems[0] = 'login_email';
324        } else {
325            checkItems[0] = emailKey;
326        }
327        if (typeof passKey === 'undefined') {
328            checkItems[1] = 'login_pass';
329        } else {
330            checkItems[1] = passKey;
331        }
332
333        var max = checkItems.length;
334        var errorFlag = false;
335
336        // 必須項目のチェック
337        for(var cnt = 0; cnt < max; cnt++) {
338            if(formElement.find("input[name=" + checkItems[cnt] + "]").val() === "") {
339                errorFlag = true;
340                break;
341            }
342        }
343
344        // 必須項目が入力されていない場合
345        if(errorFlag === true) {
346            window.alert('メールアドレス/パスワードを入力して下さい。');
347            return false;
348        } else {
349            return true;
350        }
351    };
352
353    //親ウィンドウのページを変更する.
354    eccube.changeParentUrl = function(url) {
355        // 親ウィンドウの存在確認
356        if(eccube.isOpener()) {
357            window.opener.location.href = url;
358        } else {
359            window.close();
360        }
361    };
362
363    //文字数をカウントする。
364    //引数1:フォーム名称
365    //引数2:文字数カウント対象
366    //引数3:カウント結果格納対象
367    eccube.countChars = function(form,sch,cnt) {
368        var formElement = $("form#" + form);
369        formElement.find("input[name="+cnt+"]").val(formElement.find("*[name="+sch+"]").val().length);
370    };
371
372    // テキストエリアのサイズを変更する.
373    eccube.toggleRows = function(buttonSelector, textAreaSelector, max, min) {
374        if ($(textAreaSelector).attr('rows') <= min) {
375            $(textAreaSelector).attr('rows', max);
376            $(buttonSelector).text('縮小');
377        } else {
378            $(textAreaSelector).attr('rows', min);
379            $(buttonSelector).text('拡大');
380        }
381    };
382
383    /**
384     * 規格2のプルダウンを設定する.
385     */
386    eccube.setClassCategories = function($form, product_id, $sele1, $sele2, selected_id2) {
387        if ($sele1 && $sele1.length) {
388            var classcat_id1 = $sele1.val() ? $sele1.val() : '';
389            if ($sele2 && $sele2.length) {
390                // 規格2の選択肢をクリア
391                $sele2.children().remove();
392
393                var classcat2;
394
395                // 商品一覧時
396                if (eccube.hasOwnProperty('productsClassCategories')) {
397                    classcat2 = eccube['productsClassCategories'][product_id][classcat_id1];
398                }
399                // 詳細表示時
400                else {
401                    classcat2 = eccube['classCategories'][classcat_id1];
402                }
403
404                // 規格2の要素を設定
405                for (var key in classcat2) {
406                    if (classcat2.hasOwnProperty(key)) {
407                        var id = classcat2[key]['classcategory_id2'];
408                        var name = classcat2[key]['name'];
409                        var option = $('<option />').val(id ? id : '').text(name);
410                        if (id === selected_id2) {
411                            option.attr('selected', true);
412                        }
413                        $sele2.append(option);
414                    }
415                }
416                eccube.checkStock($form, product_id, $sele1.val() ? $sele1.val() : '__unselected2',
417                    $sele2.val() ? $sele2.val() : '');
418            }
419        }
420    };
421
422    /**
423     * 規格の選択状態に応じて, フィールドを設定する.
424     */
425    eccube.checkStock = function($form, product_id, classcat_id1, classcat_id2) {
426
427        classcat_id2 = classcat_id2 ? classcat_id2 : '';
428
429        var classcat2;
430
431        // 商品一覧時
432        if (eccube.hasOwnProperty('productsClassCategories')) {
433            classcat2 = eccube['productsClassCategories'][product_id][classcat_id1]['#' + classcat_id2];
434        }
435        // 詳細表示時
436        else {
437            classcat2 = eccube['classCategories'][classcat_id1]['#' + classcat_id2];
438        }
439
440        // 商品コード
441        var $product_code_default = $form.find('[id^=product_code_default]');
442        var $product_code_dynamic = $form.find('[id^=product_code_dynamic]');
443        if (classcat2 && typeof classcat2['product_code'] !== 'undefined') {
444            $product_code_default.hide();
445            $product_code_dynamic.show();
446            $product_code_dynamic.text(classcat2['product_code']);
447        } else {
448            $product_code_default.show();
449            $product_code_dynamic.hide();
450        }
451
452        // 在庫(品切れ)
453        var $cartbtn_default = $form.find('[id^=cartbtn_default]');
454        var $cartbtn_dynamic = $form.find('[id^=cartbtn_dynamic]');
455        if (classcat2 && classcat2['stock_find'] === false) {
456
457            $cartbtn_dynamic.text('申し訳ございませんが、只今品切れ中です。').show();
458            $cartbtn_default.hide();
459        } else {
460            $cartbtn_dynamic.hide();
461            $cartbtn_default.show();
462        }
463
464        // 通常価格
465        var $price01_default = $form.find('[id^=price01_default]');
466        var $price01_dynamic = $form.find('[id^=price01_dynamic]');
467        if (classcat2 && typeof classcat2['price01'] !== 'undefined' && String(classcat2['price01']).length >= 1) {
468
469            $price01_dynamic.text(classcat2['price01']).show();
470            $price01_default.hide();
471        } else {
472            $price01_dynamic.hide();
473            $price01_default.show();
474        }
475
476        // 販売価格
477        var $price02_default = $form.find('[id^=price02_default]');
478        var $price02_dynamic = $form.find('[id^=price02_dynamic]');
479        if (classcat2 && typeof classcat2['price02'] !== 'undefined' && String(classcat2['price02']).length >= 1) {
480
481            $price02_dynamic.text(classcat2['price02']).show();
482            $price02_default.hide();
483        } else {
484            $price02_dynamic.hide();
485            $price02_default.show();
486        }
487
488        // ポイント
489        var $point_default = $form.find('[id^=point_default]');
490        var $point_dynamic = $form.find('[id^=point_dynamic]');
491        if (classcat2 && typeof classcat2['point'] !== 'undefined' && String(classcat2['point']).length >= 1) {
492
493            $point_dynamic.text(classcat2['point']).show();
494            $point_default.hide();
495        } else {
496            $point_dynamic.hide();
497            $point_default.show();
498        }
499
500        // 商品規格
501        var $product_class_id_dynamic = $form.find('[id^=product_class_id]');
502        if (classcat2 && typeof classcat2['product_class_id'] !== 'undefined' && String(classcat2['product_class_id']).length >= 1) {
503            $product_class_id_dynamic.val(classcat2['product_class_id']);
504        } else {
505            $product_class_id_dynamic.val('');
506        }
507    };
508
509    // グローバルに使用できるようにする
510    window.eccube = eccube;
511
512    /**
513     * Initialize.
514     */
515    $(function() {
516        // 規格1選択時
517        $('select[name=classcategory_id1]')
518            .change(function() {
519                var $form = $(this).parents('form');
520                var product_id = $form.find('input[name=product_id]').val();
521                var $sele1 = $(this);
522                var $sele2 = $form.find('select[name=classcategory_id2]');
523
524                // 規格1のみの場合
525                if (!$sele2.length) {
526                    eccube.checkStock($form, product_id, $sele1.val(), '0');
527                    // 規格2ありの場合
528                } else {
529                    eccube.setClassCategories($form, product_id, $sele1, $sele2);
530                }
531            });
532
533        // 規格2選択時
534        $('select[name=classcategory_id2]')
535            .change(function() {
536                var $form = $(this).parents('form');
537                var product_id = $form.find('input[name=product_id]').val();
538                var $sele1 = $form.find('select[name=classcategory_id1]');
539                var $sele2 = $(this);
540                eccube.checkStock($form, product_id, $sele1.val(), $sele2.val());
541            });
542
543        // マウスオーバーで画像切り替え
544        $(".hover_change_image").each(function(){
545            var target = $(this);
546            var srcOrig = target.attr("src");
547            var srcOver = srcOrig.substr(0, srcOrig.lastIndexOf('.')) + '_on' + srcOrig.substr(srcOrig.lastIndexOf('.'));
548            target.hover(
549                function(){
550                    target.attr("src", srcOver);
551                },
552                function(){
553                    target.attr("src", srcOrig);
554                }
555            );
556        });
557    });
558})(window);
Note: See TracBrowser for help on using the repository browser.