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