$(document).ready(function() { //// スクロールトップボタンの設定 $('#totop').click(function() { $('body,html').animate({scrollTop: 0}, 500); return false; }); //// ネイル情報に対するブックマーク及び可愛いの設定 setNailMarks(); //// カスタムチェックボックスの設定 setImageCheckbox(); }); /** * ネイル情報のマーク(ブックマーク, 可愛い) * クリック時のイベントハンドラ登録 */ var setNailMarks = function() { //// 付加されているクリックイベントを削除 $('.bookmark-toggle').unbind('click'); $('.sympathy-toggle').unbind('click'); /** * ブックマークボタン押下時の処理 * Ajaxでブックマーク登録/解除リクエストを発行する * @return boolean :一覧系では aタグに被せて使用される為、falseを固定で返しておく */ $('.bookmark-toggle').click(function() { var target = $(this); $.ajax({ type: 'GET' , url: '/user/bookmark/toggle.php' , data: {'nail_id': target.attr('data-id'), 'from_uri':location.href} , async: false , cache: false , success: function(data, dataType) { result = eval(data); if(result.result) { target.addClass('active'); } else { if(""!=result.script) { eval(result.script); return false; } // 非ログインで押下 target.removeClass('active'); if('true'==target.attr('disabled-remove')) { target.parent().parent().remove(); } } return false; } , error: function(req, status, error) { return false; } }); return false; }); /** * 可愛いボタンのアクティブ状況を確認・デザインする */ $('.sympathy-toggle').each(function() { var target = $(this); if(!isSupportWebStorage()) { target.addClass('active'); return; } // 保存できない場合は押した後っぽいデザインに var on = hasSympathy(target.attr('data-id')); if(on) { target.addClass('active'); } else { target.removeClass('active'); } }); /** * 可愛いボタン押下時の処理 * Ajaxで可愛い登録/解除リクエストを発行する * @return boolean :一覧系では aタグに被せて使用される為、falseを固定で返しておく */ $('.sympathy-toggle').click(function() { if(!isSupportWebStorage()) { return false; } // 保存できない場合は何もしない var target = $(this); var nid = target.attr('data-id'); var on = hasSympathy(nid); // if(on) { return false; } // これをコメントアウトすれば可愛いの解除が出来る $.ajax({ type: 'GET' , url: '/user/sympathy/toggle.php' , data: {'nail_id': nid, 'on': !on } , async: false , cache: false , success: function(data, dataType) { result = eval(data); if(!on) { target.addClass('active'); addSympathy(nid); } else { target.removeClass('active'); removeSympathy(nid); } target.html(result['count']); return false; } , error: function(req, status, error) { return false; } }); return false; }); }; /** * 一覧グリッド表示の高さ揃えを実行 */ var resizeGrid = function() { $('.grid-list .nail-data').height( $('.nail-data:first-child').width() ); $('.row-list .arrow').height( $('.nail-data:first-child .nail-image').width() ).css('line-height', $('.nail-data:first-child .nail-image').width()+'px'); }; /** * WEBストレージ(ローカルストレージ)がサポートされているか * @return boolean :サポート状況 */ var isSupportWebStorage = function() { return window.localStorage; } /** * 指定IDのネイルが可愛い押下履歴に保存されているか * @param integer id :ネイルID * @return boolean :保存結果 */ var hasSympathy = function(id) { if(!isSupportWebStorage()) { return false; } var ids = localStorage.getItem('_storage_key_sympathy_'); if(null==ids) { return false; } ids = JSON.parse(ids); return -1 != ids.indexOf(id); }; /** * 指定IDのネイルを可愛い押下履歴に保存する * @param integer id :ネイルID */ var addSympathy = function(id) { if(!isSupportWebStorage()) { return; } var ids = localStorage.getItem('_storage_key_sympathy_'); if(null==ids) { ids = [id]; } else { ids = JSON.parse(ids); ids.push(id); } localStorage.setItem('_storage_key_sympathy_', JSON.stringify(ids)); }; /** * 指定IDのネイルを可愛い押下履歴から削除する * @param integer id :ネイルID */ var removeSympathy = function(id) { if(!isSupportWebStorage()) { return; } var ids = localStorage.getItem('_storage_key_sympathy_'); if(null==ids) { return; } ids = JSON.parse(ids); ids.splice(ids.indexOf(id), 1); if(0==ids.length) { localStorage.removeItem('_storage_key_sympathy_'); } else { localStorage.setItem('_storage_key_sympathy_', JSON.stringify(ids)); } }; /** * カスタムチェックボックスの設定を行う */ var setImageCheckbox = function() { $('.cbx>input[type="checkbox"]').change(function(){ if($(this).is(':checked')) { $(this).parent().addClass('active'); } else { $(this).parent().removeClass('active'); } }); };