Script only executes once after reboot

Started by Ger, June 11, 2017, 09:13:08 AM

Previous topic - Next topic

Ger

I'm trying to get some interactivity in a test script (radio buttons, check buttons, input fields) and I created a script that:
- analyzes the filewindow and displays the number of files, selected files, files with pins upon start. Works fine
- pressing a button takes the selected file set for further processing. Currently is only retrieves some data for the selected files and shows that data in the console.

If I run this script after rebooting my pc, it works fine. Just once. If I rerun the script it won't show the data in the console. What am I missing here? (execution after pc reboot works always; closing and opening IMatch does not do the trick)


<!DOCTYPE html>
<html>

<head>
    <!--These lines should always be the first-->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title imt-translate>GDe - attribute handling script</title>

    <!-- <link rel="stylesheet" href="./gde.css" /> -->

    <!--These includes are typical for an IMatch App-->
    <script src="/system/jquery/dist/jquery.min.js"></script>
    <script src="/system/imws/imwslib.js"></script>
    <script src="/system/imatch/imatchlib.js"></script>
</head>

<body>
    <div id="container">
        <div id="map_container">
            <h2>Attribute handling script</h2>
            <br>

            <form name='FileSelection'>
                <table class="noborder">
                    <tr>
                        <td>
                            <input type='radio' name='FileList' value='AllFiles' id='idAllFiles' checked>
                            <label for='idAllFiles' id='CountAllFiles'> </label>
                        </td>
                        <td>
                            <input type='radio' name='FileList' value='SelectedFiles' id='idSelectedFiles'>
                            <label for='idSelectedFiles' id='CountSelectedFiles'> </label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type='radio' name='FileList' value='RedPins' id='idPinsRed'>
                            <label for='idPinsRed' id='CountPinsRed'> </label>
                        </td>
                        <td>
                            <input type='radio' name='FileList' value='GreenPins' id='idPinsGreen'>
                            <label for='idPinsGreen' id='CountPinsGreen'> </label>
                        </td>
                        <td>
                            <input type='radio' name='FileList' value='BluePins' id='idPinsBlue'>
                            <label for='idPinsBlue' id='CountPinsBlue'> </label>
                        </td>
                        <td>
                            <input type='radio' name='FileList' value='NoPins' id='idPinsNone'>
                            <label for='idPinsNone' id='CountPinsNone'> </label>
                        </td>
                    </tr>

                    <td><button id="btn-renumber-activepage" class="btn btn-default btn-sm">Renumber ActivePage</button></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </form>
            <hr>
        </div>
    </div>



    <script>
        $(document).ready(function () {
            var theCollection = {
                'PinsRed': { 'Count': 0, 'List': '' },
                'PinsGreen': { 'Count': 0, 'List': '' },
                'PinsBlue': { 'Count': 0, 'List': '' },
                'PinsNone': { 'Count': 0, 'List': '' },
                }

            // Start here: count the various options
            countOptions();

            function countOptions() {

                // Files in selection
                IMWS.get('v1/files', { idlist: IMatch.idlist.fileWindowSelection, fields: 'id,collections' }).then(function (response) {
                    $('#CountSelectedFiles').text("Selected files: " + response.files.length);
                });

                // Files in filewindow
                IMWS.get('v1/files', { idlist: IMatch.idlist.fileWindowFiles, fields: 'id,collections' }).then(function (respFW) {
                    $('#CountAllFiles').text("All files: " + respFW.files.length);

                    //console.log(JSON.stringify(respFW, null, 2));
                    respFW.files.forEach(function (record) {
                        for (var x = 0; x < record.collections.length; ++x) {
                            switch (record.collections[x]) {
                                case 51:
                                    theCollection.PinsRed.Count++;
                                    theCollection.PinsRed.List += record.id + ',';
                                    break;
                                case 52:
                                    theCollection.PinsGreen.Count++;
                                    theCollection.PinsGreen.List += record.id + ',';
                                    break;
                                case 53:
                                    theCollection.PinsBlue.Count++;
                                    theCollection.PinsBlue.List += record.id + ',';
                                    break;
                                case 54:
                                    theCollection.PinsNone.Count++;
                                    theCollection.PinsNone.List += record.id + ',';
                            }
                        }
                    });
                    $('#CountPinsRed').text('Red Pins: ' + theCollection.PinsRed.Count);
                    $('#CountPinsGreen').text('Green Pins: ' + theCollection.PinsGreen.Count);
                    $('#CountPinsBlue').text('Blue Pins: ' + theCollection.PinsBlue.Count);
                    $('#CountPinsNone').text('No Pins: ' + theCollection.PinsNone.Count);
                });

            }

            function getFiles() {
                console.log('PinsRed: ' + theCollection.PinsRed.List);
                IMWS.get('v1/files', {
                    id: theCollection.PinsRed.List,
                    fields: 'id,filename'
                }).then(function (response) {
                    console.log(JSON.stringify(response, null, 2));
                    //console.log(response.files.length);
                }, function (error) {
                    console.log('error ' + error.status);
                });
            }

            // Renumber Active Page button clicked
            $('#btn-renumber-activepage').click(function () {
                theCollection.PinsRed.List = theCollection.PinsRed.List.substr(0, theCollection.PinsRed.List.length - 1);
                getFiles();
            });

        });
    </script>

