source: timingandestimationplugin/branches/trac0.11/timingandestimationplugin/htdocs/linkifyer.js

Last change on this file was 7234, checked in by Russ Tyndall, 14 years ago

Fixed bug in linkifyer where null values were treated as invalid dates. Sorry for the trouble :/ fixes #6306

File size: 3.7 KB
Line 
1
2//var linkify =
3//(function(){
4   String.prototype.trim =
5      (function () {return this.replace(/^\s+/, "").replace(/\s+$/, "");});
6   var invalidDate = new Date("invalid").toString();
7   var billingfields= {};
8   var statusfields = [];
9   function dateToUnixEpoch(date){
10      return Math.round(date.getTime()/1000) - (60 * date.getTimezoneOffset());
11   }
12   function makeDate(val) {
13      var d = null;
14      if (val && val.length && val.length>0){
15     try{
16        d = Date.parse(val);
17     }
18     catch(e){
19        d = invalidDate;
20     }
21     if(!d || d.toString == invalidDate){
22        alert("You entered an invalid date: "+val);
23        return null;
24     }
25      }
26      return d;
27   }
28   function addBillingField( name /*optional type defaults to "textbox", optional flag status*/ ){
29      var type = arguments.length >= 1 ? arguments[1] : "textbox";
30      var status = arguments.length >= 2 ? arguments[2] : false;
31      var getSet =
32     (function(){
33        var valueProp = "value";
34
35        if(type == "date"){
36           return function (/*optional value*/){
37          if(arguments.length == 0){
38             var d = makeDate(this.$()[valueProp]);
39             if (d) return dateToUnixEpoch(d);
40             else return null;
41          }
42          else{
43             var val = makeDate(arguments[0]);
44             if(!val){
45            this.$()[valueProp] = null;
46            return null;
47             }
48             this.$()[valueProp] = val;
49             return val;
50          }
51           };
52        }
53        //FOR EVERYTHING ELSE
54        if(type == "checkbox"){
55           valueProp = "checked";
56        }
57        return function (/*optional value*/){
58           //alert(name+" : "+type+" "+valueProp);
59           if(arguments.length == 0){
60          var val = (this.$())[valueProp];
61
62          if(typeof(val) == "string") val = val.trim();
63          if(val)return val;
64          return null;
65           }
66           else{
67          var val = arguments[0];
68          (this.$())[valueProp] = val;
69          return val;
70           }
71        };
72      })();
73      billingfields[name] = {
74     "$" : function(){
75        return document.getElementById(name);
76     },
77         getval : getSet,
78     setval : getSet
79      };
80      if (status){
81     statusfields.push({
82          name:name,
83          "$" : function(){
84          return document.getElementById(name);
85          },
86          getval : getSet,
87          setval : getSet
88     });
89      }
90   }
91
92   addBillingField("billable", "checkbox");
93   addBillingField("unbillable", "checkbox");
94   addBillingField("startdate", "date");
95   addBillingField("startbilling", "dateselect");
96   addBillingField("enddate", "date");
97   addBillingField("endbilling", "dateselect");
98
99
100   var linkify = function ( atag, basehref ){
101      var query = "";
102      var haveAdded = false;
103      function addToQuery(str){
104     query += haveAdded ? "&" : "?";
105     query += str;
106     haveAdded = true;
107      }
108      //billable logic
109      addToQuery(billingfields["billable"].getval() || !(billingfields["unbillable"].getval())
110         ? "BILLABLE=1" : "BILLABLE=0");
111      addToQuery(billingfields["unbillable"].getval() || !(billingfields["billable"].getval())
112         ? "UNBILLABLE=0" : "UNBILLABLE=1");
113
114      for(var i=0, f = null ; f = statusfields[i] ; i++){
115     val = f.name.toUpperCase().replace("_","", "g").replace(" ","","g")+"=";
116     if(f.getval()){
117        val += f.name
118     }
119     addToQuery(val);
120      }
121
122      //startdate the date in the text box or the date in the dropdown or the first time
123      startdate = billingfields["startdate"].getval() || billingfields["startbilling"].getval() || 0;
124      addToQuery("STARTDATE="+startdate);
125      //the date in the enddate text box or the date in the enddate billing box or real close to the end of integer unix epoch time
126      // this will need a patch to continue working  past this point
127      enddate = billingfields["enddate"].getval() || billingfields["endbilling"].getval() || 2000000000;
128      addToQuery("ENDDATE="+enddate);
129
130      atag.href = basehref+query;
131   }
132//})()
Note: See TracBrowser for help on using the repository browser.