2 * Application logic available globally throughout the app
5 * @license http://www.gnu.org/copyleft/lesser.html LGPL
10 /** @var landmarks used to screen-scrape the html error output for a friendly message */
11 errorLandmarkStart: '<!-- ERROR ',
12 errorLandmarkEnd: ' /ERROR -->',
15 * Display an alert message inside the element with the id containerId
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')
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">×</a>'
32 + '<span>'+ this.escapeHtml(message) +'</span>'
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)
42 $('#'+id).slideDown('fast');
45 setTimeout("app.removeAlert('"+id+"')",timeout);
50 * Remove an alert that has been previously shown
51 * @param string element id
53 removeAlert: function(id) {
55 $("#"+id).slideUp('fast', function(){
61 * show the progress bar
62 * @param the id of the element containing the progress bar
64 showProgress: function(elementId)
66 $('#'+elementId).show();
67 // $('#'+elementId).animate({width:'150'},'fast');
71 * hide the progress bar
72 * @param the id of the element containing the progress bar
74 hideProgress: function(elementId)
76 setTimeout("$('#"+elementId+"').hide();",100);
77 // $('#'+elementId).animate({width:'0'},'fast');
81 * Escape unsafe HTML chars to prevent xss injection
82 * @param string potentially unsafe value
83 * @returns string safe value
85 escapeHtml: function(unsafe) {
86 return _.escape(unsafe);
90 * return true if user interface should be limited based on browser support
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;
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)
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');
125 * Convenience method for creating an option
127 getOptionHtml: function(val,label,selected) {
128 return '<option value="' + _.escape(val) + '" ' + (selected ? 'selected="selected"' : '') +'>'
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
139 getErrorMessage: function(resp) {
141 var msg = 'An unknown error occured';
143 var json = $.parseJSON(resp.responseText);
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);
155 return msg ? msg : 'Unknown server error';