How to include and use a custom "Java Script" library?

Hi,

one of our customers asked for how to include a library they want to use.

They downloaded “Crypto 3.1.2” from “https://crypto-js.googlecode.com/files/CryptoJS%20v3.1.2.zip” and tried to call the functions from that library.

Finally we ended up with the following questions:

  • Is there a “recipe” about how to use any of those “googled” libraries?
  • How does the code need to be included?
  • If the code needs to be prepared/adopted before it can be used - how?
  • Is there a rule of thumb what should be kept in mind when searching for such code? (must not contain xyz, should not depend on foo)

We understand that custom code might interfere with PCs internal code for pagination etc. and that one must test the output very well, but we need to understand what can be done with JavaScript in PC and what will not work.

I know there were similar questions before, but I’d appreciate a short and precise documentation or guide to follow.

Horrido,

Thomas

Hello Thomas,

Coincidentally, we just had a presentation yesterday from our CEO showing that he had used crypto-js within a template, along many other libraries.

Essentially, the gist of using external librairies is the following:

  • External librairies cannot be used in Scripts, only in the JavaScript resources, as-is. The “Scripts” pane is server-side, does not have access to what you might have included as libraries in the “JavaScript” resources, and the contrary is true too. They are separate concepts.
  • When instructions indicate that a js should be called, you can either download the library, insert it into the JavaScript Resources, and drag the main .js file to your Web context, or if the library is hosted on a CDN, use the “Remote JavaScript” feature:
  • When instructions indicate an actual <script> tag must be used, it should be added either as a regular script in the JavaScript Resources, or simply placed as-is at the very top of the Source view in the Web template.

So essentially, most online librairies will work, those dependant on jQuery will perform better (rather than, say, mootools) because we have a tighter integration with it. Currently, I suggest only using these on Web contexts and not attempt to add these librairies to Print contexts.

Hi Evie,

thanks for your quick response - it helps me to understand.

However, I must again ask for documentation about all that…

If there is no documentation available, I’d like to get a simple example so that we can see what can be done and how.

Unfortunately it seems that OLs main focus is related to “WEB” and “HTML-eMail” - our customers are only printing (at the moment) and before thinking about WEB and HTML-EMails, they need to migrate their existing “Classic PReS”-projects to Connect

I understand that one could use a “Remote JavaScript”, but I know that no one of our customers ever would think about using a remote script due to the risks introduced by accessing and relying on external resources.

Horrido,

Thomas

Hi,
no example available?
No documentation available?

Asked on 28th of september… and no helpful response… :frowning:

Horrido,
Thomas

Hello Thomas,

We believe that anyone who is familiar enough to understand the concept of plugins such as crypto.js should definitely understand at least the basics of how to use it in their template. We understand that there is something of a learning curve here, but it’s not that complex. Essentially, the instructions on any library’s website is applicable to Connect except for the differences noted above in my first reply (inserting into the JavaScript resources and dragging to the context, instead of adding to the <head> tag).

If you need a demo, you can take a look at the Interactive Statement on the demo website. It uses an external library to add charts and graphs in the template.

~Evie

Hi,

What about print section? Im finding myself wanting to reuse a function to format certain values in my document.

cheers
E

Hi @edanting,

Can you let us know please whether your question relates to Print- or Web content?

Can you let also us know please in what kind of scenario you would like to reuse a function to format certain values in your document? Would you like to reuse this function in a number of Control-, Standard- and/or Post Pagination Scripts or would you like to reuse this function in multiple included JavaScript files, for example?

Hi Marten,

I will be reusing the function in Pres Connect Designer - Print Content.
For example I have a netTotal, broughtForward and rebate fields, if the values are negative. I want to remove the - negative and add CR at the end.

With these multiple fields having the same format. I want to just be able to call one function

var netTotal= myCustomFunction(netTotal);
var broughtForward = myCustomFunction(broughtForward );
var rebate = myCustomFunction(rebate );

Basically I want this customFunction to be available in Control, Standard and Pagination. I just want to define this function once. Hope that makes sense.

But I supposed I would like this in the Workflow and Datamapper as well, where I can call a custom function whenever a Run script is present.

Thanks
E

Thank you for answering my questions.

In that case I would like to suggest you to execute the following steps instead:

  1. Add a new Control Script to the Print Template.
  2. Define the function myCustomFunction by the new Control Script.
  3. Optional*: Make sure that the new Control Script has been placed at the top of the list of Control Scripts in the Control folder inside the Scripts pane of the PReS Connect Designer application.

*Optional, in case you would like to use the function in other Control Scripts.

1 Like

Thank you Marten. This definitely works.

I supposed I was looking more to having the script in a network drive where other jobs just refer to it without having to copy and paste these function inside the control script every time we set up a new job.

Appreciate it.
E

1 Like

We have that on our backlog (internal ref [SHARED-87605]). Will upvote for you ;). See also Feature request: Added ability to create Remote Control Script - Designer - Upland OL User community

2 Likes