1(function(window, document, $, Handlebars, undefined) { 2 3 Handlebars.registerHelper('truncate', function(str, len) { 4 if (str.length > len) { 5 var new_str = str.substr(0, len + 1); 6 7 while (new_str.length) { 8 var ch = new_str.substr(-1); 9 new_str = new_str.substr(0, -1); 10 11 if (ch == ' ') { 12 break; 13 } 14 } 15 16 if (new_str == '') { 17 new_str = str.substr(0, len); 18 } 19 20 return new Handlebars.SafeString(new_str + '...'); 21 } 22 return str; 23 }); 24 25 var EZTemplate = function(url, template, element, options) { 26 27 var data = null; 28 var _template = $(template)[0].innerHTML; 29 var $element = $(element); 30 var _options = $.extend({}, options); 31 32 function download() { 33 $.ajax({ 34 type: "GET", 35 url: url, 36 cache: false, 37 success: function(result) { 38 39 var isXML = this.dataTypes.indexOf("xml") > -1 ? true : false; 40 var isJSON = this.dataTypes.indexOf("json") > -1 ? true : false; 41 42 if (isXML) { 43 if (_options.sortByDate) { 44 data = beforeRender(sortByDate(formatData(ezUtilities.convertXMLtoJSON(result)))); 45 } else { 46 data = beforeRender(formatData(ezUtilities.convertXMLtoJSON(result))); 47 } 48 } else if (isJSON) { 49 if (_options.sortByDate) { 50 data = beforeRender(sortByDate(formatData(result))); 51 } else { 52 data = formatData(beforeRender(result)); 53 } 54 } else { 55 console.error("EZTemplate: The url did not return XML or JSON."); 56 return; 57 } 58 59 render(); 60 61 }, 62 error: function(error) { 63 console.log(error); 64 } 65 }); 66 } 67 68 function formatData(data) { 69 return data.map(function(item) { 70 item.time = item.startHour + ":" + item.startMinute + " " + item.startPeriod + " - " + item.endHour + ":" + item.endMinute + " " + item.endPeriod; 71 if (item.time == "12:00 am - 12:00 am") { 72 item.time = "All Day"; 73 item.allDay = true; 74 } 75 return item; 76 }); 77 }; 78 79 function render() { 80 81 var html = ""; 82 for (var i = 0; i < data.length; i++) { 83 var __template = Handlebars.compile(_template); 84 html += __template(data[i]); 85 } 86 87 $element.html(html); 88 89 afterRender($element); 90 91 } 92 93 function beforeRender(data) { 94 return data; 95 } 96 97 function afterRender($element) {} 98 99 function sortByDate(data) { 100 101 var sorted = data.reduce(function(accumulator, item) { 102 var group = item.monthLong + item.date + item.year; 103 if (!accumulator[group]) { 104 accumulator[group] = []; 105 } 106 accumulator[group].push(item); 107 return accumulator; 108 }, {}); 109 110 return Object.keys(sorted).map(function(date) { 111 112 var items = sorted[date]; 113 114 return { 115 id: date, 116 monthLong: items[0].monthLong, 117 month: items[0].month, 118 date: items[0].date, 119 year: items[0].year, 120 day: items[0].day, 121 dayLong: items[0].dayLong, 122 items: items 123 } 124 125 }); 126 127 }; 128 129 130 131 download(url); 132 133 return { 134 beforeRender: function(func) { 135 beforeRender = func; 136 }, 137 afterRender: function(func) { 138 afterRender = func; 139 } 140 } 141 142 }; 143 144 window.EZTemplate = EZTemplate; 145 146})(window, document, jQuery, Handlebars);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.