var $ = jQuery; var typeForm; var baseUrl = 'https://www.fastampsites.com/'; var $progressBar = null; var xhrArr = []; siteData['data'] = JSON.parse(siteData['data']); $(function () { // check auth checkAuth(); // load all pages loadAllPages(); // action select all pages $('.select-all-pages').click(function() { $(this).parents('.pages-list').find('input').each(function(key, item) { $(item).prop('checked', true); }); }); // action deselect select all pages $('.deselect-all-pages').click(function() { $(this).parents('.pages-list').find('input').each(function(key, item) { $(item).prop('checked', false); }); }); // action export pages $('#action-export, #action-update, #action-merge').click(function() { var actionType = $(this).attr('data-type'); var isMergePages = actionType == 'merge' ? true : false; var $checkbox = actionType == 'export' ? $('.pages-list-left input:checked') : $('.pages-list-right input:checked'); var amountRequest = 0; var stepPercent = 100 / $checkbox.length; var $percent = $('.progress-bar span').text('0%'); $progressBar = $('.progress-bar div').css('width', '0%'); if($checkbox.length == 0) return; $('#modal-export').fadeIn(); //$("body").css("overflow", "hidden"); progressAnimate(30000, stepPercent / 300); recursiveRequest($checkbox.length - 1, amountRequest); function progressAnimate(duration, amountRequest, complete) { $progressBar.stop(false, false).animate({ width: (stepPercent * amountRequest) + '%', }, { duration: duration, step: function (now, fx) { now = Math.round(now); if (parseInt($percent.text()) < now) $percent.text(now + '%'); }, complete: complete }); } function recursiveRequest(index, amountRequest) { var pageId = parseInt($checkbox.eq(index).attr('data-id')); amountRequest++; var payload = { projet_name: siteData['data']['projectName'], page_name: decodeURI(siteData['data']['pages'][pageId].name), page_url: decodeURI(siteData['data']['pages'][pageId].url) }; if(!isMergePages) payload.is_save_project = true; xhrArr.push($.ajax({ url: baseUrl + 'api/templates/export', type: 'POST', crossDomain: true, data: payload, xhrFields: { withCredentials: true }, success: function (data) { if (!stringIsJson(data)) { showFormInfo('Failed to send data to the server', 0, '#modal-export'); closeModalExport(2000); return; } data = JSON.parse(data); if (data.error != undefined) { showFormInfo(data.error, 0, '#modal-export'); closeModalExport(2000); return; } if(isMergePages) addContentToVersionBlock(data, payload.page_name); progressAnimate(3000, amountRequest, function() { if (amountRequest == $checkbox.length) { if(isMergePages) { showFormInfo('Page(s) export successfully completed', 1, '#modal-export'); $('#modal-export').delay(2000).fadeOut(1000); $('#modal-update').delay(2000).fadeIn(1000); } else { showFormInfo('Page(s) export successfully completed', 1, '#modal-export'); loadAllPages(); sortedPagesByLists(); $('#modal-export').delay(2000).fadeOut(1000); } } }); } })); if(index > 0) recursiveRequest(index - 1, amountRequest); } }); // open modal login to account $('#action-enter-account').click(function () { $('#modal-account h3').text('Login to FastAMPsites'); $('#modal-account-enter').text('Login'); $("body").css("overflow", "hidden"); $('#modal-account').fadeIn(); typeForm = 1; }); // open modal create new account $('#action-create-account').click(function () { $('#modal-account h3').text('Create New Account'); $('#modal-account-enter').text('Create'); $("body").css("overflow", "hidden"); $('#modal-account').fadeIn(); typeForm = 2; }); // action enter to account $('#modal-account-enter').click(function() { $('#modal-account button:first').prop('disabled', true); var email = $('#modal-account input[name=email]').val(); var password = $('#modal-account input[name=password]').val(); if(checkAccountForm(email, password)) { var action = (typeForm == 1) ? 'users/login' : 'api/users/create'; $.ajax({ url: baseUrl + action, type: 'POST', crossDomain: true, data: { email: email, password: password }, xhrFields: { withCredentials: true }, success: function(data) { data = JSON.parse(data); if(data.error != undefined) { showAccountFormInfo(data.error, 0); return; } $('#action-enter-account, #action-create-account').fadeOut(); $('#action-export, #action-logout, #action-open-project, #action-credentials, #action-merge, #action-update').delay(1000).fadeIn(); showAccountFormInfo('Now you can continue exporting pages', 1); setTimeout(function() { $('#modal-account').fadeOut(); }, 2000); }, error: function(data) { showAccountFormInfo('An error occurred while sending data', 0); } }); } else { showAccountFormInfo('Not a valid email or password', 0); } }); // close modal-account $('#modal-account-close').click(function() { $('#modal-account').fadeOut(); $('#modal-account button:first').prop('disabled', false); setTimeout(function () { $("body").css("overflow", "auto"); }, 500); }); // close model-export $('#progress-stop').click(function() { if(xhrArr.length > 0) for(var index in xhrArr) xhrArr[index].abort(); closeModalExport(2000); $progressBar = null; }); // logout $('#action-logout').click(function() { $.ajax({ url: baseUrl + 'users/logout', type: 'POST', crossDomain: true, xhrFields: { withCredentials: true } }); $('#action-export, #action-logout, #action-open-project, #action-credentials, #action-merge, #action-update').fadeOut(); $('#action-enter-account, #action-create-account').delay(1000).fadeIn(); }); // open modal credentials $('#action-credentials').click(function () { $("body").css("overflow", "hidden"); $('#modal-credentials').fadeIn(); // load credentials data $.ajax({ url: baseUrl+'api/users/credentials', type: 'GET', crossDomain: true, xhrFields: { withCredentials: true }, success: function(data) { data = JSON.parse(data); $.each(data, function(key, value) { var $input = $('#modal-credentials input[name=' + key + ']'); if($input.length) $input.val(value); }); $('#modal-credentials input, #modal-credentials button:first').prop('disabled', false); } }); }); // close model credentials $('#action-close-credentials').click(function() { $('#modal-credentials').fadeOut(); $('#modal-credentials input, #modal-credentials button:first').prop('disabled', true); setTimeout(function () { $("body").css("overflow", "auto"); }, 500); }); // save credentials $('#action-save-credentials').click(function() { var dataSend = {}; // get all field $('#modal-credentials input').each(function(index, item) { dataSend[$(item).attr('name')] = $(item).val(); }); $.ajax({ url: baseUrl+'api/users/credentials', type: 'POST', crossDomain: true, data: dataSend, xhrFields: { withCredentials: true }, success: function(data) { data = JSON.parse(data); if(data.error != undefined) { showFormInfo(data.error, 0, '#modal-credentials'); return; } showFormInfo('Сredentials data saved successfully', 1, '#modal-credentials'); setTimeout(function() { $('#modal-credentials').fadeOut(); }, 2000); }, error: function(data) { showAccountFormInfo('An error occurred while sending data', 0, '#modal-credentials'); } }); }); // on/off AMP link $('#used_amp').change(function () { var isUsedAMP = $(this).is(':checked'); $(this).prop('disabled', true); saveSattings('false', ''); if(isUsedAMP) $.ajax({ url: baseUrl+'api/project/domain', type: 'POST', crossDomain: true, data: { projet_name: siteData['data']['projectName'], }, xhrFields: { withCredentials: true } }).done(function (data) { if(data.length > 0) { data = JSON.parse(data); saveSattings(isUsedAMP.toString(), data.domain_name); } else { $('#used_amp').prop('checked', false) .prop('disabled', false); } }).fail(function () { $('#used_amp').prop('checked', false) .prop('disabled', false); }); }); // close modal update $('#btn-close-model-update').click(function () { $('#modal-update').hide(); // remove old file content $('#modal-update .modal-content-wrap .page').remove(); }); // open file content $('#modal-update .modal-content-wrap').on('click', '.page h4', function() { var type = $(this).attr('data-type'); var $parent = $(this).parent(); var $twoElement = $parent.find('.' + (type == 'html' ? 'css' : 'html') + '-content'); if($twoElement.css('display') == 'block') $twoElement.css('display', 'none'); var $content = $parent.find('.' + type + '-content'); var isHide = $content.css('display') == 'none'; $content.css('display', isHide ? 'block' : 'none'); if($content.is(':empty')) $content.append('