; (function (window, $) {
// 変数を定義する
var $body, $search, $keyword, $result, $li,
currentItem,
nextItem,
rsLength,
timer,
ajax,
index, // 2014/04/09追加
temp = '',
apiScheme = location.protocol + "//",
maxLength = 10;
// DOM Ready
$(function () {
// 要素をキャッシュする
$body = $('body');
$search = $('.search');
$keyword = $('.input'); // 2014/4/9変更
$fmSelect = $('.fmselect')[0];
$result = $('
'); // 2014/4/9変更
$('#header, #sidebar .search').after($result);// 2014/4/9変更
// 2014/4/9 追加
// 何番目の検索フォームにいるかを確認
$keyword.on('focus', function () {
index = $keyword.index(this);
});
// 入力フィールドにフォーカスした際の処理
var focusHandler = function (ev) {
// 変数を定義する
var self = this,
val = '';
// 表示を切り替える
if ($keyword.attr("placeholder") == self.value) {
self.value = "";
}
// タイマーでクローラーを回す
timer = setInterval(function () {
val = self.value;
if (temp !== val) { // 入力内容に変更があるかを確認
temp = val;
if (val.length) { // 文字が入力されいるかを確認
//動画はサジェストに対応していない
if (!/^Video/.test($fmSelect.value)) crawl(val);
} else {
clearResult();
stopCrawl();
}
}
}, 10);
};
// 入力フィールドからブラーした際の処理
var blurHandler = function (ev) {
// クローラーを止める
stopCrawl();
clearTimeout(timer);
};
// イベントリスナーを登録する
$keyword.on({
'focus': focusHandler,
'blur': blurHandler,
'keyup': selectHandler
});
$(document).on('mouseenter', '.result li', mouseenterHandler).on('click', clearResult);
$result.on('click', '.closeList a', function (ev) { // サジェストリストの「閉じる」ボタンをクリックした際の処理
// 標準の挙動を止める
ev.preventDefault();
ev.stopPropagation();
// 入力欄にフォーカスする
$keyword.blur().focus();
// サジェスト一覧をクリアする
clearResult();
}).on('click', '.item a', function (ev) { // サジェストリストの候補をクリックした際の処理
// イベントの伝播を止める
ev.stopPropagation();
}).on('click', null, function (ev) { // 検索画面を閉じる処理
$keyword[0].value = null;
clearResult();
});
//サジェストとブラウザ組込のオートコンプリートが重ならないように
$($fmSelect).on('change', function () {
$keyword.attr('autocomplete', /^Video/.test($fmSelect.value) ? 'on' : 'off')
});
});
// サジェストデータを表示するコールバック関数(グローバル)
window.suggestCallback = function (json) {
// 一覧を生成して流し込む
var suggest = json.Suggest,
inputValue = encodeURIComponent(json.Keyword),
html = '',
item;
var searchUrl;
var searchAction;
var searchOption = $fmSelect.value;
var corner = 'top';
rsLength = suggest.length;
// 追加 選択したプルダウンでリンク先変更追加分
if (index == 0) { // 2014/4/9追加 サイドバー向けにリンク先を指定
switch (searchOption) {
case "Artist":
searchAction = "/Search/Artist?Keyword=";
break;
case "Music":
searchAction = "/Search/Music?Keyword=";
break;
case "Album":
searchAction = "/Search/Album?Keyword=";
break;
case "Movie":
searchAction = "/Search/Movie?Keyword=";
break;
case "hiresomusic":
searchAction = "/Search/hiresomusic?Keyword=";
break;
case "hiresoalbum":
searchAction = "/Search/hiresoalbum?Keyword=";
break;
default:
searchAction = "/Search/Index?Keyword=";
break;
}
// 追加 選択したプルダウンでリンク先変更追加分
for (var i = 0, len = Math.min(suggest.length, maxLength) ; i < len; i++) {
if (searchOption == "All") {
searchUrl = PortaltopUrl + searchAction;
}
else if (searchOption == "Comic") {
searchUrl = ComictopUrl + searchAction;
}
else if (searchOption == "Book") {
searchUrl = YomelTopUrl + searchAction;
}
else {
searchUrl = MusictopUrl + searchAction;
}
item = suggest[i];
searchUrl += encodeURIComponent(item.name);
searchUrl += "&s=" + corner + "&no=" + (i + 1);
html += '- ' + item.name + '
';
}
}
html += '
' +
'閉じる
';
if (i) {
$('.result').eq(index).html(html); // 2014/4/9変更
$li = $('.result').eq(index).find('li'); // 2014/4/9変更
currentItem = 0;
} else {
clearResult();
}
};
// サジェストデータを取得関数
function crawl(val) {
// 直前のリクエストが途中だったら止める
stopCrawl();
// サジェストリストをリクエストする
var strType = "a";// default for portal and music site
if ($fmSelect.value == "Comic") {
strType = "c";
} else if ($fmSelect.value == "Book") {
strType = "w";
} else if ($fmSelect.value == "All") {
strType = "acw";
}
ajax = $.ajax(apiScheme + apiFqdn + '/v3/' + apiSvCd + '/suggest/' + strType + '/' + maxLength, { // 本番用
// ajax = $.ajax(apiScheme+apiFqdn+'/v3/1000/suggest/a/'+maxLength, { //テスト用
data: {
q: val
},
dataType: 'jsonp',
jsonpCallback: 'suggestCallback',
timeout: 10000,
}).fail(function (xhr) {
if (status.text !== 'abort') {
clearResult();
}
});
}
// サジェストデータを取得を中止する関数
function stopCrawl() {
if (ajax) {
ajax.abort();
}
}
// サジェスト一覧をクリアする関数
function clearResult() {
$('.result').eq(index).empty(); // 2014/4/9変更
}
// サジェストを選択する関数
function selectHandler(ev) {
var keycode = (function (e) {
try {
return e.keyCode;
} catch (error) {
return e.charCode;
}
})(ev);
if (keycode === 38) { // カーソルキー上
selectUpper(index);
} else if (keycode === 40) { // カーソルキー下
selectDowner(index);
} else if (keycode === 8 || keycode === 46) { // 削除キーとWinのDelキー
if (!$keyword[0].value === null) {
crawl();
}
} else {
timer;
}
}
// カーソルキー上を押した時の関数
function selectUpper(index) { // 2014/4/9 引数にindex追加
if ($keyword.eq(index).val() == "") {
return;
}
clearTimeout(timer);
if (!$li.hasClass('select')) { // 最初のサジェスト時
currentItem = rsLength;
} else if (currentItem === 0) { // 最初のアイテム時
currentItem = rsLength;
}
nextItem = currentItem - 1;
$li.removeClass('select')
.eq(nextItem)
.addClass('select');
$keyword.eq(index).val($li.eq(nextItem).text()); // 2014/4/9 .eq追加
currentItem = nextItem;
}
// カーソルキー下を押した時の関数
function selectDowner(index) { // 2014/4/9 引数にindex追加
if ($keyword.eq(index).val() == "") {
return;
}
clearTimeout(timer);
if (!$li.hasClass('select') || currentItem === rsLength - 1) { // 最初のサジェスト時 or 最後のアイテムの時
currentItem = -1;
}
nextItem = currentItem + 1;
$li.removeClass('select')
.eq(nextItem)
.addClass('select');
$keyword.eq(index).val($li.eq(nextItem).text()); // 2014/4/9 .eq追加
currentItem = nextItem;
}
// サジェストでマウスを動かした時の関数
function mouseenterHandler() {
$li.removeClass('select');
$(this).addClass('select');
currentItem = $li.index(this);
}
// 20131007追記 検索結果固定配置のためのスクリプト
var $header = $('#header'),
offset = $header.offset();
$(window).scroll(function () {
if ($(window).scrollTop() > offset.top) {
$('.result').eq(0).addClass('fixed'); // 2014/4/9変更
} else {
$('.result').eq(0).removeClass('fixed'); // 2014/4/9変更
}
});
})(window, jQuery);
// 2014/4/8追加デバッグ用
function _log(s) {
if (this.console && typeof console.log != "undefined") {
console.log(s);
}
}