/* * Manage Integrate between Box and HoGo */ var BoxIntegrate = (function ($) { var folderId, itself, authorizationCode; var conectBox; // Get all data integrate between Box and HoGo function getBoxIntegrateSyncFolder() { var pData = {}; pData["SessionID"] = UserSession.getSessionID(); return $.ajax({ type: "POST", url: "api/v1/GetBoxIntegrateFolder", data: pData, timeout: HoGoConst.APITimeout, beforeSend: function () { Modal.closeModal(); }, success: function (data) { var status = data.status; switch (status) { case "OK": $("#btn-reconnect").hide(); $("#btn-disconnect").show(); // draw list folder $('#boxContent_nav').pagination({ dataSource: data.list_folder, pageSize: 5, callback: function (data, pagination) { drawListFolderSync(data); } }); $(".pagination").show(); break; case "NoFolderIntegrate": $("#boxContent").html(i18n.t('box_integrate.message.no_folder_integrate')); $("#btn-disconnect").show(); $("#btn-reconnect").hide(); $(".pagination").hide(); break case "NoIntegrate": $("#boxContent").html(i18n.t('box_integrate.message.not_integrate')); $("#btn-disconnect").hide(); $("#btn-reconnect").show(); $(".pagination").hide(); // show no integrate message break; case "DisconectSync": $("#boxContent").html(i18n.t('box_integrate.grant_access.not_granted')); // show reconect button $("#btn-disconnect").hide(); $("#btn-reconnect").show(); $(".pagination").hide(); break; case "NotAllowSync": $("#boxContent").html(i18n.t('box_integrate.message.notify_contact_admin')); $("#btn-disconnect").hide(); $("#btn-reconnect").hide(); $(".pagination").hide(); break; case "NotGrantAccess": $("#boxContent").html(i18n.t('box_integrate.grant_access.not_granted')); $("#btn-disconnect").hide(); $("#btn-reconnect").show(); $(".pagination").hide(); default : break; } } }); } function drawListFolderSync(listFolder, cb) { if (!$.isArray(listFolder)) return ""; var content = ""; $.each(listFolder, function (i, value) { var folder = value.folder; var listPackage = value.list_package; content += '
' + '
' + '
' + '' + '
' + '
' + '' + folder.name + '' + '
' + '
'; (folder.status === 1) ? content += '' + i18n.t('box_integrate.folder_sync_status.in_sync') + '' : content += '' + i18n.t('box_integrate.folder_sync_status.un_sync') + ''; content += '
' + '
'; $.each(listPackage, function (j, val) { var edit = val.editable; if (edit === 1) { content += '' + i18n.t('box_integrate.button.edit_package') + ''; } else { content += '' + i18n.t('box_integrate.button.non_edit_package') + ''; } }); content += '
' + '' + '
' + '
'; $("#boxContent").html(content); // bind event remove function for each folder $('.btn-remove-sync').bind('click', function () { var currentButton = $(this); // call function delete showConfirmRemoveSyncFolder($(this).attr('folderId'), this); }); // Stop sync single folder $('.btn-stop-sync').bind('click', function () { var currentButton = $(this); // call function delete showConfirmChangeSyncSingleFolderStatus($(this).attr('folderId'), this, true); }); // start sync for single folder $('.btn-start-sync').bind('click', function () { var currentButton = $(this); // call function delete showConfirmChangeSyncSingleFolderStatus($(this).attr('folderId'), this, false); }); }); } function showConfirmRemoveSyncFolder(_folderId, _this) { // Set value to do on Popup folderId = _folderId; itself = _this; Modal.createModal(2, { title: i18n.t('box_integrate.message.stop_sync_folder_title'), // Should support multiple langue (i18n.t('modal.change_email_title')) preventClose: true, content: i18n.t('box_integrate.message.stop_sync_folder_ms'), okCallback: function (modal) { deletePackageSync(folderId, itself); } }); } function showConfirmReconectSync() { Modal.createModal(2, { title: i18n.t('box_integrate.message.reconnect_title'), preventClose: true, content: i18n.t('box_integrate.message.reconnect_ms'), okCallback: function (modal) { connectBox(); } }); } function showConfirmDisconectSync() { Modal.createModal(2, { title: i18n.t('box_integrate.message.stop_sync_box_title'), preventClose: true, content: i18n.t('box_integrate.message.stop_sync_box_ms'), okCallback: function (modal) { disconectUserSyncFeature(); } }); } function disconectUserSyncFeature() { var pData = {}; pData["SessionID"] = UserSession.getSessionID(); return $.ajax({ type: "POST", url: "api/v1/DisconnectSyncBoxIntegate", data: pData, timeout: HoGoConst.APITimeout, beforeSend: function () { Modal.closeModal(); }, success: function (data) { var status = data.status; switch (status) { case "OK": // draw list folder HoGoUtils.makeNotifyServer({ "msg": i18n.t('box_integrate.message.save_success'), "autoClose": true, "time": 400 }); break; default : Modal.showAlert({content: i18n.t('box_integrate.message.error_stop_sync')}); break; } getBoxIntegrateSyncFolder(); } }); } function removeSyncFolder(fId, currentBtn) { var pData = {}; pData["session_id"] = UserSession.getSessionID(); pData["folder_ids"] = fId; return $.ajax({ type: "POST", url: "api/v1/DeleteBoxFilesFolders", data: pData, timeout: HoGoConst.APITimeout, beforeSend: function () { Modal.closeModal(); }, success: function (data) { var status = data.status; switch (status) { case "OK": HoGoUtils.makeNotifyServer({ "msg": i18n.t('box_integrate.message.delete_package_success'), "autoClose": true, "time": 400 }); break; case "DeleteFromBoxError": Modal.showAlert({content: i18n.t('box_integrate.message.error_delete_file')}); break; default : Modal.showAlert({content: i18n.t('box_integrate.message.error_delete_file')}); break; } }, error: function () { Modal.showAlert({content: i18n.t('box_integrate.message.error_delete_file')}); } }); } function deletePackageSync(fId, currentBtn) { var pData = {}; pData["session_id"] = UserSession.getSessionID(); pData["folder_id"] = fId; return $.ajax({ type: "POST", url: "api/v1/DeletePackageSyncBoxIntegate", data: pData, timeout: HoGoConst.APITimeout, beforeSend: function () { Modal.closeModal(); }, success: function (data) { var status = data.status; switch (status) { case "OK": getBoxIntegrateSyncFolder(); HoGoUtils.makeNotifyServer({ "msg": i18n.t('box_integrate.message.delete_package_success'), "autoClose": true, "time": 400 }); removeSyncFolder(fId, currentBtn) break; default : Modal.showAlert({content: i18n.t('box_integrate.message.error_delete_package')}); break; } } }); } function reconectSyncBoxIntegrate(authen_code) { var pData = { SessionID: UserSession.getSessionID(), authen_code: authen_code }; return $.ajax({ type: "POST", url: "api/v1/ReconectSyncBoxIntegrate", data: pData, timeout: HoGoConst.APITimeout, beforeSend: function () { Modal.closeModal(); }, success: function (data) { var status = data.status; switch (status) { case "OK": // draw list folder HoGoUtils.makeNotifyServer({ "msg": i18n.t('box_integrate.message.save_success'), "autoClose": true, "time": 400 }); getBoxIntegrateSyncFolder(); break; default : Modal.showAlert({content: i18n.t('box_integrate.message.error_reconnect')}); break; } // Ethei success or not we will remove URL prameter return from Box window.history.replaceState("", "", "/MyAccount"); }, error: function () { window.history.replaceState("", "", "/MyAccount"); Modal.showAlert({content: i18n.t('box_integrate.message.error_box_token')}); } }); } function reconnetSyncWithBox(){ var pData = {}; pData["SessionID"] = UserSession.getSessionID(); return $.ajax({ type: "POST", url: "api/v1/ReconectSyncBoxIntegrate", data: pData, timeout: HoGoConst.APITimeout, success: function (data) { // MyAccount.loadingDataSetting(); //// getBoxIntegrateSyncFolder() connectBox(); } }); } // Do conect box popup function connectBox() { $.cookie('box_return_path', ""); $.cookie('box_return_path', ""); var url = "https://account.box.com/api/oauth2/authorize?response_type=code&client_id=qzb9q06yhggos7jnypb44z6k3u67jmvs&state="; conectBox = window.location.href = url; } function showConfirmChangeSyncSingleFolderStatus(_folderId, _this, isStop) { console.log("Show confirm change folder sync status"); // Set value to do on Popup folderId = _folderId; itself = _this; var title = (isStop) ? i18n.t('box_integrate.message.stop_single_folder_title') : i18n.t('box_integrate.message.restart_single_folder_title'); var content = "
" content += (isStop) ? i18n.t('box_integrate.message.stop_single_folder_ms') : i18n.t('box_integrate.message.restart_single_folder_ms'); content += "
" if(!isStop){ content += '
' } console.log(content); Modal.createModal(2, { title: title, //i18n.t('box_integrate.message.stop_sync_box_title') preventClose: true, content: content, //i18n.t('box_integrate.message.stop_sync_box_ms'), okCallback: function (modal) { var isEncodeExisted = false; if(!isStop){ isEncodeExisted = $("#encode_existed").is(':checked'); } console.log("Is Encode existed", isEncodeExisted); changeSyncSingleFolderStaus(folderId, itself, isStop, isEncodeExisted); } }); } function changeSyncSingleFolderStaus(_folderId, _this, isStop, isEncodeExisted) { var pData = {}; pData["SessionID"] = UserSession.getSessionID(); pData["folder_id"] = _folderId; pData["is_stop"] = isStop; pData["is_sync_existed_file"] = isEncodeExisted; return $.ajax({ type: "POST", url: "api/v1/ChangeSingleFolderSyncStatus", data: pData, timeout: HoGoConst.APITimeout, beforeSend: function () { Modal.closeModal(); }, success: function (data) { var status = data.status; switch (status) { case "OK": // draw list folder HoGoUtils.makeNotifyServer({ "msg": i18n.t('box_integrate.message.save_success'), "autoClose": true, "time": 400 }); getBoxIntegrateSyncFolder(); break; case "OutPoint": Modal.showAlert({content: i18n.t('box_integrate.message.restart_single_folder_out_point')}); break; case "Stopping": Modal.showAlert({content: i18n.t('box_integrate.message.folder_stopping')}); break; case "Not Exist": setTimeout(function(){ Modal.showAlert({content: i18n.t('box_integrate.message.folder_delete')}); }, 1000); getBoxIntegrateSyncFolder(); break; default : var message = (isStop) ? i18n.t('box_integrate.message.stop_single_folder_er') : i18n.t('box_integrate.message.restart_single_folder_er'); Modal.showAlert({content: message}); break; } } }); } return { init: function () { getBoxIntegrateSyncFolder(); $("#btn-disconnect").bind("click", function () { showConfirmDisconectSync(); }); $("#btn-reconnect").bind("click", function () { showConfirmReconectSync(); }); var returnPath = $.cookie('box_return_path'); authorizationCode = HoGoUtils.getParameter()["code"]; if (authorizationCode !== null && typeof authorizationCode !== 'undefined') { $('.nav-pills a[href="#boxIntegrate"]').tab('show'); if (authorizationCode !== "boxinfo" && returnPath !== "SendWizard") // go from package detail reconectSyncBoxIntegrate(authorizationCode); } } }; }(jQuery));