.button.wpcp-insert-onedrive-content, .button.wpcp-insert-onedrive-content:hover, .button.wpcp-insert-onedrive-content:active, .button.wpcp-insert-onedrive-content:focus { float: right; margin-right: 7px; background-image: url("../../../css/images/onedrive_logo.svg"); background-repeat: no-repeat; background-position: 6px center; padding-left: 25px; background-size: 1em; } #woocommerce-product-data ul.wc-tabs li.cloud-uploads-onedrive_options a:before { font-family: Dashicons; content: '\f176'; } .show_if_shareonedrive_upload_box { background: #fff; border: 1px solid #e5e5e5; margin: 5px 15px 10px; padding: 1px 12px; position: relative; overflow: hidden; }a.woocommerce-button.wpcp-wc-open-box { display: table-cell; font-size: 16px; text-decoration: none; cursor: pointer; } .wpcp-uploads-container-admin a.woocommerce-button.wpcp-wc-open-box { font-size: unset; } #wpcp-modal-upload-action { z-index: 9999; } #wpcp-modal-upload-action.wpcp-woocommerce-upload-container .modal-dialog { max-width: 50vw; } #wpcp-modal-upload-action .woocommerce-order-upload-box { height: 100%; display: flex; flex-wrap: nowrap; justify-content: flex-start; flex-direction: column; gap: 15px; align-items: stretch; } #wpcp-modal-upload-action p { padding: 0; margin: 0; font-size: 16px; } #wpcp-modal-upload-action .woocommerce-order-upload-box #ShareoneDrive { margin: 10px 0 0 0; } #wpcp-modal-upload-action .woocommerce-order-upload-box .wpcp-browser--init { max-height: 128px !important; } #wpcp-modal-upload-action .woocommerce-order-upload-box .ajax-filelist { background-image: none !important; min-height: 96px; } #wpcp-modal-upload-action .woocommerce-order-upload-box .wpcp-thumb-view .files-container, #wpcp-modal-upload-action .woocommerce-order-upload-box .wpcp-thumb-view .folders-container { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)) !important; grid-auto-rows: auto !important; } ul.wpcp-uploads-list { list-style-type: none !important; margin: 10px 0 !important; padding: 0 !important; overflow-x: hidden; overflow-y: auto; max-height: 50vh; max-width: 40vw; font-size: 80%; position: relative; transition: max-height 1s ease-in-out; } ul.wpcp-uploads-list:not(.wpcp-uploads-received) { height: 28px; max-height: 28px; } .wpcp-uploads-container-admin ul.wpcp-uploads-list { margin: 0 0 10px 0; } ul.wpcp-uploads-list li { margin: 0; display: flex; align-items: center; gap: 10px; max-width: 100%; padding: 2px 10px; } ul.wpcp-uploads-list li:nth-child(even) { background-color: #00000004; } ul.wpcp-uploads-list li .wpcp-uploads-list-item-icon { height: 16px; width: 16px; } ul.wpcp-uploads-list li .wpcp-uploads-list-item-name { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } ul.wpcp-uploads-list li .wpcp-uploads-list-item-download { margin-left: auto; } ul.wpcp-uploads-list .loading .loader-spinner { -webkit-animation: loader-full-rotate 2s linear infinite; animation: loader-full-rotate 2s linear infinite; width: 24px; height: 24px; transform-origin: center center; position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; } ul.wpcp-uploads-list .loading .loader-spinner .path { stroke: var(--wpcp--present--color--accent, #590e54); } ul.wpcp-uploads-list .loading .path { stroke-dasharray: 1, 200; stroke-dashoffset: 0; -webkit-animation: loader-full-dash 1.5s ease-in-out infinite, color 6s ease-in-out infinite; animation: loader-full-dash 1.5s ease-in-out infinite, color 6s ease-in-out infinite; stroke-linecap: round; } @media only screen and (max-width : 768px) { #wpcp-modal-upload-action.wpcp-woocommerce-upload-container .modal-dialog { max-width: 95vw; } }jQuery(function ($) { var shareonedrive_wc = { // hold a reference to the last selected OneDrive button lastSelectedButton: false, module: $('#wpcp-modal-selector-onedrive .wpcp-module'), init: function () { // place wpcp container bottom body $('#wpcp-modal-selector-onedrive').parent().appendTo('body'); // add button for simple product this.addButtons(); this.addButtonEventHandler(); // add buttons when variable product added $('#variable_product_options').on('woocommerce_variations_added', function () { shareonedrive_wc.addButtons(); }); // add buttons when variable products loaded $('#woocommerce-product-data').on('woocommerce_variations_loaded', function () { shareonedrive_wc.addButtons(); }); // Select the already added files in the File Browser module this.initSelectAdded(); this.initAddButton(); return this; }, addButtons: function () { let self = this; var button = $( '' + shareonedrive_woocommerce_translation.choose_from + '' ); $('.downloadable_files').each(function (index) { // we want our button to appear next to the insert button var insertButton = $(this).find('a.button.insert'); // check if button already exists on element, bail if so if ($(this).find('a.button.wpcp-insert-onedrive-content').length > 0) { return; } // finally clone the button to the right place insertButton.after(button.clone()); }); /* START Support for WooCommerce Product Documents */ $('.wc-product-documents .button.wc-product-documents-set-file').each(function (index) { // check if button already exists on element, bail if so if ($(this).parent().find('a.button.wpcp-insert-onedrive-content').length > 0) { return; } // finally clone the button to the right place $(this).after(button.clone()); }); $('#wc-product-documents-data').on('click', '.wc-product-documents-add-document', function () { self.addButtons(); }); /* END Support for WooCommerce Product Documents */ }, /** * Adds the click event to the buttons * and opens the OneDrive chooser */ addButtonEventHandler: function () { let self = this; $('#woocommerce-product-data').on('click', 'a.button.wpcp-insert-onedrive-content', function (e) { self.openSelector(); e.preventDefault(); // save a reference to clicked button shareonedrive_wc.lastSelectedButton = $(this); }); $('#wpcp-modal-selector-onedrive .wpcp-dialog-close').on('click', function (e) { self.closeSelector(); }); $('#wpcp-modal-selector-onedrive .wpcp-wc-dialog-entry-select').on('click', function (e) { const account_id = self.module.attr('data-account-id'); const drive_id = self.module.attr('data-drive-id'); const entries_data = self.module .find("input[name='selected-files[]']:checked") .map(function () { const $entry = $(this).parents('.entry'); return { entry_id: $entry.attr('data-id'), entry_name: $entry.attr('data-name'), account_id: account_id, drive_id: drive_id, }; }) .get(); if (entries_data.length === 0) { return self.closeSelector(); } // Send the data via postMessage window.top.postMessage( { slug: 'shareonedrive', action: 'wpcp-select-entries', entries: entries_data, }, window.location.origin ); setTimeout(function () { self.closeSelector(); }, 100); }); }, openSelector: function () { let self = this; window.addEventListener('message', shareonedrive_wc.afterFileSelected); $('#wpcp-modal-selector-onedrive').show(); $('#wpcp-modal-selector-onedrive .wpcp-wc-dialog-entry-select').prop('disabled', 'disabled'); }, closeSelector: function () { window.removeEventListener('message', shareonedrive_wc.afterFileSelected); $('#wpcp-modal-selector-onedrive').fadeOut(); shareonedrive_wc.lastSelectedButton = null; }, /** * Mark already added file in the File Browser moulde */ initSelectAdded: function () { const self = this; self.module.on('wpcp-content-loaded', function (e, plugin) { plugin.element .find("input[name='selected-files[]']:checked") .prop('checked', false) .removeClass('is-selected'); const added_files = $(shareonedrive_wc.lastSelectedButton) .closest('.downloadable_files') .find('.file_url > input') .filter(function (index) { return $(this).val().includes('https://onedrive.com/'); }) .toArray(); added_files.forEach(function (input, index, array) { const url = new URL($(input).val()); const entry_id = url.searchParams.get('id'); const account_id = url.searchParams.get('account_id'); const drive_id = url.searchParams.get('drive_id'); // Show the entry as selected $( '.wpcp-module[data-account-id="' + account_id + '"][data-drive-id="' + drive_id + '"] .entry[data-id="' + entry_id + '"]' ).addClass('is-selected'); }); }); }, /** * Enable & Disable add button based on selection of entries */ initAddButton: function () { let self = this; $(self.module).on( { change: function (e) { if (self.module.find("input[name='selected-files[]']:checked").length) { $('#wpcp-modal-selector-onedrive .wpcp-wc-dialog-entry-select').prop('disabled', ''); } else { $('#wpcp-modal-selector-onedrive .wpcp-wc-dialog-entry-select').prop( 'disabled', 'disabled' ); } }, }, "input[name='selected-files[]']" ); }, /** * Handle selected files */ afterFileSelected: function (event) { if (event.origin !== window.location.origin) { return; } if (typeof event.data !== 'object' || event.data === null || typeof event.data.action === 'undefined') { return; } if (event.data.action !== 'wpcp-select-entries') { return; } if (event.data.slug !== 'shareonedrive') { return; } let files_added = []; let files_failed = []; event.data.entries.forEach(function (entry, index, array) { // Make sure only a single instance of the file can be added if ( $(shareonedrive_wc.lastSelectedButton) .closest('.downloadable_files') .find('.file_url > input') .filter(function (index) { return $(this) .val() .includes( entry.entry_id + '&account_id=' + entry.account_id + '&drive_id=' + entry.drive_id ); }).length ) { files_failed.push(entry.entry_name); return false; } if ($(shareonedrive_wc.lastSelectedButton).closest('.downloadable_files').length > 0) { var table = $(shareonedrive_wc.lastSelectedButton).closest('.downloadable_files').find('tbody'); var template = $(shareonedrive_wc.lastSelectedButton) .parent() .find('.button.insert:first') .data('row'); var fileRow = $(template); fileRow.find('.file_name > input:first').val(entry.entry_name).change(); fileRow .find('.file_url > input:first') .val( 'https://onedrive.com/' + encodeURIComponent(entry.entry_name) + shareonedrive_woocommerce_translation.download_url + entry.entry_id + '&account_id=' + entry.account_id + '&drive_id=' + entry.drive_id ); table.append(fileRow); // trigger change event so we can save variation $(table).find('input').last().change(); } /* START Support for WooCommerce Product Documents */ if ($(shareonedrive_wc.lastSelectedButton).closest('.wc-product-document').length > 0) { var row = $(shareonedrive_wc.lastSelectedButton).closest('.wc-product-document'); row.find('.wc-product-document-label input:first').val(entry.entry_name).change(); row.find('.wc-product-document-file-location input:first').val( shareonedrive_woocommerce_translation.wcpd_url + entry.entry_id + '&account_id=' + entry.account_id + '&drive_id=' + entry.drive_id ); } /* END Support for WooCommerce Product Documents */ // Show the entry as selected $( '.wpcp-module[data-account-id="' + entry.account_id + '"][data-drive-id="' + entry.drive_id + '"] .entry[data-id="' + entry.entry_id + '"]' ).addClass('is-selected'); files_added.push(entry.entry_name); }); if (files_failed.length) { window.showNotification( false, shareonedrive_woocommerce_translation.notification_failed_file_msg.replace( '{filename}', '' + files_failed.join(', ') + '' ) ); } if (files_added.length) { window.showNotification( true, shareonedrive_woocommerce_translation.notification_success_file_msg.replace( '{filename}', '' + files_added.join(', ') + '' ) ); } }, }; window.shareonedrive_wc = shareonedrive_wc.init(); /* Callback function to add shortcode to WC field */ if (typeof window.wpcp_sod_wc_add_content === 'undefined') { window.wpcp_sod_wc_add_content = function (data) { $('#shareonedrive_upload_box_shortcode').val(data); window.modal_action.close(); $('#wpcp-modal-action.ShareoneDrive').remove(); }; } $('input#_uploadable').on('change', function () { var is_uploadable = $('input#_uploadable:checked').length; $('.show_if_uploadable').hide(); $('.hide_if_uploadable').hide(); if (is_uploadable) { $('.hide_if_uploadable').hide(); } if (is_uploadable) { $('.show_if_uploadable').show(); } }); $('input#_uploadable').trigger('change'); $('input#shareonedrive_upload_box').on('change', function () { var shareonedrive_upload_box = $('input#shareonedrive_upload_box:checked').length; $('.show_if_shareonedrive_upload_box').hide(); if (shareonedrive_upload_box) { $('.show_if_shareonedrive_upload_box').show(); } }); $('input#shareonedrive_upload_box').trigger('change'); /* Shortcode Generator Popup */ $('.wpcp-insert-onedrive-shortcode').on('click', function (e) { let shortcode = $('#shareonedrive_upload_box_shortcode').val(); openShortcodeBuilder(shortcode); }); function openShortcodeBuilder(shortcode) { if ($('#wpcp-modal-action.ShareoneDrive').length > 0) { window.modal_action.close(); $('#wpcp-modal-action.ShareoneDrive').remove(); } /* Build the Dialog */ let modalbuttons = ''; let modalheader = $( `
` ); let modalbody = $(''); let modaldialog = $( '
' ); $('body').append(modaldialog); var query = 'shortcode=' + WPCP_shortcodeEncode(shortcode); var $iframe_template = $( "" ); var $iframe = $iframe_template.appendTo(modalbody); $('#wpcp-modal-action.ShareoneDrive .modal-content').append(modalheader, modalbody); $iframe.on('load', function () { $('.wpcp-modal-body').fadeIn(); $('.wpcp-modal-footer').fadeIn(); $('.modal-content .loading:first').fadeOut(); }); /* Open the Dialog */ let modal_action = new RModal(document.getElementById('wpcp-modal-action'), { bodyClass: 'rmodal-open', dialogOpenClass: 'animated slideInDown', dialogCloseClass: 'animated slideOutUp', escapeClose: true, }); document.addEventListener( 'keydown', function (ev) { modal_action.keydown(ev); }, false ); modal_action.open(); window.modal_action = modal_action; } }); jQuery(document).ready(function ($) { 'use strict'; $.widget('cp.ShareOneDriveWC', { options: {}, _create: function () { /* Ignite! */ this._initiate(); }, _destroy: function () { return this._super(); }, _setOption: function (key, value) { this._super(key, value); }, _initiate: function () { let self = this; self._initButtons(); self._initDetails(); }, _initButtons: function () { let self = this; $('.wpcp-shareonedrive .wpcp-wc-open-box').on('click', function (e) { self.openUploadBox($(this)); }); }, _initDetails: function () { let self = this; $('.wpcp-shareonedrive.wpcp-upload-container').each(function (e) { var item_id = $(this).data('item-id'); var listtoken = $(this).find('.ShareoneDrive').data('token'); self._loadDetails(item_id, listtoken); }); }, _loadDetails: function (item_id, listtoken) { let self = this; let $upload_list = $('#wpcp-shareonedrive-uploads-' + item_id + ' .wpcp-uploads-list'); $.ajax({ type: 'POST', url: self.options.ajax_url, data: { action: 'shareonedrive-get-filelist', type: 'wc-item-details', item_id: item_id, listtoken: listtoken, _ajax_nonce: self.options.refresh_nonce, }, complete: function (response) { $upload_list.addClass('wpcp-uploads-received'); }, success: function (response) { if ($.isPlainObject(response) === false || response.length === 0) { $upload_list.addClass('no-uploads-found'); return; } $upload_list.html(''); $.each(response, function (id, entry) { if (entry === '') { return; } $upload_list.append( `
  • ${entry.name}
  • ` ); }); $upload_list.fadeIn(); }, dataType: 'json', }); }, openUploadBox: function (button) { let self = this; var container = button.parent().find('.woocommerce-order-upload-box'); var item_id = button.parent('[data-item-id]').data('item-id'); var listtoken = container.find('[data-token]').data('token'); /* Close any open modal windows */ $('#wpcp-modal-upload-action').remove(); /* Build the Upload Dialog */ let modalbuttons = ``; let modalheader = $( `

    ${container.attr('data-title')}

    ${container.attr( 'data-order' )}
    ${container.attr( 'data-product-name' )}
    ` ); let modalbody = $('
    '); let modalfooter = $( `` ); let modaldialog = $( `
    ` ); $('body').append(modaldialog); $('#wpcp-modal-upload-action .modal-content').append(modalheader, modalbody, modalfooter); /* Fill Textarea */ $('.wpcp-modal-body').append(container); container.show(); /* Set the button actions */ $('#wpcp-modal-upload-action .wpcp-modal-confirm-btn').on('click', function (e) { modal_action.close(); }); /* Open the dialog */ let modal_action = new RModal(document.getElementById('wpcp-modal-upload-action'), { bodyClass: 'rmodal-open', dialogOpenClass: 'animated slideInDown', dialogCloseClass: 'animated slideOutUp', escapeClose: true, afterClose() { container.hide(); button.after(container); self._loadDetails(item_id, listtoken); }, }); document.addEventListener( 'keydown', function (ev) { modal_action.keydown(ev); }, false ); modal_action.open(); window.modal_upload_action = modal_action; return false; }, }); }); // Initiate the Module! jQuery(document).ready(function ($) { $(document).ShareOneDriveWC(ShareoneDrive_vars); });