'use strict';
$(function() {
// init
initDevice();
initWrappedLabel();
events
$('input[data-action-url]').on('click', function () {
var $this = $(this);
$this.closest('form').attr('action', $this.attr('data-action-url'));
});
// format number with comma
$('input.type-input-currency').on('change', function () {
var $this = $(this);
$this.val(formatCurrency($this.val()));
});
// switch radio label design
$('label.wrapped-label input[type="radio"]').on('change', function() {
var $this = $(this);
switchCheckedWrappedLabel($this);
});
// switch checkbox label design
$('label.wrapped-label input[type="checkbox"]').on('change', function() {
var $this = $(this);
switchCheckedWrappedLabel($this);
});
// show confirmation message dialog
$('input[data-confirm-message]').on('click', function (ev) {
var $this = $(this);
var confirmed = $this.attr('data-confirmed');
if (confirmed === 'true') {
$this.data('confirmed', false);
return;
}
var message = $this.data('confirm-message');
showConfirmDialog(
message,
function () {
$this.attr('data-confirmed', 'true');
$this.trigger('click');
},
null
);
ev.preventDefault();
ev.stopImmediatePropagation();
});
// show file select dialog
$('input[data-file-select]').on('click', function (ev) {
var $this = $(this);
var $form = $this.closest('form');
var fileElementName = $this.data('file-select');
var $file = $form.find('input[name="' + fileElementName + '"][type="file"]');
$file.click();
if (0 < $file.val().length) {
$form.submit();
}
ev.preventDefault();
});
// open a page with a target by JavaScript
$('a[target]').on('click', function (ev) {
var $this = $(this);
var url = $this.attr('href');
var target = $this.attr('target');
window.open(url, target);
ev.preventDefault();
});
$('form[target]').on('submit', function (ev) {
var $this = $(this);
var url = $this.attr('action');
var target = $this.attr('target');
var params = $this.serialize();
if (params) {
var hashIdx = url.indexOf('#');
var hash = '';
if (hashIdx !== -1) {
hash = url.substr(hashIdx);
url = url.substr(0, url.length - hash.length);
}
if (url.indexOf('?') === -1) {
url = url + '?' + params + hash;
} else {
url = url + '&' + params + hash;
}
}
window.open(url, target);
ev.preventDefault();
});
// iframe dialog
$('[data-dialog-url]').on('click', function (ev) {
var $this = $(this);
var url = $this.attr('data-dialog-url');
var width = $this.attr('data-dialog-width');
var height = $this.attr('data-dialog-height');
var callbackName = $this.attr('data-dialog-callback');
if (!width) {
width = window.innerWidth * 0.9;
}
if (!height) {
height = window.innerHeight * 0.9;
}
var callback = (callbackName) ? window[callbackName] : null;
showIframeDialog(url, width, height, callback);
ev.preventDefault();
ev.stopImmediatePropagation();
});
// close window
$('[data-window-close]').on('click', function (ev) {
var frameElm = window.frameElement;
if (frameElm) {
var $frame = $(frameElm);
closeDialog($frame);
} else {
window.close();
}
ev.preventDefault();
ev.stopImmediatePropagation();
});
});
function isTouchDevice() {
return ('ontouchstart' in window
|| (window.DocumentTouch && document instanceof window.DocumentTouch)
) ? true : false;
}
function formatMessage(messageInfo) {
return messageInfo.Message + '(' + messageInfo.MessageId + ')';
}
function formatCurrency(value) {
for (var chIdx = 0; chIdx < value.length; chIdx++) {
var charCode = value.charCodeAt(chIdx);
if (48 <= charCode && charCode <= 57) {
// 0 - 9
continue;
} else if (charCode === 44) {
// ,
continue;
} else {
// non number
return value;
}
}
var num = value.replace(/,/g, '');
var formatedNum = '';
var len = num.length;
for (var numIdx = 1; numIdx <= len; numIdx++) {
var d = len - numIdx;
var ch = num.charAt(d);
if (numIdx !== 1 && numIdx % 3 === 1) {
formatedNum = ch + ',' + formatedNum;
} else {
formatedNum = ch + formatedNum;
}
}
// 前ゼロ削除
var re = /^[0-9]+.?[0-9]*/; // 数字か判断
if (re.test(num)) {
// 整数以外の文字列を削除
var numNum = Number(value.replace(/[^0-9]/g, ''));
formatedNum = numNum.toLocaleString();
}
return formatedNum;
}
function initDevice() {
if (isTouchDevice()) {
$('body').addClass('tablet');
} else {
$('body').addClass('no-tablet');
}
}
function initWrappedLabel() {
$('.wrapped-label').each(function() {
var $this = $(this);
var $input = $this.find('input');
if ($input.prop('disabled')) {
$input.closest('.wrapped-label').addClass('disabled');
} else if ($input.prop('checked')) {
$input.closest('.wrapped-label').addClass('selected');
}
});
}
function switchCheckedWrappedLabel($input) {
if ($input.prop('type') === 'radio') {
var name = $input.prop('name');
$('input[name="' + name + '"]:not(:checked)').closest('.wrapped-label').removeClass('selected');
}
if ($input.prop('checked')) {
$input.closest('.wrapped-label').addClass('selected');
} else {
$input.closest('.wrapped-label').removeClass('selected');
}
}
function showMessageDialog(messageInfos, funcOk) {
if (messageInfos.length === 0) {
return;
}
var messages = new Array(messageInfos.length);
for (var i = 0; i < messageInfos.length; i++) {
messages[i] = formatMessage(messageInfos[i]);
}
showAlertDialog(messages, funcOk);
}
function showAlertDialog(messages, funcOk) {
var $dialog = $('<div></div>').addClass('alert-dialog');
if (messages instanceof Array) {
for (var i = 0; i < messages.length; i++) {
$dialog.append($('<p></p>').text(messages[i]));
}
} else {
$dialog.append($('<p></p>').text(messages));
}
$dialog.dialog({
modal: true,
resizable: true,
width: 500,
buttons: {
'OK': function () {
$(this).dialog('close');
if (funcOk) {
funcOk();
}
}
},
open: function () {
$(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
},
close: function () {
$(this).remove();
}
});
}
function showConfirmDialog(messages, funcYes, funcNo) {
var $dialog = $('<div></div>').addClass('confirm-dialog');
if (messages instanceof Array) {
for (var i = 0; i < messages.length; i++) {
$dialog.append($('<p></p>').text(messages[i]));
}
} else {
$dialog.append($('<p></p>').text(messages));
}
$dialog.dialog({
modal: true,
resizable: true,
width: 500,
buttons: {
'OK': function () {
$(this).dialog('close');
if (funcYes) {
funcYes();
}
},
'キャンセル': function (event, ui) {
$(this).dialog('close');
if (funcNo) {
funcNo();
}
}
},
open: function () {
$(this).parents('.ui-dialog-buttonpane button:eq(1)').focus();
},
close: function () {
$(this).remove();
}
});
}
function showIframeDialog(url, width, height, callbackFunc) {
var $dialog = $('<iframe></iframe>')
.addClass('iframe-dialog')
.on('load', function () {
var $metaTitle = $(this.contentDocument).find('meta[name=title]');
$dialog.dialog('option', 'title', $metaTitle.attr('content'));
});
$dialog.attr('src', url);
$dialog.dialog({
width: width,
height: height,
modal: true,
open: function () {
$dialog.attr('data-dialog-result', '');
},
close: function () {
if (callbackFunc) {
var result = $dialog.data('dialog-result');
callbackFunc(result);
}
$dialog.remove();
}
});
}
function setDialogResult($dialog, resultValue) {
$dialog.attr('data-dialog-result', resultValue);
}
/**
* ダイアログを閉じます。
* @param {any} $dialog ダイアログ
*/
function closeDialog($dialog) {
if (!$dialog || $dialog.length === 0) {
return;
}
$dialog.closest('.ui-dialog').find('.ui-dialog-titlebar-close').trigger('click');
}
/**
* 住所検索機能を利用する場合に呼び出します。
* ボタンに searchAddress クラスを設定し、下記の属性を設定することで利用できます。
* data-id-suffix: 郵便番号や住所の要素の接尾子 #zipCode1{suffix}, #zipCode2{suffix}, #address1{suffix}, #address2{suffix}, #address3{suffix}, #addressKana{suffix}
* data-zip-code1-label: 郵便番号1ラベル
* data-zip-code2-label: 郵便番号2ラベル
* data-address-label: 住所ラベル
* data-error-message: fail時に表示するメッセージ
* data-action-url: アクションURL(画面共通コントローラーのSearchAddress固定)
*/
function initAddressSearch() {
$('.searchAddress').on('click', function () {
var $this = $(this);
var idSuffix = $this.attr('data-id-suffix');
var zipCode1Label = $this.attr('data-zip-code1-label');
var zipCode2Label = $this.attr('data-zip-code2-label');
var addressLabel = $this.attr('data-address-label');
var errorMessage = $this.data('error-message');
var actionUrl = $this.attr('data-action-url');
var zipCode1 = $('#zipCode1' + idSuffix).val();
var zipCode2 = $('#zipCode2' + idSuffix).val();
$.ajax({
type: 'POST',
url: actionUrl,
cache: false,
data: {
__RequestVerificationToken: $('input[name=__RequestVerificationToken]').val(),
zipCode1: zipCode1,
zipCode2: zipCode2,
zipCode1Label: zipCode1Label,
zipCode2Label: zipCode2Label,
addressLabel: addressLabel
}
}).done(function (response, textStatus, jqXHR) {
if (response.Success) {
$('#address1' + idSuffix).val(response.Data.Address1);
$('#address2' + idSuffix).val(response.Data.Address2);
$('#address3' + idSuffix).val(response.Data.Address3);
$('#addressKana' + idSuffix).val(response.Data.AddressKana).trigger('change');
} else {
$('#address1' + idSuffix).val('');
$('#address2' + idSuffix).val('');
$('#address3' + idSuffix).val('');
$('#addressKana' + idSuffix).val('').trigger('change');
}
showMessageDialog(response.Messages, null);
}).fail(function (jqXHR, textStatus, errorThrown) {
$('#address1' + idSuffix).val('');
$('#address2' + idSuffix).val('');
$('#address3' + idSuffix).val('');
$('#addressKana' + idSuffix).val('').trigger('change');
showAlertDialog(errorMessage, null);
});
});
}
/**
* 支店検索機能を利用する場合に呼び出します。
* ボタンに searchBranch クラスを設定し、下記の属性を設定することで利用できます。
* data-id-suffix: 支店の要素の接尾子 #branchCode{suffix}, #branchName{suffix}, #branchNameKana{suffix}
* data-error-message: fail時に表示するメッセージ
* data-action-url: アクションURL(画面共通コントローラーのSearchBranch固定)
*/
function initBranchSearch() {
$('.searchBranch').on('focusout', function () {
var $this = $(this);
var idSuffix = $this.attr('data-id-suffix');
var errorMessage = $this.data('error-message');
var actionUrl = $this.attr('data-action-url');
var $branchCode = $('#branchCode' + idSuffix);
var branchCodeVal = $branchCode.val();
// 支店は非活性の時は検索しない
if ($branchCode.prop('readonly')) {
return;
}
// 空欄の時は検索しない
if (branchCodeVal === '' || branchCodeVal === undefined) {
$branchCode.val('');
$('#branchName' + idSuffix).text('');
$('#hiddenBranchName' + idSuffix).val('');
$('#branchNameKana' + idSuffix).val('');
return;
}
$.ajax({
type: 'POST',
url: actionUrl,
cache: false,
data: {
__RequestVerificationToken: $('input[name=__RequestVerificationToken]').val(),
branchCode: branchCodeVal
}
}).done(function (response, textStatus, jqXHR) {
if (response.Success) {
$branchCode.val(response.Data.BranchCode);
$('#branchName' + idSuffix).text(response.Data.BranchName);
$('#branchName' + idSuffix).val(response.Data.BranchName);
$('#hiddenBranchName' + idSuffix).val(response.Data.BranchName);
$('#branchNameKana' + idSuffix).val(response.Data.BranchNameKana);
} else {
$branchCode.val('');
$('#branchName' + idSuffix).text('');
$('#branchName' + idSuffix).val('');
$('#hiddenBranchName' + idSuffix).val('');
$('#branchNameKana' + idSuffix).val('');
}
showMessageDialog(response.Messages, null);
}).fail(function (jqXHR, textStatus, errorThrown) {
$branchCode.val('');
$('#branchName' + idSuffix).text('');
$('#branchName' + idSuffix).val('');
$('#hiddenBranchName' + idSuffix).val('');
$('#branchNameKana' + idSuffix).val('');
showAlertDialog(errorMessage, null);
});
});
}
/**
* アコーディオンボタン(+-表示切替ボタン)を利用する場合に呼び出します。
*/
function initAccordionButton() {
$('.accordion').on('click', function () {
var $this = $(this);
var accordionButtonId = $this.attr('data-accordion-button-id');
var $accordionButton = $('#' + accordionButtonId);
var accordionStatus = $accordionButton.text();
var accordionTargetArea = $this.attr('data-accordion-area-id');
var $targetArea = $('#' + accordionTargetArea);
if (accordionStatus === '+') {
$targetArea.removeAttr('hidden');
$accordionButton.text('-');
} else {
$targetArea.attr('hidden', 'hidden');
$accordionButton.text('+');
}
});
}