MTrackWeb/templates/images/js/mtrack.js
[web.mtrack] / MTrackWeb / templates / images / js / mtrack.js
1 //<Script type="text/javascript">
2
3 /**
4  * The way JQuery is used, we need to continually re-apply the css handlers whenever a new segment is loaded.
5  * 
6  * to solve this, we will use a simple mtrack.registry - all code get's added to it, 
7  * then whenever html is loaded via AJAX, we can run run through the handlers and add them.
8  * 
9  */
10  
11  
12 MTrack = {
13     
14     
15     init : function()
16     {
17         
18         $(document).ready(function () {
19             window.mtrack_footer_position();
20             if (authUser) {
21                 setTimeout(MTrack.keepLoggedIn, 30000);
22                 
23             }
24             
25             
26             
27         });
28             
29         $(document).ready(function () {
30             var path = [ 'Tree' ];
31             //if (repoName) path.push( repoName );
32             // ??/ jump?
33             
34             var href = (window.location.pathname + window.location.search).substring(baseURL.length);
35             try { 
36                 window.history.pushState( { url: href }, document.title , window.location.pathname + window.location.search );
37             } catch (e) {}
38             MTrack.currentURL = href;
39             jQuery.ajax( {
40             
41                 url : window.location.pathname + window.location.search,
42                 data : { ajax_body : 1 } ,
43                 success : function (data) {
44                     $('#ajaxbody').html(data);
45                     MTrack.addHandlers($('#ajaxbody'));
46                     MTrack.missingHashes($('#ajaxbody'), href)
47                 }
48             }); 
49         });
50         
51     },
52     
53     registry : [],
54     /**
55      * usage:
56      * MTrack.register('a.changeset-link', 'click', function() ....)
57      * 
58      */
59     register : function( selector, event, handler) 
60     {
61         MTrack.registry.push( {
62             selector : selector,
63             event : event,
64             handler : handler
65         });
66     },
67     
68     addHandlers : function (toWhat) 
69     {
70         // forEach? - no IE support?
71         toWhat = toWhat || document.body; // 
72         // run the main registry
73         MTrack.registry.forEach(function(cfg) {
74             
75             $(toWhat).find(cfg.selector)[cfg.event](cfg.handler);
76         });
77         // any other weird crap goes here.
78         
79         if ($.browser.mozilla) {
80             $(toWhat).find("form").attr("autocomplete", "off");
81         }
82         // time ago 
83         jQuery.timeago.settings.allowFuture = true;
84         $(toWhat).find('abbr.timeinterval').timeago();
85         
86         // multipe select addon
87         $(toWhat).find("select[multiple]").asmSelect({
88             addItemTarget: 'bottom',
89             animate: false,
90             highlight: false,
91             removeLabel: '[x]',
92             sortable: false
93         });
94       
95     },
96     
97     keepLoggedIn: function()
98     {
99         jQuery.ajax({
100             url : baseURL + '/Login.html',
101             type:  'POST',
102             data : { refresh : 1 },
103             success : function() {
104                 
105                 setTimeout(MTrack.keepLoggedIn, 30000);
106                 // what if it's false..currentURL = false;
107                 
108                 
109             }
110             
111         })
112         
113         
114         
115     }
116     
117 }
118
119 $(document).ready(function() {
120     MTrack.addHandlers(); // adds to main body..
121     // change project.
122     $('#banner select').change( function(e) {
123         // ajax change project, and refresh body..
124         jQuery.ajax({
125             url : baseURL + '/Project.html',
126             data : { active_project_id : this.value },
127             success : function() {
128                 // what if it's false..currentURL = false;
129                 MTrack.ajaxLoad(MTrack.currentURL, true);
130                 
131             }
132             
133         })
134          
135     });
136         
137         
138 });
139
140 // any date picers.. - on milestones?
141 MTrack.register('.dateinput', 'datepicker', {
142     // minDate: 0,
143     dateFormat: 'yy-mm-dd' // nice and compatible..
144 });
145
146
147 //// ------------------- OLD STUFF NEEDS TIDY UP ----------------------
148
149
150
151
152 $(document).ready(function() {
153     
154      
155     
156   
157   $("textarea.wiki").markItUp({
158     nameSpace:          "wiki",
159     previewParserPath:  baseURL + "/Preview",
160     root: rootURL + "/js",
161     onShiftEnter:       {keepDefault:false, replaceWith:'\\n\\n'},
162     markupSet:  [
163       {
164         name:'Heading 1', key:'1',
165         openWith:'== ', closeWith:' ==', placeHolder:'Your title here...'
166       },
167       {
168         name:'Heading 2', key:'2',
169         openWith:'=== ', closeWith:' ===', placeHolder:'Your title here...'
170       },
171       {
172         name:'Heading 3', key:'3',
173         openWith:'==== ', closeWith:' ====', placeHolder:'Your title here...'
174       },
175       {
176         name:'Heading 4', key:'4',
177         openWith:'===== ', closeWith:' =====', placeHolder:'Your title here...'
178       },
179       {
180         name:'Heading 5', key:'5',
181         openWith:'====== ', closeWith:' ======',
182         placeHolder:'Your title here...'
183       },
184       {separator:'---------------' },
185       {name:'Bold', key:'B', openWith:"'''", closeWith:"'''"},
186       {name:'Italic', key:'I', openWith:"''", closeWith:"''"},
187       {name:'Stroke through', key:'S', openWith:'~~', closeWith:'~~'},
188       {separator:'---------------' },
189       {name:'Bulleted list', openWith:' * '},
190       {name:'Numeric list', openWith:' 1. '},
191       {separator:'---------------' },
192       {name:'Quotes', openWith:'(!(> |!|>)!)'},
193       {name:'Code', openWith:'{{{\\n', closeWith:'\\n}}}'},
194       {separator:'---------------' },
195       {name:'Preview', call:'preview', className:'preview'}
196     ]
197   });
198
199   $.tablesorter.addParser({
200     id: 'ticket',
201     is: function(s) {
202       return /^#\d+/.test(s);
203     },
204     format: function(s) {
205       return $.tablesorter.formatFloat(s.replace(new RegExp(/#/g), ''));
206     },
207     type: 'numeric'
208   });
209  
210   $.tablesorter.addParser({
211     id: 'mtrackdate',
212     is: function(s) {
213       // don't auto-detect
214       return false;
215     },
216     format: function(s) {
217       // relies on the textExtraction routine below to pull a
218       // date/time string out of the title portion of the abbr tag
219       return $.tablesorter.formatFloat(new Date(s).getTime());
220     },
221     type: 'numeric'
222   });
223   
224   
225   
226   $("table.report, table.wiki").tablesorter({
227     textExtraction: function(node) {
228       var kid = node.childNodes[0];
229       if (kid && kid.tagName == 'ABBR') {
230         // assuming that this abbr is of class='timeinterval'
231         return kid.title;
232       }
233       // default 'simple' behavior
234       if (kid && kid.hasChildNodes()) {
235         return kid.innerHTML;
236       }
237       return node.innerHTML;
238     }
239   });
240   
241   
242   $('input.search[type=text]').each(function () {
243     if ($.browser.webkit) {
244       this.type = 'search';
245       ///$(this).attr('autosave', ABSWEB+'/');
246       $(this).attr('results', 5);
247     } else {
248       $(this).addClass('roundsearch');
249     }
250   });
251   // Convert links that are styled after buttons into actual buttons
252   $('a.button[href]').each(function () {
253     var href = $(this).attr('href');
254     var but = $('<button type="button"/>');
255     but.text($(this).text());
256     $(this).replaceWith(but);
257     but.click(function () {
258       document.location.href = href;
259       return false;
260     });
261   });
262
263   $.fn.mtrackWatermark = function () {
264     this.each(function () {
265       var ph = $(this).attr('title');
266       if ($.browser.webkit) {
267         // Use native safari placeholder for watermark
268         $(this).attr('placeholder', ph);
269       } else {
270         // http://plugins.jquery.com/files/jquery.tinywatermark-2.0.0.js.txt
271         var w;
272         var me = $(this);
273         me.focus(function () {
274           if (w) {
275             w = 0;
276             me.removeClass('watermark').data('w', 0).val('');
277           }
278         })
279         .blur(function () {
280           if (!me.val()) {
281             w = 1;
282             me.addClass('watermark').data('w', 1).val(ph);
283           }
284         })
285         .closest('form').submit(function () {
286           if (w) {
287             me.val('');
288           }
289         });
290         me.blur();
291       }
292     });
293   };
294   // Watermarking -??? what??
295   $('input[title!=""]').mtrackWatermark();
296  
297
298   // Arrange for the footer to sink to the bottom of the window, if the window
299   // contents are not very tall
300   var last_dh = 0;
301   var last_wh = 0;
302   function mtrack_footer_position(force) {
303     var ele = $('#footer');
304     if (!force &&
305         (last_dh != $(document).height() || last_wh != $(window).height)) {
306       force = true;
307     }
308     if (force) {
309       // Force a from-scratch layout assessment; put the footer back in
310       // it's natural location in the doc
311       ele.css({
312         position: "relative",
313         "margin-top": "3em",
314         top: 0
315       });
316     }
317     if ($(document).height() <= $(window).height()) {
318       ele.css({
319         position: "absolute",
320         "margin-top": "0",
321         top: (
322             $(window).scrollTop() +
323             $(window).height() -
324             ele.height() - 1
325           )+"px"
326       });
327     } else {
328       ele.css({
329         position: "relative",
330         "margin-top": "3em"
331       });
332     }
333     last_dh = $(document).height();
334     last_wh = $(window).height();
335   }
336   window.mtrack_footer_position = mtrack_footer_position;
337   $(window)
338     .scroll(mtrack_footer_position)
339     .resize(mtrack_footer_position);
340   function mtrack_footer_set_and_wait() {
341     mtrack_footer_position();
342     setTimeout(function () {
343       mtrack_footer_set_and_wait();
344     }, 1500);
345   }
346   mtrack_footer_set_and_wait();
347 });
348  
349
350 // from file.php
351
352
353 // from head -- probably for reports only..
354
355  
356 $(document).ready(function() {
357          
358       $.tablesorter.addParser({
359         id: 'priority',
360         is: function(s) {
361           // don't auto-detect
362           return false;
363         },
364         format: function(s) {
365             if (typeof(priorities[s]) != 'undefined') {
366                 return priorities[s];
367             }
368             return s;
369         },
370         type: 'numeric'
371       });
372       
373       $.tablesorter.addParser({
374         id: 'severity',
375         is: function(s) {
376           // don't auto-detect
377           return false;
378         },
379         format: function(s) {
380             if (typeof(severities[s]) != 'undefined') {
381                 return severities[s];
382             }
383             return s;
384         },
385         type: 'numeric'
386     });
387 });
388
389 // from wiki..
390
391 $(document).ready(function(){
392   $('ul.wikitree').treeview({
393     collapsed: true,
394     persist: "location"
395   });
396 });
397