Integrating JavaScript web plugin in Ionic 2

555 views Asked by At

Im trying to integrate Applozic chat platform to my Ionic 2 project, which I wish to export to Web, Android and iOS. Using the sample as a base and created the applozic.d.ts & applozichv.js for the Javascript integration process.

applozic.d.ts

interface AppLozicStatic {
    initPlugin(): any;
}

declare var AppLozic : AppLozicStatic;
export = AppLozic;

applozichv.js

(function () {
    var root = this;

    var AppLozic = function (obj) {
        if (obj instanceof AppLozic) return obj;
        if (!(this instanceof AppLozic)) return new AppLozic(obj);
        // this.EXIFwrapped = obj;
    };

    if (typeof exports !== 'undefined') {
        if (typeof module !== 'undefined' && module.exports) {
            exports = module.exports = AppLozic;
        }
        exports.AppLozic = AppLozic;
    } else {
        root.AppLozic = AppLozic;
    }

    AppLozic.initPlugin = function () {
        var $original;
        // var $applozic = "";
        var $applozic;
        console.log("initPlugin");

        $original = jQuery.noConflict(true);
        $ = $original;
        jQuery = $original;


        if (typeof jQuery !== 'undefined') {
            console.log("initPlugin 1");


            $applozic = jQuery.noConflict(true);

            $applozic.fn
                .applozic({

                    baseUrl : 'https://apps.applozic.com',
                                    userId : 'debug454545', //TODO: replace userId with actual UserId
                                    userName : 'test',          //TODO: replace userId with actual UserName
                                        appId : 'applozic-sample-app',
//                        accessToken: 'suraj',                             //TODO: set user access token.for new user it will create new access token

                    ojq: $original,
                    // obsm: oModal,

                    //optional, leave it blank for testing purpose, read this if you want to add additional security by verifying password from your server https://www.applozic.com/docs/configuration.html#access-token-url
                    //  authenticationTypeId: 1,    //1 for password verification from Applozic server and 0 for access Token verification from your server
                    //  autoTypeSearchEnabled : false,
                    //  messageBubbleAvator: true,
                    notificationIconLink: "https://www.applozic.com/resources/images/applozic_icon.png",
                    notificationSoundLink: "",
                    readConversation: readMessage, // readMessage function defined above
                    onInit: onInitialize, //callback function execute on plugin initialize
                    maxAttachmentSize: 25, //max attachment size in MB
                    desktopNotification: true,
                    locShare: true,
                    video: true,
                    topicBox: true,
//                        mapStaticAPIkey: "AIzaSyCWRScTDtbt8tlXDr6hiceCsU83aS2UuZw",
//                        googleApiKey: "AIzaSyDKfWHzu9X7Z2hByeW4RRFJrD9SizOzZt4" // replace it with your Google API key
                    // initAutoSuggestions : initAutoSuggestions //  function to enable auto suggestions
                });
        }
        var oModal = "";

        /*if (typeof $original !== 'undefined') {
            $ = $original;
            jQuery = $original;
            if (typeof $.fn.modal === 'function') {
                oModal = $.fn.modal.noConflict();
            }
        } else {
            $ = $applozic;
            jQuery = $applozic;
            if (typeof $applozic.fn.modal === 'function') {
                oModal = $applozic.fn.modal.noConflict();
            }
        }*/

        //Sample json contains display name and photoLink for userId


        function readMessage() {
            //console.log(userId);
        }


        //callback function execute after plugin initialize.
        function onInitialize(response, data) {
            if (response.status === 'success') {
                // $applozic.fn.applozic('loadContacts', {'contacts':contactsJSON});
                // $applozic.fn.applozic('loadTab', 'shanki.connect');
                //write your logic exectute after plugin initialize.
                alert("success");
            } else {
                alert(response.errorMessage);
            }
        }


        // init();

    };


})();

I added all the above created files including applozic.common.js, applozic.fullview.js and jquery.min.js to the assets/js folder and linked them in my index.html. This was the only way I could get the JavaScript method in applozic.js to execute from my chat.ts.

The problem that i'm now facing is the I get the error:

TypeError: Cannot read property 'noConflict' of undefined` in `applozic.js`

on the line

$original = jQuery.noConflict(true);

and consequently the rest of the if block is also not executing.

In an attempt to make jQuery work in the project I tried to install it via NPM by executing the following commands:

npm install jquery --save
npm install @types/jquery --save

But this led to issue in ionic serve giving the following error:

JavaScript heap out of memory

I really need help in executing my applozic.js file to initialize and call the chat plugin functions.

1

There are 1 answers

0
Mayur Kukadiya On

You should first identify the actual problem.

If problem is in jquery installation, then you can debug it using following line.

console.log("Jwuery Text : " , $('.testClassName'));

If below log is completely printed in the console then there is no mistake in jquery.

This memory size issue is resolved using reInitialize memory heap by using this command :

set NODE_OPTIONS=--max_old_space_size=4096
or
node --max_old_space_size=4096

After this command run into the command prompt, try to serve project or build a project.