Including Moment.js in LiveCycle Designer - javascript

For few days I've been trying to include the actual Moment.js library in my dynamic PDF which I've created with Adobe Livecycle Designer.
We've used an older version (1.7.2) without any problems but now I only get a 'Function not exist' error.
Does anyone have any expierience with this?
Thanks in advance.

How to include Moment.js in an Adobe LiveCycle Form:
Download the minified script
In LiveCycle Designer open your form and create a Script Object called MOMENTJSMIN
Copy the minified script into that Script Object
In the Script Editor window of LiveCycle Designer, edit MOMENTJSMIN Script Object in the following manner:
Remove all the script up to but not including the second curly brace { :
!function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):a.moment=b()}(this,function()
Remove the rounded parenthesis and semicolon from the end of the minified script
Add this line to the beginning of the minified script:
if (xfa.momentjs == undefined) xfa.momentjs = function()
In the MOMENTJSMIN Script Object add this function after the end of the script:
function getMomentJS(){
return xfa.momentjs();
}
Now your MOMENTJSMIN script object is set up to provide Moment.js to scripts throughout your form.
To use Moment.js in any of your script, start your script object or event script with this line:
var moment = MOMENTJSMIN.getMomentJS();
Now you can use moment() anywhere in the script that starts with that line. eg:
var moment = MOMENTJSMIN.getMomentJS();
var jan07 = moment([2007, 0, 29]);
app.alert(moment().format("dddd, MMMM Do YYYY, h:mm:ss a"));
app.alert(jan07.format("dddd, MMMM Do YYYY") + " was " + jan07.fromNow());
app.alert(moment.isDate(new Date()));

What I would check first:
Make sure your script is fully loaded before trying to invoke functions from it. (check the event, where you call the function-calculate, form:readty etc.)
Check the script referencing. Right path? Right name?
Check if the function really exists
Check function parameters.

Related

Edit a function in Jquery.validate.js validation for date dd/mm/yyyy

I'm having a problem to validate the date field in my system (asp.net). I already standardized the fields in the class with annotations:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
It's working quite good, showing me the fields like i want, but when i try to edit, the forms show me a error like this:
The field [field] must be a date.
So i can't save the editions because it's doesn't pass by the jquery validator.
I've standardized in c# annotations to dd/mm/yyy but the jquery is set to mm/dd/yyyy.
During my tests, i found (i guess) that one of this files is that one which makes the validation for the date field in the forms:
jquery.validate.js
jquery.validate.unobtrusive.js
I found this because when i remove this scripts from the page, the validation doesn't work, but i'm not sure also if this scripts call anothers.
And in the jquery.validate.js i found this functions and i tried to edit it in many ways, but without success:
date: function( value, element ) {
return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
},
dateISO: function( value, element ) {
return this.optional(element) || /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/.test(value);
},
I'm not sure that i'm modifying the right thing, so i need a hand here, please.
As i don't know what to modify, here's the link to jquery.validate.js.
https://github.com/KaioMartins/PersonalLibrary/blob/master/PersonalLibrary/Scripts/jquery.validate.js
You can write your own parser function, as #RobG suggested, OR you can modify the original one, if you don't use it in your page.
I use old versions of Globalize plugin and there was some functions to change the 'language' for validation plugin, as numbers with 1.234,56 format (instead standard 1,234.56 english format) and so on...
In your case, you can use:
$.validator.methods.date = function (value, element) { /* place your function here */ }
to override the default behavior.
And you can find reg expr for dd/MM/yyyy here:
Regex to validate date format dd/mm/yyyy
EDIT:
Where do you do this? Ok, in your own javascript code, just after load jquery.validate plugin. You can wait to onReady event, too.
<script src="jquery.validate.min.js"></script>
<script>
$(function(){
$.validator.methods.date = ...
})
<script>

JavaScript: Change objectHTMLScriptElement

I am currently working within a CMS, cleaning up 12 websites.
Currently there are 12 identical JS files each residing within their respective site. Since they are all the same, my first initiative is to point every site to a single JS file that lives on the server.
Because I'm working within a CMS, I would have to open up 200 templates to accomplish this feat manually, so I'd, of course, rather do this dynamically.
Here is what I have done, so far:
var scripts = document.getElementsByTagName ("script");
console.log(scripts[15]);
My console.log statement returns what I'd like to replace, which is this:
<script src="/Assets/AmericanTower.com.br/uploads/content/js/main.js">
When I use alert(); rather than console.log(); I get this:
[object HTMLScriptElement]
I don't really understand why alert and console.log are showing me 2 different results.
So, I gather that I need to find a way to convert this HTMLElement to a string and then replace the string(or part of it) with the path to my new JS file.
Can anyone please shed some light?
Thank you in advance!
Robin
===============================
Thank you, L.C. Echo Chan, for your contribution. Here's how I used your suggestion and it worked like a charm!
var scripts = document.getElementsByTagName("script");
var jsPath=scripts[15].outerHTML;
var changedURL=jsPath.replace(jsPath,"RegionalGlobalAssets");
alert(changedURL);
Using outerHTML property can return the entire element
var scripts = document.getElementsByTagName("script");
alert(scripts[15].outerHTML);
Try to set you code as that
var scripts = document.getElementsByTagName("script");
console.log(scripts[15]);

Auto calculate todays date when doc opens and add days to date in another field

I am using Adobe XI Standard and have a pdf document with a text field “Today” with a Mouse Up Java script of
var f = this.getField("Today");
f.value = util.printd("mmm/d/yyyy", new Date());
Problem 1) I want this to automatically update when the document opens instead of when the mouse enters it but I dont know how or where to place the script in the proper place.
I have a text field labeled “text11” formatted to be a date mm/dd/yyyy
I have another text field labeled “21stDay” which I want to calculate 21 days from the date in the “text11” field.
Problem 2) I have not been able to get any script to work. Can anyone please tell me how to make this work properly and where to exactly place the scripts as I am just a novice at doing this.
Thank you in advance!
I believe that there are two potential ways to do this:
An OpenAction entry in the Catalog root with an Action dictionary of S(ub)type JavaScript
Named JavaScripts seem to be executed when the document is first opened in Acrobat.
For the fomer see table 3.25 in section 3.6.1 and section 8.5 in the PDF v1.7 Reference, and section 3.6.3 for the latter.
The first thing you can do with a Document-level script containing just that single line:
this.getField("Today").value = util.printd("mmm/d/yyyy", new Date()) ;
If you encounter timing issues (meaning that the field does not get filled reliably, use the PageOpen event of the page on which the document opens. This makes sure that the script is run only when the document is properly loaded.
For the second question, this should work (not I have not verified the code):
var now = new Date() ;
var then = new Date() ;
then.setDate(now.getDate+21) ;
this.getField("text11").value = util.printd("mm/dd/yyyy", then) ;
Note that the Date object is smart enough to properly convert a date number greater than the date of the end of month.

How to include more JavaScript inside Atom editor by Github?

I've been playing with snippets for Atom editor,
and see that I've learned I can include
JavaScript inside of a snippet, as my example shows.
(It inserts a TODO Comment with date)
TODO: (RAM) Fix this - 2014-11-23 20:55
HELLO
How can I include MORE JavaScript.?
For example
inside the snippet to set
var= to something
or
call a JS library
or
ask for input from user i.e. confirm();
and proceed on basis of confirm() function.
Any help would be appreciated, I looked for a long time,
but not much documentation on this. Submlime Text's snippets allowed lots of code to be inserted via Python.
Thanks
~Rob
Inside file snippets.cson
'.source.js':
'Date TODO: insert':
'prefix': 'datetd'
'body': """
TODO: (RAM) $1 - #{datetime = new Date(); datetime.getFullYear()}-#{(datetime.getMonth()+1)}-#{datetime.getDate()} #{datetime.getHours()}:#{datetime.getMinutes()}
#{"hello".toUpperCase(); }
$2
"""
Update: With the merge of atom/atom#4791, the capability of putting CoffeeScript code in CSON configuration files has been removed from Atom. The rest of the answer has been left intact for historic reference.
Actually, the syntax of the file is CoffeeScript (hence .cson as in CoffeeScript Object Notation), not JavaScript. It just so happens that you typed in JavaScript that is allowed as CoffeeScript. CoffeeScript doesn't use the var keyword, so you can assign variables like you did in your example:
datetime = new Date()
The other items, you'll probably have to get a little creative. I don't believe that the snippets package was intended to be used in this manner, which is why the lack of documentation on the "feature".

ASP.NET inline server tags

I'd like to start by saying that my code is working perfectly, this is more a "how best to do it" kind of question.
So I have code like this in my .aspx file:
function EditRelationship() {
var projects=<%= GetProjectsForEditRelationship() %>;
// fill in the projects list
$('#erProjectsSelect').empty();
for(var i in projects)
$('#erProjectsSelect').append('<option value='+projects[i][0]+'>'+projects[i][1]+'</option>');
var rels=<%= GetRelationshipsForEditRelationship() %>;
// etc
}
Again, it's working fine. The problem is that VS2008 kinda chokes on code like this, it's underlining the < character in the tags (with associated warnings), then refusing to provide code completion for the rest of the javascript. It's also refusing to format my document anymore, giving parsing errors. The last part is my worst annoyance.
I could put some of these in evals I guess, but it seems sorta dumb to add additional layers and runtime performance hits just to shut VS up, and it's not always an option (I can't remember off the top of my head where this wasn't an option but trust me I had a weird construct).
So my question is, how do you best write this (where best means fewest VS complaints)? Neither eval nor ajax calls fit this imo.
If your aim is to reduce VS complaints, and if you are running asp.net 4 (supporting Static client Ids), maybe a strategy like the following would be better?
Create a ASP:HiddenField control, set its ClientIdMode to "Static"
Assign the value of GetRelationshipsForEditRelationship() to this field on page load
In your javascript, read the value from the hidden field instead, I assume you know how to do this.
It's more work than your solution, and you will add some data to the postback (if you perform any) but it won't cause any VS complaints I guess :)
You could do this from your page in the code-behind
ClientScript.RegisterArrayDeclaration("projects", "1, 2, 3, 4");
or to construct something like JSON you could write it out
ClientScript.RegisterClientScriptBlock(GetType(), "JSONDeclarations", "your json stuff");
UPDATE Based on my comment
<script id="declaration" type="text/javascript">
var projects=<%= GetProjectsForEditRelationship() %>;
var rels=<%= GetRelationshipsForEditRelationship() %>;
</script>
<script type="text/javascript">
function EditRelationship() {
// fill in the projects list
$('#erProjectsSelect').empty();
for(var i in projects)
$('#erProjectsSelect').append('<option value='+projects[i][0]+'>'+projects[i][1]+'</option>');
}
</script>
I don't have VS2008 installed to test with, so take this with a grain of salt, but have you tried something like this?
var projects = (<%= GetProjectsForEditRelationship() %>);
Something like that might trick the JavaScript parser into ignoring the content of your expression.
For what it's worth, VS2010 correctly parses and highlights your original code snippet.
Is it an option to move this to VS2010? I just copied and pasted your code and the IDE interpreted it correctly.
The best solution is to put javascript in a separate file and avoid this entirely. For this particular function, you're doing server-side work. Why not build the list of options that you intend to add dynamically in codebehind, put them in a hidden div, and then just have jQuery add them from the already-rendered HTML?
If you have a situation where you really want to dynamically create a lot javascript this way, consider using ScriptManager in codebehind to set up the variables you'll need as scripts and register them, then your inline script won't need to escape
ScriptManager.RegisterClientScript("projects = " + GetProductsForEditRelationship());
(Basically, that is not the complete syntax, which is context dependent). Then refer to "projects" in your function.
(edit)
A little cleaner way to do this on a larger scale, set up everything you need like this in codebehind:
string script = "var servervars = {" +
"GetProductsForEditRelationship: " + GetProductsForEditRelationship() +
"GetRelationshipsForEditRelationship: " + GetRelationshipsForEditRelationship() +
"}"
and refer to everything like:
servervars.GetProductsForEditRelationship
If you do this a lot, of course, you can create a class to automate the construction of the script.

Categories

Resources