</body>

</html>

Mario

When you close the App Panel, IMatch releases the associated Chromium instance and all associated resources. This will also stop all running scripts for that instance. If you close IMatch, all Chromium browser instances will be closed. And optionally even the cache will be deleted (if enabled).

If you actually have to reboot your PC  :o to make your script working again, something really, really strange seems to happen. I never experienced such a thing.

I have currently no time to debug scripts written by users, sorry.

I would first do this:

+ Did you check your script for infinite loops or similar? Maybe add some console.log(...) calls to see what happens?
+ What does the Performance tab in the Task Manager tell you? Is your script creating a high CPU load?
+ Did you check the IMatch log file (and the IMATCH6D_CEFLOG) for problem reports?
+ Did you look at the Output panel in IMatch to see if Chromium has errors to report?
+ Did you run your script in an external browser using the debugger?
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Ger


Quote
+ Did you check your script for infinite loops or similar? Maybe add some console.log(...) calls to see what happens?

To my understanding I only do retrieve file information (/v1/files) and show the response in the console.

Quote
+ What does the Performance tab in the Task Manager tell you? Is your script creating a high CPU load?

I don't see anything strange. No high CPU at all. But I'm not an expert.

Quote
+ Did you check the IMatch log file (and the IMATCH6D_CEFLOG) for problem reports?

No strange things (to my unexperienced eyes) in the regular log file. The CEFLOG-file (attached) opens with an error message. Script output seems similar to console output

Quote
+ Did you look at the Output panel in IMatch to see if Chromium has errors to report?

Output Panel | Apps is also the console output panel, never seen any chrome error here.

Quote
+ Did you run your script in an external browser using the debugger?

I just installed Google Chrome and ran the script. Output seems similar, but the debugger screen and options require some understanding before drawing conclusions :)

I'm definitely going to try other things; if in the meantime somebody has ideas (or test-runs the script on his/her computer), please let me know!

Ger

Mario

I see some problems in your script, but nothing that should cause this.
I can run the script multiple times by refreshing it with <F5>. No problems.

1. Beginner error (I still fall for that at times):

You have  <form> with a button. When you click the button, the browser will "submit" the form to the server, causing a reload of the page (and all scripts). Not what you want in this case. Change the handler for the button as follows:

$('#btn-renumber-activepage').click(function (event) {
  theCollection.PinsRed.List = theCollection.PinsRed.List.substr(0, theCollection.PinsRed.List.length - 1);
  getFiles();
  event.preventDefault();
  return false;
});

event.preventDefault disables the submit. The same does the return false, but not in all browsers. Best to use both.

2.

During my first test I wondered why no output was showing in the console. I waited a bit and then your app dumped output for all files in the database (Yikes!).
My database has no red pins set so the getFiles() function which uses theCollection.PinsRed.List actually used an empty id parameter, and this returns all files. Not a problem when you have files with a red pin and I'm sure you will change that to use whatever the user has selected in the radio butons in your form.

Besides that, the app seems to do what you intended.

-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Ger

Mario,
Thanks for looking into it.
I continue to play (and fail)...

The beginner error... I never worked (much) with html, just some basic things, so another lesson learned.
The second error, that would definitely be a check you want to add, for now I'm already too happy if I get something working... I try to please the script as much as possible by creating perfect circumstances :)

To be continued...

Mario

QuoteI continue to play (and fail)...

That's how you learn programming. Or electronics (usually with more smoke, though). And basically everything else...

QuoteThe second error, that would definitely be a check you want t

Note. As documented, /files returns all files in the database if neither id nor idlist nor path are specified. That's intentional.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook