Add scripts to create myops-getqueryview:
[myops.git] / web / query / vendor / couchapp / _attachments / jquery.couch.app.util.js
1 $.log = function(m) {
2   if (window && window.console && window.console.log) {
3     window.console.log(arguments.length == 1 ? m : arguments);
4   }
5 };
6
7 // http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery/1186309#1186309
8 $.fn.serializeObject = function() {
9     var o = {};
10     var a = this.serializeArray();
11     $.each(a, function() {
12         if (o[this.name]) {
13             if (!o[this.name].push) {
14                 o[this.name] = [o[this.name]];
15             }
16             o[this.name].push(this.value || '');
17         } else {
18             o[this.name] = this.value || '';
19         }
20     });
21     return o;
22 };
23
24 // todo remove this crap
25 function escapeHTML(st) {                                       
26   return(                                                                 
27     st && st.replace(/&/g,'&').                                         
28       replace(/>/g,'>').                                           
29       replace(/</g,'&lt;').                                           
30       replace(/"/g,'&quot;')                                         
31   );                                                                     
32 };
33
34 function safeHTML(st, len) {
35   return st ? escapeHTML(st.substring(0,len)) : '';
36 }
37
38 // todo this should take a replacement template
39 $.linkify = function(body) {
40   return body.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,function(a) {
41     return '<a target="_blank" href="'+a+'">'+a+'</a>';
42   }).replace(/\@([\w\-]+)/g,function(user,name) {
43     return '<a href="#/mentions/'+encodeURIComponent(name.toLowerCase())+'">'+user+'</a>';
44   }).replace(/\#([\w\-\.]+)/g,function(word,tag) {
45     return '<a href="#/tags/'+encodeURIComponent(tag.toLowerCase())+'">'+word+'</a>';
46   });
47 };
48
49 $.fn.prettyDate = function() {
50   $(this).each(function() {
51     var string, title = $(this).attr("title");
52     if (title) {
53       string = $.prettyDate(title);
54     } else {
55       string = $.prettyDate($(this).text());
56     }
57     $(this).text(string);
58   });
59 };
60
61 $.prettyDate = function(time){
62   
63         var date = new Date(time.replace(/-/g,"/").replace("T", " ").replace("Z", " +0000").replace(/(\d*\:\d*:\d*)\.\d*/g,"$1")),
64                 diff = (((new Date()).getTime() - date.getTime()) / 1000),
65                 day_diff = Math.floor(diff / 86400);
66
67   if (isNaN(day_diff)) return time;
68
69         return day_diff < 1 && (
70                         diff < 60 && "just now" ||
71                         diff < 120 && "1 minute ago" ||
72                         diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
73                         diff < 7200 && "1 hour ago" ||
74                         diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
75                 day_diff == 1 && "yesterday" ||
76                 day_diff < 21 && day_diff + " days ago" ||
77                 day_diff < 45 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
78     time;
79     // day_diff < 730 && Math.ceil( day_diff / 31 ) + " months ago" ||
80     // Math.ceil( day_diff / 365 ) + " years ago";
81 };
82
83 $.argsToArray = function(args) {
84   if (!args.callee) return args;
85   var array = [];
86   for (var i=0; i < args.length; i++) {
87     array.push(args[i]);
88   };
89   return array;
90 }