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