Highway to PSR2
[openemr.git] / portal / patient / scripts / app.js
blobc98672636b8e524d7f215eda5b7401281b535ab7
1 /**
2  * Application logic available globally throughout the app
3  *
4  * From phreeze package
5  * @license http://www.gnu.org/copyleft/lesser.html LGPL
6  *
7  */
8 var app = {
10         /** @var landmarks used to screen-scrape the html error output for a friendly message */
11         errorLandmarkStart: '<!-- ERROR ',
12         errorLandmarkEnd: ' /ERROR -->',
14         /**
15          * Display an alert message inside the element with the id containerId
16          *
17          * @param string message to display
18          * @param string style: '', 'alert-error', 'alert-success' or 'alert-info'
19          * @param int timeout for message to hide itself
20          * @param string containerId (default = 'alert')
21          */
22         appendAlert: function(message,style, timeout,containerId) {
24                 if (!style) style = '';
25                 if (!timeout) timeout = 0;
26                 if (!containerId) containerId = 'alert';
28                 var id = _.uniqueId('alert_');
30                 var html = '<div id="'+id+'" class="alert '+ this.escapeHtml(style) +'" style="display: none;">'
31                         + '<a class="close" data-dismiss="alert">&times;</a>'
32                         + '<span>'+ this.escapeHtml(message) +'</span>'
33                         + '</div>';
35                 // scroll the alert message into view
36                 var container = $('#' + containerId);
37                 container.append(html);
38                 container.parent().animate({
39                         scrollTop: container.offset().top - container.parent().offset().top + container.parent().scrollTop() - 10 // (10 is for top padding)
40                 });
42                 $('#'+id).slideDown('fast');
44                 if (timeout > 0) {
45                         setTimeout("app.removeAlert('"+id+"')",timeout);
46                 }
47         },
49         /**
50          * Remove an alert that has been previously shown
51          * @param string element id
52          */
53         removeAlert: function(id) {
55                 $("#"+id).slideUp('fast', function(){
56                         $("#"+id).remove();
57                 });
58         },
60         /**
61          * show the progress bar
62          * @param the id of the element containing the progress bar
63          */
64         showProgress: function(elementId)
65         {
66                 $('#'+elementId).show();
67                 // $('#'+elementId).animate({width:'150'},'fast');
68         },
70         /**
71          * hide the progress bar
72          * @param the id of the element containing the progress bar
73          */
74         hideProgress: function(elementId)
75         {
76                 setTimeout("$('#"+elementId+"').hide();",100);
77                 // $('#'+elementId).animate({width:'0'},'fast');
78         },
80         /**
81          * Escape unsafe HTML chars to prevent xss injection
82          * @param string potentially unsafe value
83          * @returns string safe value
84          */
85         escapeHtml: function(unsafe) {
86                 return _.escape(unsafe);
87         },
89         /**
90          * return true if user interface should be limited based on browser support
91          * @returns bool
92          */
93         browserSucks: function() {
94                 isIE6 = navigator.userAgent.match(/msie [6]/i) && !window.XMLHttpRequest;
95                 isIE7 = navigator.userAgent.match(/msie [7]/i);
96                 isIE8 = navigator.userAgent.match(/msie [8]/i);
97                 return isIE6 || isIE7 || isIE8;
98         },
100         /**
101          * Accept string in the following format: 'YYYY-MM-DD hh:mm:ss' or 'YYYY-MM-DD'
102          * If a date object is passed in, it will be returned as-is.  if a time-only
103          * value is provided, then it will be given the date of 1970-01-01
104          * @param string | date:
105          * @param defaultDate if the provided string can't be parsed, return this instead (default is Now)
106          * @returns Date
107          */
108         parseDate: function(str, defaultDate) {
110                 // don't re-parse a date obj
111                 if (str instanceof Date) return str;
113                 if (typeof(defaultDate) == 'undefined') defaultDate = ''; //new Date();
115                 // if the value passed in was blank, default to today
116                 if (str == '' || typeof(str) == 'undefined') {
117                         if (console) console.log('app.parseDate: empty or undefined date value');
118                         return defaultDate;
119                 }
120                 return str;
122         },
124         /**
125          * Convenience method for creating an option
126          */
127         getOptionHtml: function(val,label,selected)     {
128                 return '<option value="' + _.escape(val) + '" ' + (selected ? 'selected="selected"' : '') +'>'
129                         + _.escape(label)
130                         + '</option>';
131         },
133         /**
134          * A server error should contain json data, but if a fatal php error occurs it
135          * may contain html.  the function will parse the return contents of an
136          * error response and return the error message
137          * @param server response
138          */
139         getErrorMessage: function(resp) {
141                 var msg = 'An unknown error occured';
142                 try     {
143                         var json = $.parseJSON(resp.responseText);
144                         msg = json.message;
145                 } catch (error) {
146                         // TODO: possibly use regex or some other more robust way to get details...?
147                         var parts = resp.responseText.split(app.errorLandmarkStart);
149                         if (parts.length > 1) {
150                                 var parts2 = parts[1].split(app.errorLandmarkEnd);
151                                 msg = parts2[0];
152                         }
153                 }
155                 return msg ? msg : 'Unknown server error';
156         },
158         version: 1.1