We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Open file from parent document

Home Forums Puzzles Open file from parent document

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #18410
    hoody
    Participant

    Hey guys Im using the “open file” puzzle to replace a texture on an object.

    Is there a way to use another file uploader in the parent document to replace the texture.

    I´ve got a basic file uploader from wordpress on a woocommere product page and want the user to be able to use this uploader (located in parent document) to change the texture on the object.

    Thanks

    #18411

    Hi,

    You can use an HTML event puzzle with your parent doc (for example, to setup clicks on a div). Inside its do slot just use the open file puzzle. It should work!

    Chief 3D Verger | LinkedIn | Twitter

    #18414
    hoody
    Participant

    yes thanks for your answer. i understand but thats not what im trying to achive.

    I need the image to be fetched from the original wp uploader (located in parent document on a Woocommerce Product page) – because the image is also used for the woocommerce checkout after clicking the add to cart button.

    If I use your uploader there will be no connection to woocommerce, when clicking add to cart.

    do you get what i mean?

    #18416

    So the image already exists and you just need to fetch it? By URL or something?

    Chief 3D Verger | LinkedIn | Twitter

    #18476
    hoody
    Participant

    Im not sure about that, if it actually exists. Basicly what you suggested first was “triggering” the “open file puzzle” from verge by clicking on an element in the parent document.

    But what I need is: triggering an uploader (not the one from verge) that comes from woocommerce and is located in the parent document. Basically its the same file-upload like the one frome verge, but its provided by the woocommerce product itself.

    So what i need is a puzzle or a piece of code that can get the image from an “external” (aor any) uploader located in the parent document. Is there a way to detect if any image was uploaded , not only through the open file puzzle?

    I attached a screenshot for better understanding and also heres the link: http://brudastan.de/produkt/jajaja

    I need to use the file upload from woocommerce, or the image will not be saved wo Cart or checkout page.

    I hope you can now better understand what i meant.

    Greets

    #18478

    Thanks for the details, now I see what you want.

    The open file puzzle creates an input element, “clicks” on it, waits until the image is loaded and then saves the loaded image data to a global variable. You can check the generated code in the visual_logic.js file. It looks like this:

    
    function openFile(callback) {
    
        _pGlob.openedFile = '';
    
        var input = document.createElement('input');
        input.type = 'file';
        input.style = 'display: none';
        // NOTE: fixes iOS event issue
        document.body.appendChild(input);
    
        input.addEventListener('change', function(event) {
            var file = event.target.files[0];
    
            var reader = new FileReader();
    
            reader.addEventListener('load', function () {
                _pGlob.openedFile = reader.result;
                callback();
                document.body.removeChild(input);
            }, false);
    
            reader.readAsDataURL(file);
        });
    
        if (appInstance.controls) {
            appInstance.controls.dispose();
            appInstance.enableControls();
        }
    
        input.click();
    }

    So, instead of that temporary input element, you need to use an existing one provided by Woocommerce. To do that, replace

    var input = document.createElement('input');

    with

    var input = parent.document.getElementsByClassName('wc-pao-addon-file-upload input-text wc-pao-addon-field');

    and remove other lines related to input creation and deletion so that it looks like

    
    function openFile(callback) {
    
        _pGlob.openedFile = '';
    
        var input = parent.document.getElementsByClassName('wc-pao-addon-file-upload input-text wc-pao-addon-field');
    
        input.addEventListener('change', function(event) {
            var file = event.target.files[0];
    
            var reader = new FileReader();
    
            reader.addEventListener('load', function () {
                _pGlob.openedFile = reader.result;
                callback();
    
            }, false);
    
            reader.readAsDataURL(file);
        });
    
        if (appInstance.controls) {
            appInstance.controls.dispose();
            appInstance.enableControls();
        }
    
        input.click();
    }

    The changes in visual_logic.js will be dropped once you save Puzzles again, but at least give this a try.

    Chief 3D Verger | LinkedIn | Twitter

    #18480
    hoody
    Participant

    Thank you for your answer! When I try your code , i get an error in my console.

    v3d.js:1 TypeError: input.addEventListener is not a function (its reffering to line 8 in your code)

    #18482

    May be the rest of the document has not loaded yet. Try calling open file upon mouse click or something.

    Chief 3D Verger | LinkedIn | Twitter

    #18496
    hoody
    Participant

    Hi Thanks. now the error is gone, but the fuction is not executed.

    maybe you could take a look at my code (click event):

    eventHTMLElem(‘click’, ‘wc-pao-addon-file-upload input-text wc-pao-addon-field’, true, function(event) {
    console.log(“trigger”)
    openFile(function() {
    replaceTexture(‘vladmaterial’, ‘vlad.jpg’, _pGlob.openedFile, function() {});
    });
    });

    #18517

    Hi,

    This won’t work with class name. Try creating your own button or use a timer puzzle.

    Chief 3D Verger | LinkedIn | Twitter

    #18542
    hoody
    Participant

    I seem to have fixed it! Thank you very much for your kind support!

    #18544

    Excellent! :good:

    Chief 3D Verger | LinkedIn | Twitter

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.