function showWPThickboxWithOurDocuments() { jQuery('#TB_load').show(); createDocumentListToPublish(); jQuery('#TB_load').hide(); } function insertShortCodeInEditor() { var documentIdToInsert = jQuery('.abcsubmit-select-document-to-insert').val(); window.send_to_editor('[abc-submit-inline id="' + documentIdToInsert + '"]'); tb_remove(); } function insertShortCodeInEditorAsButton() { var documentIdToInsert = jQuery('.abcsubmit-select-document-to-insert').val(); var documentName = jQuery(".abcsubmit-select-document-to-insert option:selected").text(); window.send_to_editor('[abc-submit-popup id="' + documentIdToInsert + '" name="' + documentName + '"]'); tb_remove(); } function createDocumentListToPublish(documentList) { (function ($) { $.Deferred(function (defer) { getJwt().then(function (jwt) { $('#abcsubmit-login-dialog, .abc-document-list-for-publish').remove(); getDocuments(jwt).then(function(documentsList) { defer.resolve( documentsList ); }).fail(function(error){ try { window.localStorage.setItem('abcsubmit_jwt_token', ""); } catch (e) { try { window.sessionStorage.setItem('abcsubmit_jwt_token', ""); } catch (e) { wp_set_cookie( 'abcsubmit_jwt_token', "" ); } } createDocumentListToPublish(); }); }); }).then(function (documentList) { if ( ! documentList.length ) { jQuery('#abcsubmit-media-thickbox').html( '
' + 'You dont have any documents yet. Go to AbcSubmit plugin and show us some art.' + '
'); } else { var ListWithAllDocumentsHtml = '
'; var selectDocuments = ''; ListWithAllDocumentsHtml += '
' + selectDocuments + '' + '' + '
'; ListWithAllDocumentsHtml += '
'; jQuery('#abcsubmit-media-thickbox').html(ListWithAllDocumentsHtml); } tb_show('Insert form, slider, images, online shop or entire web page', "#TB_inline?inlineId=abcsubmit-media-thickbox"); $("#TB_ajaxContent, #TB_iframeContent").css({width: '100%'}); $("#TB_ajaxContent, #TB_iframeContent").css({height: '100%'}); jQuery('#TB_load').hide(); }); })(jQuery); } function getDocuments(jwt) { return (function($){ return $.Deferred(function(defer){ $.get("//www.abcsubmit.com/api/v1/forms/?JWT=" + jwt + "&t=" + ( +new Date ) ) .then( function( documentsList) { defer.resolve(documentsList); }) .fail( function(xhr){ defer.reject( new Error("Failed to fetch documents list from server")); }); }); })(jQuery); } function getJwt() { return (function ($) { return $.Deferred(function (defer) { var existentJwtToken = null; try { existentJwtToken = window.localStorage.getItem('abcsubmit_jwt_token'); } catch (e) { try { existentJwtToken = window.sessionStorage.getItem('abcsubmit_jwt_token'); } catch (e) { existentJwtToken = wp_get_cookie( 'abcsubmit_jwt_token'); } } if (existentJwtToken) { defer.resolve(existentJwtToken); } // CREATE A POPUP AND MAKE USER TO LOGIN var dialog = document.createElement('div'); dialog.innerHTML = [ '
', '
', '', '', '
', '', '
', '', '', '
', '', '
', '
', '
', ].join("\n"); dialog.id = "abcsubmit-login-dialog"; document.getElementById('abcsubmit-media-thickbox').innerHTML = ''; document.getElementById('abcsubmit-media-thickbox').appendChild(dialog); tb_show('Login to AbcSubmit', "#TB_inline?inlineId=abcsubmit-media-thickbox"); $("#TB_ajaxContent, #TB_iframeContent").css({width: '100%'}); $("#TB_ajaxContent, #TB_iframeContent").css({height: '100%'}); $(dialog).on('click', 'button#abcsubmit-login-button', function () { var username = $(dialog).find("#abcsubmit-user").val(); var password = $(dialog).find("#abcsubmit-password").val(); if ( !username ) { alert("Please enter your username"); $(dialog).find("#abcsubmit-user").focus(); return; } if ( !password ) { alert("Please enter your password"); $(dialog).find("#abcsubmit-password").focus(); return; } $.post("//www.abcsubmit.com/api/v1/users/login", { username: username, password: password, }).then(function (jwt) { try { window.localStorage.setItem('abcsubmit_jwt_token', jwt); } catch (e) { try { window.sessionStorage.setItem('abcsubmit_jwt_token', jwt); } catch (e) { wp_set_cookie( 'abcsubmit_jwt_token', jwt); } } defer.resolve( jwt ); }).fail(function (xhr) { if (!xhr) { return; } var errorAsJSON = xhr.responseJSON; if (!errorAsJSON) { alert("Unknown login problem"); return; } if (errorAsJSON.message) { alert(errorAsJSON.message); } else { alert("Unknown login problem"); } }); }); $(dialog).on('click', 'button#abcsubmit-cancel-login-button', function () { tb_remove(); defer.reject(new Error("User aborted the login process")); }); }); })(jQuery); }