sync
[web.mtrack] / MTrackWeb / templates / images / js / mtrack.js
1 //<Script type="text/javascript">
2
3 /**
4  * The way JQuery is used, we need to continually re-apply the css handlers whenever a new segment is loaded.
5  * 
6  * to solve this, we will use a simple mtrack.registry - all code get's added to it, 
7  * then whenever html is loaded via AJAX, we can run run through the handlers and add them.
8  * 
9  */
10  
11  
12 MTrack = {
13     
14     
15     init : function()
16     {
17         
18         $(document).ready(function () {
19             window.mtrack_footer_position();
20             if (authUser) {
21                 setTimeout(MTrack.keepLoggedIn, 30000);
22                 
23             }
24             
25             
26             
27         });
28             
29         $(document).ready(function () {
30             var path = [ 'Tree' ];
31             //if (repoName) path.push( repoName );
32             // ??/ jump?
33             
34             var href = (window.location.pathname + window.location.search).substring(baseURL.length);
35             try { 
36                 window.history.pushState( { url: href }, document.title , window.location.pathname + window.location.search );
37             } catch (e) {}
38             MTrack.currentURL = href;
39             jQuery.ajax( {
40             
41                 url : window.location.pathname + window.location.search,
42                 data : { ajax_body : 1 } ,
43                 success : function (data) {
44                     $('#ajaxbody').html(data);
45                     document.body.scrollTop = 0;
46                     MTrack.addHandlers($('#ajaxbody'));
47                     MTrack.missingHashes($('#ajaxbody'), href)
48                 }
49             }); 
50         });
51         
52     },
53     
54     registry : [],
55     /**
56      * usage:
57      * MTrack.register('a.changeset-link', 'click', function() ....)
58      * 
59      */
60     register : function( selector, event, handler) 
61     {
62         MTrack.registry.push( {
63             selector : selector,
64             event : event,
65             handler : handler
66         });
67     },
68     
69     addHandlers : function (toWhat) 
70     {
71         // forEach? - no IE support?
72         toWhat = toWhat || document.body; // 
73         // run the main registry
74         MTrack.registry.forEach(function(cfg) {
75             
76             $(toWhat).find(cfg.selector)[cfg.event](cfg.handler);
77         });
78         // any other weird crap goes here.
79         
80         if ($.browser.mozilla) {
81             $(toWhat).find("form").attr("autocomplete", "off");
82         }
83         // time ago 
84         jQuery.timeago.settings.allowFuture = true;
85         $(toWhat).find('abbr.timeinterval').timeago();
86         
87         // multipe select addon
88         $(toWhat).find("select[multiple]").asmSelect({
89             addItemTarget: 'bottom',
90             animate: false,
91             highlight: false,
92             removeLabel: '[x]',
93             sortable: false
94         });
95       
96     },
97     
98     keepLoggedIn: function()
99     {
100         jQuery.ajax({
101             url : baseURL + '/Login.html',
102             type:  'POST',
103             data : { refresh : 1 },
104             success : function() {
105                 
106                 setTimeout(MTrack.keepLoggedIn, 30000);
107                 // what if it's false..currentURL = false;
108                 
109                 
110             }
111             
112         })
113         
114         
115         
116     }
117     
118 }
119
120 $(document).ready(function() {
121     MTrack.addHandlers(); // adds to main body..
122     // change project.
123     $('#banner select').change( function(e) {
124         // ajax change project, and refresh body..
125         jQuery.ajax({
126             url : baseURL + '/core_project.html',
127             data : { active_project_id : this.value },
128             success : function() {
129                 // what if it's false..currentURL = false;
130                 MTrack.ajaxLoad(MTrack.currentURL, true);
131                 
132             }
133             
134         })
135          
136     });
137         
138         
139 });
140
141 // any date picers.. - on milestones?
142 MTrack.register('.dateinput', 'datepicker', {
143     // minDate: 0,
144     dateFormat: 'yy-mm-dd' // nice and compatible..
145 });
146
147
148 //// ------------------- OLD STUFF NEEDS TIDY UP ----------------------
149
150
151
152
153 $(document).ready(function() {
154     
155      
156     
157   
158   $("textarea.wiki").markItUp({
159     nameSpace:          "wiki",
160     previewParserPath:  baseURL + "/Preview",
161     root: rootURL + "/js",
162     onShiftEnter:       {keepDefault:false, replaceWith:'\\n\\n'},
163     markupSet:  [
164       {
165         name:'Heading 1', key:'1',
166         openWith:'== ', closeWith:' ==', placeHolder:'Your title here...'
167       },
168       {
169         name:'Heading 2', key:'2',
170         openWith:'=== ', closeWith:' ===', placeHolder:'Your title here...'
171       },
172       {
173         name:'Heading 3', key:'3',
174         openWith:'==== ', closeWith:' ====', placeHolder:'Your title here...'
175       },
176       {
177         name:'Heading 4', key:'4',
178         openWith:'===== ', closeWith:' =====', placeHolder:'Your title here...'
179       },
180       {
181         name:'Heading 5', key:'5',
182         openWith:'====== ', closeWith:' ======',
183         placeHolder:'Your title here...'
184       },
185       {separator:'---------------' },
186       {name:'Bold', key:'B', openWith:"'''", closeWith:"'''"},
187       {name:'Italic', key:'I', openWith:"''", closeWith:"''"},
188       {name:'Stroke through', key:'S', openWith:'~~', closeWith:'~~'},
189       {separator:'---------------' },
190       {name:'Bulleted list', openWith:' * '},
191       {name:'Numeric list', openWith:' 1. '},
192       {separator:'---------------' },
193       {name:'Quotes', openWith:'(!(> |!|>)!)'},
194       {name:'Code', openWith:'{{{\\n', closeWith:'\\n}}}'},
195       {separator:'---------------' },
196       {name:'Preview', call:'preview', className:'preview'}
197     ]
198   });
199
200   $.tablesorter.addParser({
201     id: 'ticket',
202     is: function(s) {
203       return /^#\d+/.test(s);
204     },
205     format: function(s) {
206       return $.tablesorter.formatFloat(s.replace(new RegExp(/#/g), ''));
207     },
208     type: 'numeric'
209   });
210  
211   $.tablesorter.addParser({
212     id: 'mtrackdate',
213     is: function(s) {
214       // don't auto-detect
215       return false;
216     },
217     format: function(s) {
218       // relies on the textExtraction routine below to pull a
219       // date/time string out of the title portion of the abbr tag
220       return $.tablesorter.formatFloat(new Date(s).getTime());
221     },
222     type: 'numeric'
223   });
224   
225   
226   
227   $("table.report, table.wiki").tablesorter({
228     textExtraction: function(node) {
229       var kid = node.childNodes[0];
230       if (kid && kid.tagName == 'ABBR') {
231         // assuming that this abbr is of class='timeinterval'
232         return kid.title;
233       }
234       // default 'simple' behavior
235       if (kid && kid.hasChildNodes()) {
236         return kid.innerHTML;
237       }
238       return node.innerHTML;
239     }
240   });
241   
242   
243   $('input.search[type=text]').each(function () {
244     if ($.browser.webkit) {
245       this.type = 'search';
246       ///$(this).attr('autosave', ABSWEB+'/');
247       $(this).attr('results', 5);
248     } else {
249       $(this).addClass('roundsearch');
250     }
251   });
252   // Convert links that are styled after buttons into actual buttons
253   $('a.button[href]').each(function () {
254     var href = $(this).attr('href');
255     var but = $('<button type="button"/>');
256     but.text($(this).text());
257     $(this).replaceWith(but);
258     but.click(function () {
259       document.location.href = href;
260       return false;
261     });
262   });
263
264   $.fn.mtrackWatermark = function () {
265     this.each(function () {
266       var ph = $(this).attr('title');
267       if ($.browser.webkit) {
268         // Use native safari placeholder for watermark
269         $(this).attr('placeholder', ph);
270       } else {
271         // http://plugins.jquery.com/files/jquery.tinywatermark-2.0.0.js.txt
272         var w;
273         var me = $(this);
274         me.focus(function () {
275           if (w) {
276             w = 0;
277             me.removeClass('watermark').data('w', 0).val('');
278           }
279         })
280         .blur(function () {
281           if (!me.val()) {
282             w = 1;
283             me.addClass('watermark').data('w', 1).val(ph);
284           }
285         })
286         .closest('form').submit(function () {
287           if (w) {
288             me.val('');
289           }
290         });
291         me.blur();
292       }
293     });
294   };
295   // Watermarking -??? what??
296   $('input[title!=""]').mtrackWatermark();
297  
298
299   // Arrange for the footer to sink to the bottom of the window, if the window
300   // contents are not very tall
301   var last_dh = 0;
302   var last_wh = 0;
303   function mtrack_footer_position(force) {
304     return;
305     var ele = $('#footer');
306     if (!force &&
307         (last_dh != $(document).height() || last_wh != $(window).height)) {
308       force = true;
309     }
310     if (force) {
311       // Force a from-scratch layout assessment; put the footer back in
312       // it's natural location in the doc
313       ele.css({
314         position: "relative",
315         "margin-top": "3em",
316         top: 0
317       });
318     }
319     if ($(document).height() <= $(window).height()) {
320       ele.css({
321         position: "absolute",
322         "margin-top": "0",
323         top: (
324             $(window).scrollTop() +
325             $(window).height() -
326             ele.height() - 1
327           )+"px"
328       });
329     } else {
330       ele.css({
331         position: "relative",
332         "margin-top": "3em"
333       });
334     }
335     last_dh = $(document).height();
336     last_wh = $(window).height();
337   }
338   window.mtrack_footer_position = mtrack_footer_position;
339   $(window)
340     .scroll(mtrack_footer_position)
341     .resize(mtrack_footer_position);
342   function mtrack_footer_set_and_wait() {
343     return;
344     mtrack_footer_position();
345     setTimeout(function () {
346       mtrack_footer_set_and_wait();
347     }, 1500);
348   }
349   mtrack_footer_set_and_wait();
350 });
351  
352
353 // from file.php
354
355
356 // from head -- probably for reports only..
357
358  
359 $(document).ready(function() {
360          
361       $.tablesorter.addParser({
362         id: 'priority',
363         is: function(s) {
364           // don't auto-detect
365           return false;
366         },
367         format: function(s) {
368             if (typeof(priorities[s]) != 'undefined') {
369                 return priorities[s];
370             }
371             return s;
372         },
373         type: 'numeric'
374       });
375       
376       $.tablesorter.addParser({
377         id: 'severity',
378         is: function(s) {
379           // don't auto-detect
380           return false;
381         },
382         format: function(s) {
383             if (typeof(severities[s]) != 'undefined') {
384                 return severities[s];
385             }
386             return s;
387         },
388         type: 'numeric'
389     });
390 });
391
392 // from wiki..
393
394 $(document).ready(function(){
395   $('ul.wikitree').treeview({
396     collapsed: true,
397     persist: "location"
398   });
399 });
400