/* global HoGoConst, i18n, BEObj, Modal, ClientInfo, HoGoUtils */ (function ($) { var API = { CHECK_USER_POINT: 'api/v1/CheckUserPoint', VALIDATE_STORED_PIN_CODE: 'api/v1/CheckPinValidate', VALIDATE_VIEW_ONLINE_EMAIL_ADDRESS: 'api/v1/CreateViewOnlinePINCode', VALIDATE_PIN_CODE: 'api/v1/VerifyViewOnlinePINCode' }; var cookieKey = { email: 'email', pinCode: 'pinCode', packageId: 'packId', packageType: 'packType', documentId: 'documentId' }; var keys, email; $(document).on('doViewDocument', function (event, arg) { keys = arg; // check point first $.ajax({ type: 'POST', timeout: HoGoConst.APITimeout, url: API.CHECK_USER_POINT, data: {'PackageID': arg.packageId}, dataType: 'json', success: function (resp) { if (resp.status === 'OK') { var data = resp.data; if (data.points + data.free_points > 0) { if (arg.isSpecificType) { verifyPinCode(); } else { checkOnlineView(arg.packageId, arg.docId); } } else { // show message missing point Modal.showAlert({content: i18n.t('error.missing_point')}); } } else { HoGoUtils.writeLog(data.status_desc); Modal.showAlert({content: i18n.t('error.unknown')}); } } }); }); $('#emailField').live('keypress', function (event) { if (event.which === 13 || event.keyCode === 13) { event.preventDefault(); verifyEmaiPinCode(); } }); $('#pinCodeField').live('keypress', function (event) { if (event.which === 13 || event.keyCode === 13) { event.preventDefault(); verifyShowPinCode(); } }); // show popup input PIN code $('#btnInputPin').live('click', function (event) { event.preventDefault(); showPopupInputPinCode(); }); function verifyPinCode() { // loading stored cookies var email = $.cookie(cookieKey.email), pinCode = $.cookie(cookieKey.pinCode), packageId = $.cookie(cookieKey.packageId); // check local stored pin code if (email !== null && pinCode !== null && packageId !== null && packageId === keys.packageId) { // call api validate stored value $.ajax({ type: 'POST', timeout: HoGoConst.APITimeout, url: API.VALIDATE_STORED_PIN_CODE, data: { 'Email': email, 'PinCode': pinCode, 'PackageID': packageId, 'DocumentID': keys.docId }, dataType: 'json', success: function (resp) { if (resp.status === 'OK') { // already to view online checkOnlineView(keys.packageId, keys.docId); } else { // pincode is expired or invalid, re-authorize // clear stored old cookies $.cookie(cookieKey.email, null); $.cookie(cookieKey.pinCode, null); $.cookie(cookieKey.packageId, null); $.cookie(cookieKey.packageType, null); $.cookie(cookieKey.documentId, null); authorizePinCode(); } } }); } else { // require input email to authorize authorizePinCode(); } } function authorizePinCode() { // show popup authorize Modal.createModal(2, { title: i18n.t('view_online.input_email_title'), preventClose: true, templateUrl: 'Template/verify_email_pin_code.html', onLoad: function () { $('#emailField').focus(); }, okCallback: verifyEmaiPinCode }); } function verifyEmaiPinCode() { email = $('#emailField').val(); // validate input data if (HoGoUtils.isValidEmail(email)) { verifyEmail(email); $('#formContainer').removeClass('has-error'); $('#helpBlock').addClass('hide'); } else { // show error $('#formContainer').addClass('has-error'); $('#helpBlock').removeClass('hide'); } } function verifyEmail(email) { $.ajax({ type: 'POST', timeout: HoGoConst.APITimeout, url: API.VALIDATE_VIEW_ONLINE_EMAIL_ADDRESS, data: { 'Email': email, 'DocumentID': keys.docId, 'PackageID': keys.packageId, 'Language' : HoGoUtils.detectBrowserLang() }, dataType: 'json', success: function (resp) { if (resp.status === 'OK') { showPopupInputPinCode(); } else { HoGoUtils.writeLog(resp.status_desc); Modal.showAlert({content: i18n.t('error.unknown')}); } } }); } function showPopupInputPinCode() { // input pin code Modal.createModal(2, { title: i18n.t('view_online.input_pin_code_title'), preventClose: true, templateUrl: 'Template/verify_pin_code.html', okTitle: i18n.t('buttons.submit'), onLoad: function () { $('#pinCodeField').focus(); }, okCallback: verifyShowPinCode }); } function verifyShowPinCode() { var pinCode = $('#pinCodeField').val(); // validate input data if (pinCode !== '') { verifyPIN(pinCode); $('#formContainer').removeClass('has-error'); $('#helpBlock').addClass('hide'); } else { // show error $('#formContainer').addClass('has-error'); $('#helpBlock').removeClass('hide'); } } function verifyPIN(PIN) { $.ajax({ type: 'POST', timeout: HoGoConst.APITimeout, url: API.VALIDATE_PIN_CODE, data: { 'PinCode': PIN, 'DocumentID': keys.docId, 'PackageID': keys.packageId }, dataType: 'json', success: function (resp) { if (resp.status === 'OK') { Modal.closeModal(); checkOnlineView(keys.packageId, keys.docId); // save cookies $.cookie(cookieKey.email, email); $.cookie(cookieKey.pinCode, PIN); $.cookie(cookieKey.packageId, keys.packageId); $.cookie(cookieKey.packageType, keys.isSpecificType); $.cookie(cookieKey.documentId, keys.docId); } else { HoGoUtils.writeLog(resp.status_desc); // pin code is invalid or expired Modal.showAlert({content: i18n.t('view_online.pin_code_invalid')}); } } }); } function checkOnlineView(packageID, documentID) { var FLASH_VIEWER_LINK = 'https://www.hogodoc.com/viewer/old-viewer.html?url=:urlFile', NEW_VIEWER_LINK = 'viewOnline?packageId=:packageId&documentId=:documentId'; var postData = {}; postData['PackageID'] = packageID; postData['DocumentID'] = documentID; $.ajax({ type: 'POST', timeout: HoGoConst.APITimeout, url: 'api/v1/CheckOnlineViewLinkValid', data: postData, dataType: 'json', success: function (data) { if (data.status === 'OK') { var viewerInfo = data.data; // check viewer version if (viewerInfo.viewer_version === 1) { // flash viewer if (viewerInfo.url_file && viewerInfo.url_file !== '') { // Check support plug-ins if (BEObj.isAdobeReaderPluginInstalled() === true && BEObj.isBookendPluginBlocked() === false && BEObj.isAdobeReaderPluginBlocked() === false && ClientInfo.checkFlashPlayerInstall() === true) { window.open(FLASH_VIEWER_LINK.replace(':urlFile', encodeURIComponent(viewerInfo.url_file))); } else { // Show warnsing Modal.showAlert({ title: i18n.t('notification.ask_enable_plugin.title'), content: i18n.t('notification.ask_enable_plugin.content') }); } } else { Modal.showAlert({content: i18n.t('error.unknown')}); } } else { // new viewer var link = NEW_VIEWER_LINK.replace(':packageId', packageID) .replace(':documentId', documentID); window.location.href = link; } } else { HoGoUtils.writeLog(data.status_desc); Modal.showAlert({content: i18n.t('error.unknown')}); } } }); } })(jQuery);