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