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             MTrack.footerSetAndWait();
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                     var ab = Roo.get('ajaxbody');
47                     ab.dom.innerHTML = data.responseText;
48                     MTrack.addHandlers(ab);
49                     MTrack.missingHashes(ab, 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 ? [ cfg.event, cfg.args ] : [ cfg.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     last_dh : false,
218     last_wh : false,
219    
220     footerPosition : function (force) {
221         var ele = Roo.get('footer');
222         if (!force &&
223             (MTrack.last_dh != Roo.lib.Dom.getDocumentHeight() || MTrack.last_wh != Roo.lib.Dom.getViewHeight())) {
224           force = true;
225         }
226         
227         if (force) {
228           // Force a from-scratch layout assessment; put the footer back in
229           // it's natural location in the doc
230           ele.setStyle({
231             position: "relative",
232             "margin-top": "3em",
233             top: 0
234           });
235         }
236         if (Roo.lib.Dom.getDocumentHeight() <= Roo.lib.Dom.getViewHeight()) {
237           ele.setStyle({
238             position: "absolute",
239             "margin-top": "0",
240             top: (
241                 Roo.get(document).getScroll().top +
242                 Roo.lib.Dom.getViewHeight() -
243                 ele.getSize().height - 1
244               )+"px"
245           });
246         } else {
247           ele.setStyle({
248             position: "relative",
249             "margin-top": "3em"
250           });
251         }
252         MTrack.last_dh =  Roo.lib.Dom.getDocumentHeight();
253         MTrack.last_wh = Roo.lib.Dom.getViewHeight();
254     },
255     
256       /*
257       
258        $(window)
259         .scroll(mtrack_footer_position)
260         .resize(mtrack_footer_position);
261         
262       */
263         
264     footerSetAndWait : function () {
265         this.footerPosition();
266         setTimeout(function () {
267           MTrack.footerSetAndWait();
268         }, 1500);
269     }
270     
271     
272     
273         
274 }
275
276 Roo.onReady(function() {
277     MTrack.addHandlers(); // adds to main body..
278     // change project.
279     
280     Roo.select('#banner select').on('change', function(e) {
281         // ajax change project, and refresh body..
282         Roo.Ajax.request({
283             url : baseURL + '/Project.html',
284             params : { active_project_id : this.value },
285             success : function() {
286                 // what if it's false..currentURL = false;
287                 MTrack.ajaxLoad(MTrack.currentURL, true);
288                 
289             }
290             
291         })
292          
293     });
294         
295         
296 });
297
298
299 try { 
300     window.onpopstate = function(ev) {
301          
302         if (!ev.state || typeof(ev.state.url) == 'undefined') {
303             return;
304         }
305         MTrack.ajaxLoad(ev.state.url, false);
306 };
307 } catch (e) {}
308     
309      
310      
311
312
313
314
315 // any date picers.. - on milestones?
316 MTrack.register('.dateinput', 'each', function(e) {
317     var ee = new Roo.form.DateField({
318         dateFormat: 'Y-m-d' // nice and compatible..
319     });
320     ee.applyTo(e);
321     // minDate: 0,
322     
323 } );
324
325
326
327 MTrack.register('a.browse-link', 'on', 'click', function(event) 
328 {
329     event.preventDefault();
330   
331     var href= this.dom.getAttribute('href').substring(baseURL.length);
332     try { 
333         window.history.pushState( { url: href }, "Browse : " + href , this.href );
334     } catch (e) {}
335     var slideleft = $(this).is('.browse-link-up')  ? 0 : 1;    
336     MTrack.ajaxLoad(href,slideleft); 
337 });
338
339
340
341 //// ------------------- OLD STUFF NEEDS TIDY UP ----------------------
342
343 /*
344
345
346 $(document).ready(function() {
347     
348      
349     
350   
351   $("textarea.wiki").markItUp({
352     nameSpace:          "wiki",
353     previewParserPath:  baseURL + "/Preview",
354     root: rootURL + "/js",
355     onShiftEnter:       {keepDefault:false, replaceWith:'\\n\\n'},
356     markupSet:  [
357       {
358         name:'Heading 1', key:'1',
359         openWith:'== ', closeWith:' ==', placeHolder:'Your title here...'
360       },
361       {
362         name:'Heading 2', key:'2',
363         openWith:'=== ', closeWith:' ===', placeHolder:'Your title here...'
364       },
365       {
366         name:'Heading 3', key:'3',
367         openWith:'==== ', closeWith:' ====', placeHolder:'Your title here...'
368       },
369       {
370         name:'Heading 4', key:'4',
371         openWith:'===== ', closeWith:' =====', placeHolder:'Your title here...'
372       },
373       {
374         name:'Heading 5', key:'5',
375         openWith:'====== ', closeWith:' ======',
376         placeHolder:'Your title here...'
377       },
378       {separator:'---------------' },
379       {name:'Bold', key:'B', openWith:"'''", closeWith:"'''"},
380       {name:'Italic', key:'I', openWith:"''", closeWith:"''"},
381       {name:'Stroke through', key:'S', openWith:'~~', closeWith:'~~'},
382       {separator:'---------------' },
383       {name:'Bulleted list', openWith:' * '},
384       {name:'Numeric list', openWith:' 1. '},
385       {separator:'---------------' },
386       {name:'Quotes', openWith:'(!(> |!|>)!)'},
387       {name:'Code', openWith:'{{{\\n', closeWith:'\\n}}}'},
388       {separator:'---------------' },
389       {name:'Preview', call:'preview', className:'preview'}
390     ]
391   });
392
393   $.tablesorter.addParser({
394     id: 'ticket',
395     is: function(s) {
396       return /^#\d+/.test(s);
397     },
398     format: function(s) {
399       return $.tablesorter.formatFloat(s.replace(new RegExp(/#/g), ''));
400     },
401     type: 'numeric'
402   });
403  
404   $.tablesorter.addParser({
405     id: 'mtrackdate',
406     is: function(s) {
407       // don't auto-detect
408       return false;
409     },
410     format: function(s) {
411       // relies on the textExtraction routine below to pull a
412       // date/time string out of the title portion of the abbr tag
413       return $.tablesorter.formatFloat(new Date(s).getTime());
414     },
415     type: 'numeric'
416   });
417   
418   
419   
420   $("table.report, table.wiki").tablesorter({
421     textExtraction: function(node) {
422       var kid = node.childNodes[0];
423       if (kid && kid.tagName == 'ABBR') {
424         // assuming that this abbr is of class='timeinterval'
425         return kid.title;
426       }
427       // default 'simple' behavior
428       if (kid && kid.hasChildNodes()) {
429         return kid.innerHTML;
430       }
431       return node.innerHTML;
432     }
433   });
434   
435   
436   $('input.search[type=text]').each(function () {
437     if ($.browser.webkit) {
438       this.type = 'search';
439       ///$(this).attr('autosave', ABSWEB+'/');
440       $(this).attr('results', 5);
441     } else {
442       $(this).addClass('roundsearch');
443     }
444   });
445   // Convert links that are styled after buttons into actual buttons
446   $('a.button[href]').each(function () {
447     var href = $(this).attr('href');
448     var but = $('<button type="button"/>');
449     but.text($(this).text());
450     $(this).replaceWith(but);
451     but.click(function () {
452       document.location.href = href;
453       return false;
454     });
455   });
456
457   $.fn.mtrackWatermark = function () {
458     this.each(function () {
459       var ph = $(this).attr('title');
460       if ($.browser.webkit) {
461         // Use native safari placeholder for watermark
462         $(this).attr('placeholder', ph);
463       } else {
464         // http://plugins.jquery.com/files/jquery.tinywatermark-2.0.0.js.txt
465         var w;
466         var me = $(this);
467         me.focus(function () {
468           if (w) {
469             w = 0;
470             me.removeClass('watermark').data('w', 0).val('');
471           }
472         })
473         .blur(function () {
474           if (!me.val()) {
475             w = 1;
476             me.addClass('watermark').data('w', 1).val(ph);
477           }
478         })
479         .closest('form').submit(function () {
480           if (w) {
481             me.val('');
482           }
483         });
484         me.blur();
485       }
486     });
487   };
488   // Watermarking -??? what??
489   $('input[title!=""]').mtrackWatermark();
490  
491
492   // Arrange for the footer to sink to the bottom of the window, if the window
493   // contents are not very tall
494   var last_dh = 0;
495   var last_wh = 0;
496   function mtrack_footer_position(force) {
497     var ele = $('#footer');
498     if (!force &&
499         (last_dh != $(document).height() || last_wh != $(window).height)) {
500       force = true;
501     }
502     if (force) {
503       // Force a from-scratch layout assessment; put the footer back in
504       // it's natural location in the doc
505       ele.css({
506         position: "relative",
507         "margin-top": "3em",
508         top: 0
509       });
510     }
511     if ($(document).height() <= $(window).height()) {
512       ele.css({
513         position: "absolute",
514         "margin-top": "0",
515         top: (
516             $(window).scrollTop() +
517             $(window).height() -
518             ele.height() - 1
519           )+"px"
520       });
521     } else {
522       ele.css({
523         position: "relative",
524         "margin-top": "3em"
525       });
526     }
527     last_dh = $(document).height();
528     last_wh = $(window).height();
529   }
530   window.mtrack_footer_position = mtrack_footer_position;
531   $(window)
532     .scroll(mtrack_footer_position)
533     .resize(mtrack_footer_position);
534   function mtrack_footer_set_and_wait() {
535     mtrack_footer_position();
536     setTimeout(function () {
537       mtrack_footer_set_and_wait();
538     }, 1500);
539   }
540   mtrack_footer_set_and_wait();
541 });
542  
543
544 // from file.php
545
546
547 // from head -- probably for reports only..
548
549  
550 $(document).ready(function() {
551          
552       $.tablesorter.addParser({
553         id: 'priority',
554         is: function(s) {
555           // don't auto-detect
556           return false;
557         },
558         format: function(s) {
559             if (typeof(priorities[s]) != 'undefined') {
560                 return priorities[s];
561             }
562             return s;
563         },
564         type: 'numeric'
565       });
566       
567       $.tablesorter.addParser({
568         id: 'severity',
569         is: function(s) {
570           // don't auto-detect
571           return false;
572         },
573         format: function(s) {
574             if (typeof(severities[s]) != 'undefined') {
575                 return severities[s];
576             }
577             return s;
578         },
579         type: 'numeric'
580     });
581 });
582
583 // from wiki..
584
585 $(document).ready(function(){
586   $('ul.wikitree').treeview({
587     collapsed: true,
588     persist: "location"
589   });
590 });
591
592  */