(function($){
$(window).on({
'load':function(){
var
configs = {
cookieName:'popup_new_item',
cookieOption:{
//expires:1,
path:'/',
secure:true
},
scroll:400,
device:[
'sp'
],
fileList:[
{
type:'js',
func:$.numberFormat,
url:'/js/jquery/jquery.numberFormat.js'
},
{
type:'js',
func:$.dateToUnixTime,
url:'/js/jquery/jquery.dateToUnixTime.js'
},
{
type:'js',
func:$.cookie,
url:'/js/jquery/jquery.cookie.js'
},
{
type:'js',
func:$.fn.sliceStrLimit,
url:'/plugin/jquery/slice_str_limit/jquery.sliceStrLimit.js'
},
{
type:'js',
func:$.fn.popup,
url:'/plugin/jquery/popup/jquery.popup.js'
},
{
type:'css',
url:'/plugin/jquery/popup/default.css'
},
{
type:'css',
url:'/item/new_item_popup/css/index.css'
}
],
cache:false,
cacheParam:'r',
sliceStrLimit:14
},
funcConfirmDevice = () => {
var
nav = navigator.userAgent.toLowerCase();
return nav.indexOf('iphone') > 0 || nav.indexOf('ipad') > 0 || nav.indexOf('android') > 0 || nav.indexOf('mobile') > 0?'sp':'pc';
};
if($.inArray(funcConfirmDevice(), configs.device) === -1)
return false;
var
flagExecute = false,
funcExecute = () => {
$.ajax({
url:'/configs/popup/new_item.json?r=' + Math.random(),
dataType:'json',
error:function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
return;
},
success:function(response, textStatus, jqXHR){
var
tax = 1.1,
currentDate = '',
funcGetItemName = (itemName) => {
var
mchSaleTime = itemName.match(/【(.+?)販売新作】/);
if(mchSaleTime === null)
return [
itemName,
''
];
var
arrItemName = itemName.split(mchSaleTime[0]);
return [
arrItemName[0],
mchSaleTime[0]
];
},
elePopupNewItemList = $('#popup_new_item_list');
elePopupNewItemList.empty();
for(var i = 0, l = response.length;i < l;i++){
if(currentDate === ''){
currentDate = response[i].sale_time;
}else
if(currentDate !== response[i].sale_time){
break;
}
var
arrItemName = funcGetItemName(response[i].item_name);
elePopupNewItemList.append('\
\
\
\
\
\
\
\
' + arrItemName[0] + '\
\
\
\
\
\
\
\
\
\
');
}
elePopupNewItemList.find('.slice_str_limit').sliceStrLimit();
var
oneDaySecond = 86400;
funcGetDate = (timestamp) => {
var
date = new Date(timestamp * 1000),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes(),
dayOfWeek = date.getDay(),
dayOfWeekStr = [
'日',
'月',
'火',
'水',
'木',
'金',
'土'
][dayOfWeek];
return month + '/' + day + '(' + dayOfWeekStr + ')' + hour + ':' + (minute < 10?'0':'') + minute;
},
elePopupNewItemDateComment = $('#popup_new_item_date_comment'),
elePopupNewItemList = $('#popup_new_item_list'),
unixTimeNewItemSaleTime = $.dateToUnixTime(elePopupNewItemList.find('> li').eq(0).data('popup_new_item_sale_time')),
unixTimeCurrent = $.dateToUnixTime();
if(unixTimeNewItemSaleTime + oneDaySecond - 1 < unixTimeCurrent)
return false;
elePopupNewItemDateComment.html(funcGetDate(unixTimeNewItemSaleTime) + '~' + funcGetDate(unixTimeNewItemSaleTime + oneDaySecond - 1));
$('#popup_new_item')
.popup({
callbackFadeOutFinish:() => {
$.cookie(configs.cookieName, '1', configs.cookieOption);
}
})
.trigger('popup.show');
}
});
},
funcPutJs = (
location,
callback
) => {
$.getScript(location + (configs.cache !== false?'':(location.indexOf('?') === -1?'?':'&') + configs.cacheParam + '=' + Math.random()))
.done((script, textStatus) => {
callback(true, location);
})
.fail((jqxhr, settings, exception) => {
callback(false, location);
});
},
funcPutLink = (
location,
callback
) => {
var
eleHead = $('head'),
eleLink = document.createElement('link');
eleLink.rel = 'stylesheet';
eleLink.href = location + (configs.cache !== false?'':(location.indexOf('?') === -1?'?':'&') + configs.cacheParam + '=' + Math.random());
eleHead.append(eleLink);
eleLink.addEventListener('load', function(){
callback(true, location);
});
eleLink.addEventListener('error', function(){
eleLink.parentNode.removeChild(eleLink);
callback(false, location);
});
},
funcLoadFile = () => {
var
cntImg = 0,
lenFile = configs.fileList.length,
funcPutResult = function(
result,
location
){
if(result === false)
console.log('popup_new_item:load_file => errer url(' + location + ')');
cntImg++;
if(lenFile === cntImg)
funcLoadComplete();
};
for(var i = 0;i < lenFile;i++)
switch(configs.fileList[i].type){
case 'js':
if(typeof configs.fileList[i].func === 'function'){
funcPutResult(
true,
configs.fileList[i].url
);
break;
}
funcPutJs(
configs.fileList[i].url,
function(
result,
location
){
funcPutResult(
result,
location
);
}
);
break;
case 'css':
funcPutLink(
configs.fileList[i].url,
function(
result,
location
){
funcPutResult(
result,
location
);
}
);
break;
default:
funcPutResult(false, configs.fileList[i]);
break;
}
},
funcPutHtml = (eleParent) => {
$(eleParent).append('\
\
');
},
funcLoadComplete = () => {
var
eleNewItemPopupParent = $('#new_item_popup_parent');
if(!eleNewItemPopupParent[0]){
console.log('popup_new_item:undefined ele => #new_item_popup_parent');
return false;
}
var
dataNewItemPopupParent = eleNewItemPopupParent.data('new_item_popup_parent');
if(typeof dataNewItemPopupParent === 'undefined'){
console.log('popup_new_item:undefined data => new_item_popup_parent');
return false;
}
funcPutHtml(dataNewItemPopupParent);
if(configs.scroll !== 0){
$(window).on({
'scroll':function(){
var
scroll = $(this).scrollTop();
if(
flagExecute ||
configs.scroll > scroll
)return false;
flagExecute = true;
var
displayFlag = $.cookie(configs.cookieName);
if(
typeof displayFlag !== 'undefined' &&
displayFlag === '1'
)
return false;
funcExecute();
}
});
}else{
var
displayFlag = $.cookie(configs.cookieName);
if(
typeof displayFlag !== 'undefined' &&
displayFlag === '1'
)
return false;
funcExecute();
}
};
funcLoadFile();
}
});
})(jQuery);