Guest User

Untitled

a guest
Aug 12th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. //Uses the namespace pattern from http://stackoverflow.com/a/5947280
  2. // expects to extend htmlForm defined in the core HFE module
  3. (function( htmlForm, jq, undefined) {
  4.  
  5. // individual forms can define their own functions to execute before a form validation or submission by adding them to these lists
  6. // if any function returns false, no further functions are called and the validation or submission is cancelled
  7. var beforeValidation = new Array(); // a list of functions that will be executed before the validation of a form
  8. var beforeSubmit = new Array(); // a list of functions that will be executed before the submission of a form
  9. var propertyAccessorInfo = new Array();
  10.  
  11. var whenObsHasValueThenDisplaySection = { };
  12.  
  13. var tryingToSubmit = false;
  14.  
  15. var returnUrl = '';
  16.  
  17. var disableSubmitButton = function() {
  18. jq('.submitButton.confirm').attr('disabled', 'disabled');
  19. jq('.submitButton.confirm').addClass("disabled");
  20. jq('.submitButton.confirm .icon-spin').css('display', 'inline-block');
  21. }
  22.  
  23. var enableSubmitButton = function() {
  24. jq('.submitButton.confirm').removeAttr('disabled', 'disabled');
  25. jq('.submitButton.confirm').removeClass("disabled");
  26. jq('.submitButton.confirm .icon-spin').css('display', 'none');
  27. }
  28.  
  29. var submitButtonIsDisabled = function() {
  30. return $(".submitButton.confirm").is(":disabled");
  31. }
  32.  
  33. var findAndHighlightErrors = function() {
  34. /* see if there are error fields */
  35. var containError = false
  36. var ary = jq(".autoCompleteHidden");
  37. jq.each(ary, function(index, value){
  38. if(value.value == "ERROR"){
  39. if(!containError){
  40.  
  41. // TODO: get this localized? are we even using this?
  42. alert("Autocomplete answer not valid");
  43. // alert("${ ui.message("htmlformentry.error.autoCompleteAnswerNotValid") }");
  44. var id = value.id;
  45. id = id.substring(0,id.length-4);
  46. jq("#"+id).focus();
  47. }
  48. containError=true;
  49. }
  50. });
  51. return containError;
  52. }
  53.  
  54. /*
  55. It seems the logic of showAuthenticateDialog and
  56. findAndHighlightErrors should be in the same callback function.
  57. i.e. only authenticated user can see the error msg of
  58. */
  59. var checkIfLoggedInAndErrorsCallback = function(isLoggedIn) {
  60.  
  61. var state_beforeValidation=true;
  62.  
  63. if (!isLoggedIn) {
  64. showAuthenticateDialog();
  65. }else{
  66.  
  67. // first call any beforeValidation functions that may have been defined by the html form
  68. if (beforeValidation.length > 0){
  69. for (var i=0, l = beforeValidation.length; i < l; i++){
  70. if (state_beforeValidation){
  71. var fncn=beforeValidation[i];
  72. state_beforeValidation=fncn.call(htmlForm);
  73. }
  74. else{
  75. // forces the end of the loop
  76. i=l;
  77. }
  78. }
  79. }
  80.  
  81. // only do the validation if all the beforeValidation functions returned "true"
  82. if (state_beforeValidation) {
  83. var anyErrors = findAndHighlightErrors();
  84.  
  85. if (anyErrors) {
  86. tryingToSubmit = false;
  87. return;
  88. } else {
  89. doSubmitHtmlForm();
  90. }
  91. }
  92. else {
  93. tryingToSubmit = false;
  94. }
  95. }
  96. }
  97.  
  98. var showAuthenticateDialog = function() {
  99. jq('#passwordPopup').show();
  100. tryingToSubmit = false;
  101. }
  102.  
  103. // if an encounter id is passed in, that is appended to the return string
  104. var goToReturnUrl = function(encounterId) {
  105. if (returnUrl) {
  106. location.href = returnUrl
  107. + (encounterId ? (returnUrl.indexOf('?') != -1 ? '&' : '?') +"encounterId=" + encounterId : '');
  108. }
  109. else {
  110. if (typeof(parent) !== 'undefined') {
  111. parent.location.reload();
  112. } else {
  113. location.reload();
  114. }
  115. }
  116. }
  117.  
  118. var doSubmitHtmlForm = function() {
  119.  
  120. // first call any beforeSubmit functions that may have been defined by the form
  121. var state_beforeSubmit=true;
  122. if (beforeSubmit.length > 0){
  123. for (var i=0, l = beforeSubmit.length; i < l; i++){
  124. if (state_beforeSubmit){
  125. var fncn=beforeSubmit[i];
  126. state_beforeSubmit=fncn();
  127. }
  128. else{
  129. // forces the end of the loop
  130. i=l;
  131. }
  132. }
  133. }
  134.  
  135. // only do the submit if all the beforeSubmit functions returned "true"
  136. // also, hack to double check to disallow form submittal if submit button is disabled (prevent multiple submits)
  137. if (state_beforeSubmit && !submitButtonIsDisabled()){
  138. disableSubmitButton();
  139. var form = jq('#htmlform');
  140. jq(".error", form).text(""); //clear errors
  141. //ui.openLoadingDialog('Submitting Form');
  142. $.ajax({
  143. url: form.attr('action'),
  144. data: new FormData(form.get(0)),
  145. type : "POST",
  146. contentType: false,
  147. processData: false,
  148. success : function(result) {
  149. if (result.success) {
  150. if (result.goToUrl) {
  151. emr.navigateTo({ applicationUrl: result.goToUrl });
  152. } else {
  153. goToReturnUrl(result.encounterId);
  154. }
  155. } else {
  156. //ui.closeLoadingDialog();
  157. enableSubmitButton();
  158. for (key in result.errors) {
  159. showError(key, result.errors[key]);
  160. }
  161. // scroll to one of the errors
  162. // TODO there must be a more efficient way to do this!
  163. for (key in result.errors) {
  164. jq(document).scrollTop(jq('#' + key).offset().top - 100);
  165. break;
  166. }
  167.  
  168. //ui.enableConfirmBeforeNavigating();
  169. }
  170. },
  171. error : function(jqXHR, textStatus, errorThrown) {
  172. //ui.closeLoadingDialog();
  173. //ui.enableConfirmBeforeNavigating();
  174.  
  175. emr.errorAlert('Unexpected error, please contact your System Administrator: ' + textStatus);
  176. }
  177. });
  178.  
  179. //jq.post(form.attr('action'), form.serialize(), function(result) {
  180. // if (result.success) {
  181. // if (result.goToUrl) {
  182. // emr.navigateTo({ applicationUrl: result.goToUrl });
  183. // } else {
  184. // goToReturnUrl(result.encounterId);
  185. // }
  186. // } else {
  187. // //ui.closeLoadingDialog();
  188. // enableSubmitButton();
  189. // for (key in result.errors) {
  190. // showError(key, result.errors[key]);
  191. // }
  192. // // scroll to one of the errors
  193. // // TODO there must be a more efficient way to do this!
  194. // for (key in result.errors) {
  195. // jq(document).scrollTop(jq('#' + key).offset().top - 100);
  196. // break;
  197. // }
  198. //
  199. // //ui.enableConfirmBeforeNavigating();
  200. // }
  201. // }, 'json')
  202. // .error(function(jqXHR, textStatus, errorThrown) {
  203. // //ui.closeLoadingDialog();
  204. // //ui.enableConfirmBeforeNavigating();
  205. //
  206. // emr.errorAlert('Unexpected error, please contact your System Administrator: ' + textStatus);
  207. // });
  208. }
  209. tryingToSubmit = false;
  210. };
  211.  
  212. htmlForm.submitHtmlForm = function() {
  213. if (!tryingToSubmit) { // don't allow form submittal if submit button is disabled (disallows multiple submits)
  214. tryingToSubmit = true;
  215. jq.getJSON(emr.fragmentActionLink('htmlformentryui', 'htmlform/enterHtmlForm', 'checkIfLoggedIn'), function(result) {
  216. checkIfLoggedInAndErrorsCallback(result.isLoggedIn);
  217. });
  218. }
  219. };
  220.  
  221. htmlForm.loginThenSubmitHtmlForm = function() {
  222. jq('#passwordPopup').hide();
  223. var username = jq('#passwordPopupUsername').val();
  224. var password = jq('#passwordPopupPassword').val();
  225. jq('#passwordPopupUsername').val('');
  226. jq('#passwordPopupPassword').val('');
  227. jq.getJSON(emr.fragmentActionLink('htmlformentryui', 'htmlform/enterHtmlForm', 'authenticate', { user: username, pass: password }), submitHtmlForm);
  228. };
  229.  
  230. htmlForm.cancel = function() {
  231. goToReturnUrl();
  232. };
  233.  
  234. htmlForm.getValueIfLegal = function(idAndProperty) {
  235. var jqField = getField(idAndProperty);
  236. if (jqField && jqField.hasClass('illegalValue')) {
  237. return null;
  238. }
  239. return getValue(idAndProperty);
  240. };
  241.  
  242. htmlForm.getPropertyAccessorInfo = function() {
  243. return propertyAccessorInfo;
  244. };
  245.  
  246. htmlForm.getBeforeSubmit = function() {
  247. return beforeSubmit;
  248. };
  249.  
  250. htmlForm.getBeforeValidation = function() {
  251. return beforeValidation;
  252. };
  253.  
  254. htmlForm.setReturnUrl = function(url) {
  255. returnUrl = url;
  256. };
  257.  
  258. // TODO: these methods (getEncounter*Date*) will have to be modified when/if we switch datepickers
  259. // TODO: could/should be generalized so as not to be datepicker dependent?
  260.  
  261. htmlForm.setEncounterStartDateRange = function(date) {
  262. if (getField('encounterDate.value')) {
  263. getField('encounterDate.value').datepicker('option', 'minDate', date);
  264. }
  265. };
  266.  
  267. htmlForm.setEncounterStopDateRange = function(date) {
  268. if (getField('encounterDate.value')) {
  269. getField('encounterDate.value').datepicker('option', 'maxDate', date);
  270. }
  271. };
  272.  
  273. htmlForm.setEncounterDate = function(date) {
  274. if (getField('encounterDate.value')) {
  275. getField('encounterDate.value').datepicker('setDate', date);
  276. }
  277. };
  278.  
  279. htmlForm.disableEncounterDateManualEntry = function() {
  280. if (getField('encounterDate.value')) {
  281. getField('encounterDate.value').attr( 'readOnly' , 'true' );
  282. }
  283. };
  284.  
  285. htmlForm.showDiv = function(id) {
  286. var div = document.getElementById(id);
  287. if ( div ) { div.style.display = ""; }
  288. };
  289.  
  290. htmlForm.hideDiv = function(id) {
  291. var div = document.getElementById(id);
  292. if ( div ) { div.style.display = "none"; }
  293. }
  294.  
  295. }( window.htmlForm = window.htmlForm || {}, jQuery ));
Add Comment
Please, Sign In to add comment