Initial commit
[clearscm.git] / web / JavaScript / common.js
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File:        common.js
4 // Description: Common Javascript functions
5 // Author:      Andrew@DeFaria.com
6 // Created:     Thu Oct  6 14:16:05 PDT 2011
7 // Language:    javascript
8 //
9 ////////////////////////////////////////////////////////////////////////////////
10 function basename (path) {
11   return path.replace (/\\/g, '/').replace (/.*\//, '');
12 } // basename
13  
14 function dirname (path) {
15   return path.replace (/\\/g, '/').replace (/\/[^\/]*$/, '');
16 } // dirname
17
18 function getText (item) {
19   // There's this annoying thing about getting the text of an HTML object - both
20   // Chrome and IE use innerText but Firefox uses textContent.
21   return item.innerText ? item.innerText : item.textContent;
22 } // getText
23
24 function getVar (variable) {
25   var query = window.location.search.substring (1);
26   
27   var vars = query.split ('&');
28   
29   for (var i=0; i < vars.length; i++) {
30     var pair = vars[i].split ('=');
31     
32     if (pair[0] == variable) {
33       return pair[1];
34     } // if
35   } // for
36   
37   return null;
38 } // getVar
39
40 function keys (obj) {
41   // keys: Emulate Perl's keys function. Note that hashes in Javascript are
42   // implemented as associative arrays, which are really objects. There is no
43   // keys function and as an Objects there are functions in there. So we use the
44   // hasOwnProperty function to insure that this is a pproperty and not a 
45   // method.
46   var keys = [];
47
48   for (key in obj) {
49     if (obj.hasOwnProperty (key)) keys.push (key);
50   } // for
51
52   return keys;
53 } // keys
54
55 function objLength (object) {
56   // The .length property doesn't exist for JavaScript objects and associative
57   // arrays. You need to count the properties instead.
58   var count = 0;
59   
60   for (property in object) {
61     if (object.hasOwnProperty (property)) {
62       count++;
63     } // if
64   } // for
65   
66   return count;
67 } // objLength