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