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

Revision 23108, 19.2 KB checked in by pineray, 11 years ago (diff)

#2349 マウスオーバーで画像を切り替える処理をスマートにする
img タグを対応.

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    eccube.chgImgImageSubmit = function(fileName,imgObj){
91        imgObj.src = fileName;
92    };
93
94    // 郵便番号入力呼び出し.
95    eccube.getAddress = function(php_url, tagname1, tagname2, input1, input2) {
96        var zip1 = document['form1'][tagname1].value;
97        var zip2 = document['form1'][tagname2].value;
98
99        if(zip1.length === 3 && zip2.length === 4) {
100            $.get(
101                php_url,
102                {zip1: zip1, zip2: zip2, input1: input1, input2: input2},
103                function(data) {
104                    var arrData = data.split("|");
105                    if (arrData.length > 1) {
106                        eccube.putAddress(input1, input2, arrData[0], arrData[1], arrData[2]);
107                    } else {
108                        window.alert(data);
109                    }
110                }
111            );
112        } else {
113            window.alert("郵便番号を正しく入力して下さい。");
114        }
115    };
116
117    // 郵便番号から検索した住所を渡す.
118    eccube.putAddress = function(input1, input2, state, city, town) {
119        if(state !== "") {
120            // 項目に値を入力する.
121            document['form1'][input1].selectedIndex = state;
122            document['form1'][input2].value = city + town;
123        }
124    };
125
126    eccube.setFocus = function(name) {
127        if(document['form1'].hasOwnProperty(name)) {
128            document['form1'][name].focus();
129        }
130    };
131
132    // モードとキーを指定してSUBMITを行う。
133    eccube.setModeAndSubmit = function(mode, keyname, keyid) {
134        switch(mode) {
135            case 'delete_category':
136                if(!window.confirm('選択したカテゴリとカテゴリ内の全てのカテゴリを削除します')){
137                    return;
138                }
139                break;
140            case 'delete':
141                if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか?')){
142                    return;
143                }
144                break;
145            case 'confirm':
146                if(!window.confirm('登録しても宜しいですか')){
147                    return;
148                }
149                break;
150            case 'delete_all':
151                if(!window.confirm('検索結果を全て削除しても宜しいですか')){
152                    return;
153                }
154                break;
155            default:
156                break;
157        }
158        document['form1']['mode'].value = mode;
159        if(keyname !== undefined && keyname !== "" && keyid !== undefined && keyid !== "") {
160            document['form1'][keyname].value = keyid;
161        }
162        document['form1'].submit();
163    };
164
165    eccube.fnFormModeSubmit = function(form, mode, keyname, keyid) {
166        var formElement = $("form#" + form);
167        switch(mode) {
168            case 'delete':
169                if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか?')){
170                    return;
171                }
172                break;
173            case 'confirm':
174                if(!window.confirm('登録しても宜しいですか')){
175                    return;
176                }
177                break;
178            case 'regist':
179                if(!window.confirm('登録しても宜しいですか')){
180                    return;
181                }
182                break;
183            default:
184                break;
185        }
186        formElement.find("input[name='mode']").val(mode);
187        if(keyname !== undefined && keyname !== "" && keyid !== undefined && keyid !== "") {
188            formElement.find("*[name=" + keyname + "]").val(keyid);
189        }
190        formElement.submit();
191    };
192
193    eccube.setValueAndSubmit = function(form, key, val) {
194        var formElement = $("form#" + form);
195        formElement.find("*[name=" + key + "]").val(val);
196        formElement.submit();
197        return false;
198    };
199
200    eccube.setValue = function(key, val, form) {
201        if (typeof form === 'undefined') {
202            form = eccube.defaults.formId;
203        }
204        var formElement = $("form#" + form);
205        formElement.find("*[name=" + key + "]").val(val);
206    };
207
208    eccube.changeAction = function(url) {
209        document['form1'].action = url;
210    };
211
212    // ページナビで使用する。
213    eccube.movePage = function(pageno, mode, form) {
214        if (typeof form === 'undefined') {
215            form = eccube.defaults.formId;
216        }
217        var formElement = $("form#" + form);
218        formElement.find("input[name=pageno]").val(pageno);
219        if (typeof mode !== 'undefined') {
220            formElement.find("input[name='mode']").val('search');
221        }
222        formElement.submit();
223    };
224
225    eccube.submitForm = function(form){
226        if (typeof form === 'undefined') {
227            form = eccube.defaults.formId;
228        }
229        $("form#" + form).submit();
230    };
231
232    // ポイント入力制限。
233    eccube.togglePointForm = function() {
234        if(document['form1']['point_check']) {
235            var list = ['use_point'];
236            var color;
237            var flag;
238
239            if(!document['form1']['point_check'][0].checked) {
240                color = "#dddddd";
241                flag = true;
242            } else {
243                color = "";
244                flag = false;
245            }
246
247            var len = list.length;
248            for(var i = 0; i < len; i++) {
249                if(document['form1'][list[i]]) {
250                    var current_color = document['form1'][list[i]].style.backgroundColor;
251                    if (color !== "#dddddd" && (current_color === "#ffe8e8" || current_color === "rgb(255, 232, 232)"))
252                    {
253                        continue;
254                    }
255                    document['form1'][list[i]].disabled = flag;
256                    document['form1'][list[i]].style.backgroundColor = color;
257                }
258            }
259        }
260    };
261
262    // 別のお届け先入力制限。
263    eccube.toggleDeliveryForm = function() {
264        if(!document['form1']) {
265            return;
266        }
267        if(document['form1']['deliv_check']) {
268            var list = [
269                'shipping_name01',
270                'shipping_name02',
271                'shipping_kana01',
272                'shipping_kana02',
273                'shipping_pref',
274                'shipping_zip01',
275                'shipping_zip02',
276                'shipping_addr01',
277                'shipping_addr02',
278                'shipping_tel01',
279                'shipping_tel02',
280                'shipping_tel03'
281            ];
282
283            if(!document['form1']['deliv_check'].checked) {
284                eccube.changeDisabled(list, '#dddddd');
285            } else {
286                eccube.changeDisabled(list, '');
287            }
288        }
289    };
290
291    // 最初に設定されていた色を保存しておく。
292    eccube.savedColor = [];
293
294    eccube.changeDisabled = function(list, color) {
295        var len = list.length;
296
297        for(var i = 0; i < len; i++) {
298            if(document['form1'][list[i]]) {
299                if(color === "") {
300                    // 有効にする。
301                    document['form1'][list[i]].removeAttribute('disabled');
302                    document['form1'][list[i]].style.backgroundColor = eccube.savedColor[list[i]];
303                } else {
304                    // 無効にする。
305                    document['form1'][list[i]].setAttribute('disabled', 'disabled');
306                    eccube.savedColor[list[i]] = document['form1'][list[i]].style.backgroundColor;
307                    document['form1'][list[i]].style.backgroundColor = color;//"#f0f0f0";
308                }
309            }
310        }
311    };
312
313    // ログイン時の入力チェック
314    eccube.checkLoginFormInputted = function(form, emailKey, passKey) {
315        var formElement = $("form#" + form);
316        var checkItems = [];
317
318        if (typeof emailKey === 'undefined') {
319            checkItems[0] = 'login_email';
320        } else {
321            checkItems[0] = emailKey;
322        }
323        if (typeof passKey === 'undefined') {
324            checkItems[1] = 'login_pass';
325        } else {
326            checkItems[1] = passKey;
327        }
328
329        var max = checkItems.length;
330        var errorFlag = false;
331
332        // 必須項目のチェック
333        for(var cnt = 0; cnt < max; cnt++) {
334            if(formElement.find("input[name=" + checkItems[cnt] + "]").val() === "") {
335                errorFlag = true;
336                break;
337            }
338        }
339
340        // 必須項目が入力されていない場合
341        if(errorFlag === true) {
342            window.alert('メールアドレス/パスワードを入力して下さい。');
343            return false;
344        } else {
345            return true;
346        }
347    };
348
349    //親ウィンドウのページを変更する.
350    eccube.changeParentUrl = function(url) {
351        // 親ウィンドウの存在確認
352        if(eccube.isOpener()) {
353            window.opener.location.href = url;
354        } else {
355            window.close();
356        }
357    };
358
359    //文字数をカウントする。
360    //引数1:フォーム名称
361    //引数2:文字数カウント対象
362    //引数3:カウント結果格納対象
363    eccube.countChars = function(form,sch,cnt) {
364        var formElement = $("form#" + form);
365        formElement.find("input[name="+cnt+"]").val(formElement.find("*[name="+sch+"]").val().length);
366    };
367
368    // テキストエリアのサイズを変更する.
369    eccube.toggleRows = function(buttonSelector, textAreaSelector, max, min) {
370        if ($(textAreaSelector).attr('rows') <= min) {
371            $(textAreaSelector).attr('rows', max);
372            $(buttonSelector).text('縮小');
373        } else {
374            $(textAreaSelector).attr('rows', min);
375            $(buttonSelector).text('拡大');
376        }
377    };
378
379    /**
380     * 規格2のプルダウンを設定する.
381     */
382    eccube.setClassCategories = function($form, product_id, $sele1, $sele2, selected_id2) {
383        if ($sele1 && $sele1.length) {
384            var classcat_id1 = $sele1.val() ? $sele1.val() : '';
385            if ($sele2 && $sele2.length) {
386                // 規格2の選択肢をクリア
387                $sele2.children().remove();
388
389                var classcat2;
390
391                // 商品一覧時
392                if (eccube.hasOwnProperty('productsClassCategories')) {
393                    classcat2 = eccube['productsClassCategories'][product_id][classcat_id1];
394                }
395                // 詳細表示時
396                else {
397                    classcat2 = eccube['classCategories'][classcat_id1];
398                }
399
400                // 規格2の要素を設定
401                for (var key in classcat2) {
402                    if (classcat2.hasOwnProperty(key)) {
403                        var id = classcat2[key]['classcategory_id2'];
404                        var name = classcat2[key]['name'];
405                        var option = $('<option />').val(id ? id : '').text(name);
406                        if (id === selected_id2) {
407                            option.attr('selected', true);
408                        }
409                        $sele2.append(option);
410                    }
411                }
412                eccube.checkStock($form, product_id, $sele1.val() ? $sele1.val() : '__unselected2',
413                    $sele2.val() ? $sele2.val() : '');
414            }
415        }
416    };
417
418    /**
419     * 規格の選択状態に応じて, フィールドを設定する.
420     */
421    eccube.checkStock = function($form, product_id, classcat_id1, classcat_id2) {
422
423        classcat_id2 = classcat_id2 ? classcat_id2 : '';
424
425        var classcat2;
426
427        // 商品一覧時
428        if (eccube.hasOwnProperty('productsClassCategories')) {
429            classcat2 = eccube['productsClassCategories'][product_id][classcat_id1]['#' + classcat_id2];
430        }
431        // 詳細表示時
432        else {
433            classcat2 = eccube['classCategories'][classcat_id1]['#' + classcat_id2];
434        }
435
436        // 商品コード
437        var $product_code_default = $form.find('[id^=product_code_default]');
438        var $product_code_dynamic = $form.find('[id^=product_code_dynamic]');
439        if (classcat2 && typeof classcat2['product_code'] !== 'undefined') {
440            $product_code_default.hide();
441            $product_code_dynamic.show();
442            $product_code_dynamic.text(classcat2['product_code']);
443        } else {
444            $product_code_default.show();
445            $product_code_dynamic.hide();
446        }
447
448        // 在庫(品切れ)
449        var $cartbtn_default = $form.find('[id^=cartbtn_default]');
450        var $cartbtn_dynamic = $form.find('[id^=cartbtn_dynamic]');
451        if (classcat2 && classcat2['stock_find'] === false) {
452
453            $cartbtn_dynamic.text('申し訳ございませんが、只今品切れ中です。').show();
454            $cartbtn_default.hide();
455        } else {
456            $cartbtn_dynamic.hide();
457            $cartbtn_default.show();
458        }
459
460        // 通常価格
461        var $price01_default = $form.find('[id^=price01_default]');
462        var $price01_dynamic = $form.find('[id^=price01_dynamic]');
463        if (classcat2 && typeof classcat2['price01'] !== 'undefined' && String(classcat2['price01']).length >= 1) {
464
465            $price01_dynamic.text(classcat2['price01']).show();
466            $price01_default.hide();
467        } else {
468            $price01_dynamic.hide();
469            $price01_default.show();
470        }
471
472        // 販売価格
473        var $price02_default = $form.find('[id^=price02_default]');
474        var $price02_dynamic = $form.find('[id^=price02_dynamic]');
475        if (classcat2 && typeof classcat2['price02'] !== 'undefined' && String(classcat2['price02']).length >= 1) {
476
477            $price02_dynamic.text(classcat2['price02']).show();
478            $price02_default.hide();
479        } else {
480            $price02_dynamic.hide();
481            $price02_default.show();
482        }
483
484        // ポイント
485        var $point_default = $form.find('[id^=point_default]');
486        var $point_dynamic = $form.find('[id^=point_dynamic]');
487        if (classcat2 && typeof classcat2['point'] !== 'undefined' && String(classcat2['point']).length >= 1) {
488
489            $point_dynamic.text(classcat2['point']).show();
490            $point_default.hide();
491        } else {
492            $point_dynamic.hide();
493            $point_default.show();
494        }
495
496        // 商品規格
497        var $product_class_id_dynamic = $form.find('[id^=product_class_id]');
498        if (classcat2 && typeof classcat2['product_class_id'] !== 'undefined' && String(classcat2['product_class_id']).length >= 1) {
499            $product_class_id_dynamic.val(classcat2['product_class_id']);
500        } else {
501            $product_class_id_dynamic.val('');
502        }
503    };
504
505    // グローバルに使用できるようにする
506    window.eccube = eccube;
507
508    /**
509     * Initialize.
510     */
511    $(function() {
512        // 規格1選択時
513        $('select[name=classcategory_id1]')
514            .change(function() {
515                var $form = $(this).parents('form');
516                var product_id = $form.find('input[name=product_id]').val();
517                var $sele1 = $(this);
518                var $sele2 = $form.find('select[name=classcategory_id2]');
519
520                // 規格1のみの場合
521                if (!$sele2.length) {
522                    eccube.checkStock($form, product_id, $sele1.val(), '0');
523                    // 規格2ありの場合
524                } else {
525                    eccube.setClassCategories($form, product_id, $sele1, $sele2);
526                }
527            });
528
529        // 規格2選択時
530        $('select[name=classcategory_id2]')
531            .change(function() {
532                var $form = $(this).parents('form');
533                var product_id = $form.find('input[name=product_id]').val();
534                var $sele1 = $form.find('select[name=classcategory_id1]');
535                var $sele2 = $(this);
536                eccube.checkStock($form, product_id, $sele1.val(), $sele2.val());
537            });
538
539        // マウスオーバーで画像切り替え
540        $(".hover_change_image").each(function(){
541            var target = $(this);
542            var srcOrig = target.attr("src");
543            var srcOver = srcOrig.substr(0, srcOrig.lastIndexOf('.')) + '_on' + srcOrig.substr(srcOrig.lastIndexOf('.'));
544            target.hover(
545                function(){
546                    target.attr("src", srcOver);
547                },
548                function(){
549                    target.attr("src", srcOrig);
550                }
551            );
552        });
553    });
554})(window);
Note: See TracBrowser for help on using the repository browser.