MTrackWeb/templates/images/js/mtrack2.js
[web.mtrack] / MTrackWeb / templates / images / js / mtrack2.js
1 //<Script type="text/javascript">
2
3 /**
4  * Roo conversion....
5  * 
6  * currenlty mostly a clone of the JQuery version..
7  *
8  * uses a simple  registry - all code get's added to it, 
9  * then whenever html is loaded via AJAX, we can run run through the handlers and add them.
10  * 
11  */
12
13
14  
15 MTrack = {
16     init : function()
17     {
18         
19         Roo.onReady(function () {
20             window.mtrack_footer_position();
21             if (authUser) {
22                 setTimeout(MTrack.keepLoggedIn, 30000);
23                 
24             }
25             
26             
27             
28         });
29             
30         Roo.onReady( function () {
31             var path = [ 'Tree' ];
32             //if (repoName) path.push( repoName );
33             // ??/ jump?
34             
35             var href = (window.location.pathname + window.location.search).substring(baseURL.length);
36             try { 
37                 window.history.pushState( { url: href }, document.title , window.location.pathname + window.location.search );
38             } catch (e) {}
39             MTrack.currentURL = href;
40             Roo.Ajax.request( {
41                 method: 'GET',
42                 url : window.location.pathname + window.location.search,
43                 params: { ajax_body : 1 } ,
44                 success : function (data) {
45                     Roo.log(data);
46                     
47                     $('#ajaxbody').html(data);
48                     MTrack.addHandlers($('#ajaxbody'));
49                     MTrack.missingHashes($('#ajaxbody'), href)
50                 }
51             }); 
52         });
53         
54     },
55     registry : [],
56     /**
57      * usage:
58      * MTrack.register('a.changeset-link', 'each', function() ....)
59      * MTrack.register('a.changeset-link', 'on', 'click', function() ....)
60      * 
61      */
62     register : function( selector, method, arg1, arg2) 
63     {
64         MTrack.registry.push( {
65             selector : selector,
66             method : method,
67             event : method == 'on' ? arg1 : false,
68             args : method == 'on' ? arg2 : arg1 
69         });
70     },
71     
72     addHandlers : function (toWhat) 
73     {
74         // forEach? - no IE support?
75         toWhat = toWhat || document.body; // 
76         // run the main registry
77         MTrack.registry.forEach(function(cfg) {
78             var el = Roo.get(toWhat).select(cfg.selector,true)
79             el[cfg.method].apply(el, cfg.event ? [ event, args ] : [ args ] );  
80         });
81         // any other weird crap goes here.
82         
83         if (Roo.isGecko) {
84             Roo.select(toWhat,true).select("form").set( { "autocomplete" : "off" });
85         }
86         // time ago 
87         //jQuery.timeago.settings.allowFuture = true;
88         //$(toWhat).find('abbr.timeinterval').timeago();
89         
90         // multipe select addon
91         //$(toWhat).find("select[multiple]").asmSelect({
92         //    addItemTarget: 'bottom',
93         //    animate: false,
94         //    highlight: false,
95         //    removeLabel: '[x]',
96         //    sortable: false
97         //});
98       
99     },
100     
101     currentURL : false,
102     missingHashRequest : false,
103     
104     
105     // This is the main body load tool...
106     // it still needs to handle anchors...
107     
108     
109     ajaxLoad : function(url,slideleft)
110     { 
111         this.currentURL = url;
112         var target = Roo.get("ajaxbody");
113         var content = Roo.get("content");
114         target.setStyle('position', 'relative');
115         
116         Roo.select('.mask').show(); // must show first?
117         Roo.select('.mask-loading').show();
118         // content has a border...
119         var t = content.getBox().top - Roo.get(document).getScroll().top - 13;
120         var l = content.getBox().left - Roo.get(document).getScroll().top - 13;
121         var w = content.getBox().width+ 26;
122         var h = content.getBox().height + 26;
123      
124         Roo.select('.mask').first().setLocation(l,t);
125         Roo.select('.mask').first().setSize( w , h, false  );
126         
127         Roo.select('.mask-loading').first().setLocation(
128                 l + ( w / 2) - 16, t +   16 
129         );
130         
131         if (MTrack.missingHashRequest) {
132             MTrack.missingHashRequest.abort();
133             MTrack.missingHashRequest = false;
134         }
135         Roo.get('loader').hide();
136         Roo.Ajax.request({
137             url : baseURL + url,
138             params : { ajax_body : 1 },
139             success : function(data) {
140                 
141                 Roo.select('.mask').hide();
142                 Roo.get('loader').update(data);
143                 Roo.get('loader').show();
144                 Roo.get('loader').setWidth( w );
145                 Roo.get('loader').setPosition( slideleft?  l+w : l-w , t  );
146                 
147                 target.animate(
148                     {
149                         left: slideleft ? -1 * w  : w
150                     },
151                     .5,
152                     function() {
153                         target.setPosition(  0, 0 ); // reset it at end of animation - as it ends up with the new content.
154                     }
155                 );
156                 
157                 Roo.get('loader').animate(
158                     {
159                             left: l+13
160                     },
161                     .5,
162                     function () {
163                         target.update( Roo.get('loader').dom.innerHTML);
164                         Roo.get('loader').hide();
165                         Roo.get('loader').update("");
166                         target.show();// make sure!!
167                     
168                         MTrack.missingHashes(target,  url);
169                         MTrack.addHandlers(target);
170                     }
171                 );
172             }
173             //console.log('loaded dif');
174             
175                
176         });
177       
178         return false;  
179     },
180     
181     
182     missingHashRequest : false,
183     missingHashes: function(el,url) {
184         //console.log('finding missing hashes?');
185         var hashes = [];
186         el.select('.browse-missing-hash').each(function() {
187             hashes.push(this.dom.id);
188         });
189         if (!hashes.length) {
190             return;
191         }
192         if (MTrack.missingHashRequest) {
193             MTrack.missingHashRequest.abort();
194         }
195         MTrack.missingHashRequest = Roo.Ajax.request({
196             method : 'POST',
197             url : baseURL + url,
198             params : { hashes : hashes.join(',') },
199             success : function(data) {
200                 //console.log(data.data);
201                 if (data.data) {
202                     for(var hash in data.data) {
203                         var o = data.data[hash];
204                         Roo.select('#'+ hash).first().dom.innerHTML =  o.changeby + ' : ' + o.changelog;
205                         Roo.select('#age-'+ hash).first().dom.innerHTML = o.age ; // do magic replacement!!!
206                         Roo.select('#rev-'+ hash).first().dom.innerHTML = o.rev ; // do magic replacement!!!
207                         MTrack.addHandlers(Roo.select('#age-'+ hash).first());
208                         MTrack.missingHashRequest  = false;
209                     }
210                     
211                 }
212                // console.log(data)
213             }
214         });
215         
216     }
217         
218         
219 }
220
221 Roo.onReady(function() {
222     MTrack.addHandlers(); // adds to main body..
223     // change project.
224     
225     Roo.select('#banner select').on('change', function(e) {
226         // ajax change project, and refresh body..
227         Roo.Ajax.request({
228             url : baseURL + '/Project.html',
229             params : { active_project_id : this.value },
230             success : function() {
231                 // what if it's false..currentURL = false;
232                 MTrack.ajaxLoad(MTrack.currentURL, true);
233                 
234             }
235             
236         })
237          
238     });
239         
240         
241 });
242
243
244 try { 
245     window.onpopstate = function(ev) {
246          
247         if (!ev.state || typeof(ev.state.url) == 'undefined') {
248             return;
249         }
250         MTrack.ajaxLoad(ev.state.url, false);
251 };
252 } catch (e) {}
253     
254      
255      
256
257
258
259
260 // any date picers.. - on milestones?
261 MTrack.register('.dateinput', 'each', function(e) {
262     var ee = new Roo.form.DateField({
263         dateFormat: 'Y-m-d' // nice and compatible..
264     });
265     ee.applyTo(e);
266     // minDate: 0,
267     
268 } );
269
270
271
272 MTrack.register('a.browse-link', 'on', 'click', function(event) 
273 {
274     event.preventDefault();
275   
276     var href= this.getAttribute('href').substring(baseURL.length);
277     try { 
278         window.history.pushState( { url: href }, "Browse : " + href , this.href );
279     } catch (e) {}
280     var slideleft = $(this).is('.browse-link-up')  ? 0 : 1;    
281     MTrack.ajaxLoad(href,slideleft); 
282 });
283
284
285
286 //// ------------------- OLD STUFF NEEDS TIDY UP ----------------------
287
288 /*
289
290
291 $(document).ready(function() {
292     
293      
294     
295   
296   $("textarea.wiki").markItUp({
297     nameSpace:          "wiki",
298     previewParserPath:  baseURL + "/Preview",
299     root: rootURL + "/js",
300     onShiftEnter:       {keepDefault:false, replaceWith:'\\n\\n'},
301     markupSet:  [
302       {
303         name:'Heading 1', key:'1',
304         openWith:'== ', closeWith:' ==', placeHolder:'Your title here...'
305       },
306       {
307         name:'Heading 2', key:'2',
308         openWith:'=== ', closeWith:' ===', placeHolder:'Your title here...'
309       },
310       {
311         name:'Heading 3', key:'3',
312         openWith:'==== ', closeWith:' ====', placeHolder:'Your title here...'
313       },
314       {
315         name:'Heading 4', key:'4',
316         openWith:'===== ', closeWith:' =====', placeHolder:'Your title here...'
317       },
318       {
319         name:'Heading 5', key:'5',
320         openWith:'====== ', closeWith:' ======',
321         placeHolder:'Your title here...'
322       },
323       {separator:'---------------' },
324       {name:'Bold', key:'B', openWith:"'''", closeWith:"'''"},
325       {name:'Italic', key:'I', openWith:"''", closeWith:"''"},
326       {name:'Stroke through', key:'S', openWith:'~~', closeWith:'~~'},
327       {separator:'---------------' },
328       {name:'Bulleted list', openWith:' * '},
329       {name:'Numeric list', openWith:' 1. '},
330       {separator:'---------------' },
331       {name:'Quotes', openWith:'(!(> |!|>)!)'},
332       {name:'Code', openWith:'{{{\\n', closeWith:'\\n}}}'},
333       {separator:'---------------' },
334       {name:'Preview', call:'preview', className:'preview'}
335     ]
336   });
337
338   $.tablesorter.addParser({
339     id: 'ticket',
340     is: function(s) {
341       return /^#\d+/.test(s);
342     },
343     format: function(s) {
344       return $.tablesorter.formatFloat(s.replace(new RegExp(/#/g), ''));
345     },
346     type: 'numeric'
347   });
348  
349   $.tablesorter.addParser({
350     id: 'mtrackdate',
351     is: function(s) {
352       // don't auto-detect
353       return false;
354     },
355     format: function(s) {
356       // relies on the textExtraction routine below to pull a
357       // date/time string out of the title portion of the abbr tag
358       return $.tablesorter.formatFloat(new Date(s).getTime());
359     },
360     type: 'numeric'
361   });
362   
363   
364   
365   $("table.report, table.wiki").tablesorter({
366     textExtraction: function(node) {
367       var kid = node.childNodes[0];
368       if (kid && kid.tagName == 'ABBR') {
369         // assuming that this abbr is of class='timeinterval'
370         return kid.title;
371       }
372       // default 'simple' behavior
373       if (kid && kid.hasChildNodes()) {
374         return kid.innerHTML;
375       }
376       return node.innerHTML;
377     }
378   });
379   
380   
381   $('input.search[type=text]').each(function () {
382     if ($.browser.webkit) {
383       this.type = 'search';
384       ///$(this).attr('autosave', ABSWEB+'/');
385       $(this).attr('results', 5);
386     } else {
387       $(this).addClass('roundsearch');
388     }
389   });
390   // Convert links that are styled after buttons into actual buttons
391   $('a.button[href]').each(function () {
392     var href = $(this).attr('href');
393     var but = $('<button type="button"/>');
394     but.text($(this).text());
395     $(this).replaceWith(but);
396     but.click(function () {
397       document.location.href = href;
398       return false;
399     });
400   });
401
402   $.fn.mtrackWatermark = function () {
403     this.each(function () {
404       var ph = $(this).attr('title');
405       if ($.browser.webkit) {
406         // Use native safari placeholder for watermark
407         $(this).attr('placeholder', ph);
408       } else {
409         // http://plugins.jquery.com/files/jquery.tinywatermark-2.0.0.js.txt
410         var w;
411         var me = $(this);
412         me.focus(function () {
413           if (w) {
414             w = 0;
415             me.removeClass('watermark').data('w', 0).val('');
416           }
417         })
418         .blur(function () {
419           if (!me.val()) {
420             w = 1;
421             me.addClass('watermark').data('w', 1).val(ph);
422           }
423         })
424         .closest('form').submit(function () {
425           if (w) {
426             me.val('');
427           }
428         });
429         me.blur();
430       }
431     });
432   };
433   // Watermarking -??? what??
434   $('input[title!=""]').mtrackWatermark();
435  
436
437   // Arrange for the footer to sink to the bottom of the window, if the window
438   // contents are not very tall
439   var last_dh = 0;
440   var last_wh = 0;
441   function mtrack_footer_position(force) {
442     var ele = $('#footer');
443     if (!force &&
444         (last_dh != $(document).height() || last_wh != $(window).height)) {
445       force = true;
446     }
447     if (force) {
448       // Force a from-scratch layout assessment; put the footer back in
449       // it's natural location in the doc
450       ele.css({
451         position: "relative",
452         "margin-top": "3em",
453         top: 0
454       });
455     }
456     if ($(document).height() <= $(window).height()) {
457       ele.css({
458         position: "absolute",
459         "margin-top": "0",
460         top: (
461             $(window).scrollTop() +
462             $(window).height() -
463             ele.height() - 1
464           )+"px"
465       });
466     } else {
467       ele.css({
468         position: "relative",
469         "margin-top": "3em"
470       });
471     }
472     last_dh = $(document).height();
473     last_wh = $(window).height();
474   }
475   window.mtrack_footer_position = mtrack_footer_position;
476   $(window)
477     .scroll(mtrack_footer_position)
478     .resize(mtrack_footer_position);
479   function mtrack_footer_set_and_wait() {
480     mtrack_footer_position();
481     setTimeout(function () {
482       mtrack_footer_set_and_wait();
483     }, 1500);
484   }
485   mtrack_footer_set_and_wait();
486 });
487  
488
489 // from file.php
490
491
492 // from head -- probably for reports only..
493
494  
495 $(document).ready(function() {
496          
497       $.tablesorter.addParser({
498         id: 'priority',
499         is: function(s) {
500           // don't auto-detect
501           return false;
502         },
503         format: function(s) {
504             if (typeof(priorities[s]) != 'undefined') {
505                 return priorities[s];
506             }
507             return s;
508         },
509         type: 'numeric'
510       });
511       
512       $.tablesorter.addParser({
513         id: 'severity',
514         is: function(s) {
515           // don't auto-detect
516           return false;
517         },
518         format: function(s) {
519             if (typeof(severities[s]) != 'undefined') {
520                 return severities[s];
521             }
522             return s;
523         },
524         type: 'numeric'
525     });
526 });
527
528 // from wiki..
529
530 $(document).ready(function(){
531   $('ul.wikitree').treeview({
532     collapsed: true,
533     persist: "location"
534   });
535 });
536
537  */