##// END OF EJS Templates
rhodecode.js: pyroute, don't iterate over req_params, make for loop over its length
Mads Kiilerich -
r4169:c560a578 rhodecode-2.2.5-gpl
parent child Browse files
Show More
@@ -1,2150 +1,2150
1 1 /**
2 2 RhodeCode JS Files
3 3 **/
4 4
5 5 if (typeof console == "undefined" || typeof console.log == "undefined"){
6 6 console = { log: function() {} }
7 7 }
8 8
9 9 /**
10 10 * INJECT .format function into String
11 11 * Usage: "My name is {0} {1}".format("Johny","Bravo")
12 12 * Return "My name is Johny Bravo"
13 13 * Inspired by https://gist.github.com/1049426
14 14 */
15 15 String.prototype.format = function() {
16 16 function format() {
17 17 var str = this;
18 18 var len = arguments.length+1;
19 19 var safe = undefined;
20 20 var arg = undefined;
21 21
22 22 // For each {0} {1} {n...} replace with the argument in that position. If
23 23 // the argument is an object or an array it will be stringified to JSON.
24 24 for (var i=0; i < len; arg = arguments[i++]) {
25 25 safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
26 26 str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
27 27 }
28 28 return str;
29 29 }
30 30
31 31 // Save a reference of what may already exist under the property native.
32 32 // Allows for doing something like: if("".format.native) { /* use native */ }
33 33 format.native = String.prototype.format;
34 34
35 35 // Replace the prototype property
36 36 return format;
37 37
38 38 }();
39 39
40 40 String.prototype.strip = function(char) {
41 41 if(char === undefined){
42 42 char = '\\s';
43 43 }
44 44 return this.replace(new RegExp('^'+char+'+|'+char+'+$','g'), '');
45 45 }
46 46
47 47 String.prototype.lstrip = function(char) {
48 48 if(char === undefined){
49 49 char = '\\s';
50 50 }
51 51 return this.replace(new RegExp('^'+char+'+'),'');
52 52 }
53 53
54 54 String.prototype.rstrip = function(char) {
55 55 if(char === undefined){
56 56 char = '\\s';
57 57 }
58 58 return this.replace(new RegExp(''+char+'+$'),'');
59 59 }
60 60
61 61 /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill
62 62 under MIT license / public domain, see
63 63 https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses */
64 64 if(!Array.prototype.indexOf) {
65 65 Array.prototype.indexOf = function (searchElement, fromIndex) {
66 66 if ( this === undefined || this === null ) {
67 67 throw new TypeError( '"this" is null or not defined' );
68 68 }
69 69
70 70 var length = this.length >>> 0; // Hack to convert object.length to a UInt32
71 71
72 72 fromIndex = +fromIndex || 0;
73 73
74 74 if (Math.abs(fromIndex) === Infinity) {
75 75 fromIndex = 0;
76 76 }
77 77
78 78 if (fromIndex < 0) {
79 79 fromIndex += length;
80 80 if (fromIndex < 0) {
81 81 fromIndex = 0;
82 82 }
83 83 }
84 84
85 85 for (;fromIndex < length; fromIndex++) {
86 86 if (this[fromIndex] === searchElement) {
87 87 return fromIndex;
88 88 }
89 89 }
90 90
91 91 return -1;
92 92 };
93 93 }
94 94
95 95 /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Compatibility
96 96 under MIT license / public domain, see
97 97 https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses */
98 98 if (!Array.prototype.filter)
99 99 {
100 100 Array.prototype.filter = function(fun /*, thisArg */)
101 101 {
102 102 "use strict";
103 103
104 104 if (this === void 0 || this === null)
105 105 throw new TypeError();
106 106
107 107 var t = Object(this);
108 108 var len = t.length >>> 0;
109 109 if (typeof fun !== "function")
110 110 throw new TypeError();
111 111
112 112 var res = [];
113 113 var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
114 114 for (var i = 0; i < len; i++)
115 115 {
116 116 if (i in t)
117 117 {
118 118 var val = t[i];
119 119
120 120 // NOTE: Technically this should Object.defineProperty at
121 121 // the next index, as push can be affected by
122 122 // properties on Object.prototype and Array.prototype.
123 123 // But that method's new, and collisions should be
124 124 // rare, so use the more-compatible alternative.
125 125 if (fun.call(thisArg, val, i, t))
126 126 res.push(val);
127 127 }
128 128 }
129 129
130 130 return res;
131 131 };
132 132 }
133 133
134 134 /**
135 135 * A customized version of PyRoutes.JS from https://pypi.python.org/pypi/pyroutes.js/
136 136 * which is copyright Stephane Klein and was made available under the BSD License.
137 137 *
138 138 * Usage pyroutes.url('mark_error_fixed',{"error_id":error_id}) // /mark_error_fixed/<error_id>
139 139 */
140 140 var pyroutes = (function() {
141 141 // access global map defined in special file pyroutes
142 142 var matchlist = PROUTES_MAP;
143 143 var sprintf = (function() {
144 144 function get_type(variable) {
145 145 return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
146 146 }
147 147 function str_repeat(input, multiplier) {
148 148 for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */}
149 149 return output.join('');
150 150 }
151 151
152 152 var str_format = function() {
153 153 if (!str_format.cache.hasOwnProperty(arguments[0])) {
154 154 str_format.cache[arguments[0]] = str_format.parse(arguments[0]);
155 155 }
156 156 return str_format.format.call(null, str_format.cache[arguments[0]], arguments);
157 157 };
158 158
159 159 str_format.format = function(parse_tree, argv) {
160 160 var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;
161 161 for (i = 0; i < tree_length; i++) {
162 162 node_type = get_type(parse_tree[i]);
163 163 if (node_type === 'string') {
164 164 output.push(parse_tree[i]);
165 165 }
166 166 else if (node_type === 'array') {
167 167 match = parse_tree[i]; // convenience purposes only
168 168 if (match[2]) { // keyword argument
169 169 arg = argv[cursor];
170 170 for (k = 0; k < match[2].length; k++) {
171 171 if (!arg.hasOwnProperty(match[2][k])) {
172 172 throw(sprintf('[sprintf] property "%s" does not exist', match[2][k]));
173 173 }
174 174 arg = arg[match[2][k]];
175 175 }
176 176 }
177 177 else if (match[1]) { // positional argument (explicit)
178 178 arg = argv[match[1]];
179 179 }
180 180 else { // positional argument (implicit)
181 181 arg = argv[cursor++];
182 182 }
183 183
184 184 if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) {
185 185 throw(sprintf('[sprintf] expecting number but found %s', get_type(arg)));
186 186 }
187 187 switch (match[8]) {
188 188 case 'b': arg = arg.toString(2); break;
189 189 case 'c': arg = String.fromCharCode(arg); break;
190 190 case 'd': arg = parseInt(arg, 10); break;
191 191 case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;
192 192 case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;
193 193 case 'o': arg = arg.toString(8); break;
194 194 case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;
195 195 case 'u': arg = Math.abs(arg); break;
196 196 case 'x': arg = arg.toString(16); break;
197 197 case 'X': arg = arg.toString(16).toUpperCase(); break;
198 198 }
199 199 arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg);
200 200 pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' ';
201 201 pad_length = match[6] - String(arg).length;
202 202 pad = match[6] ? str_repeat(pad_character, pad_length) : '';
203 203 output.push(match[5] ? arg + pad : pad + arg);
204 204 }
205 205 }
206 206 return output.join('');
207 207 };
208 208
209 209 str_format.cache = {};
210 210
211 211 str_format.parse = function(fmt) {
212 212 var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;
213 213 while (_fmt) {
214 214 if ((match = /^[^\x25]+/.exec(_fmt)) !== null) {
215 215 parse_tree.push(match[0]);
216 216 }
217 217 else if ((match = /^\x25{2}/.exec(_fmt)) !== null) {
218 218 parse_tree.push('%');
219 219 }
220 220 else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {
221 221 if (match[2]) {
222 222 arg_names |= 1;
223 223 var field_list = [], replacement_field = match[2], field_match = [];
224 224 if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
225 225 field_list.push(field_match[1]);
226 226 while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
227 227 if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
228 228 field_list.push(field_match[1]);
229 229 }
230 230 else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) {
231 231 field_list.push(field_match[1]);
232 232 }
233 233 else {
234 234 throw('[sprintf] huh?');
235 235 }
236 236 }
237 237 }
238 238 else {
239 239 throw('[sprintf] huh?');
240 240 }
241 241 match[2] = field_list;
242 242 }
243 243 else {
244 244 arg_names |= 2;
245 245 }
246 246 if (arg_names === 3) {
247 247 throw('[sprintf] mixing positional and named placeholders is not (yet) supported');
248 248 }
249 249 parse_tree.push(match);
250 250 }
251 251 else {
252 252 throw('[sprintf] huh?');
253 253 }
254 254 _fmt = _fmt.substring(match[0].length);
255 255 }
256 256 return parse_tree;
257 257 };
258 258
259 259 return str_format;
260 260 })();
261 261
262 262 var vsprintf = function(fmt, argv) {
263 263 argv.unshift(fmt);
264 264 return sprintf.apply(null, argv);
265 265 };
266 266 return {
267 267 'url': function(route_name, params) {
268 268 var result = route_name;
269 269 if (typeof(params) != 'object'){
270 270 params = {};
271 271 }
272 272 if (matchlist.hasOwnProperty(route_name)) {
273 273 var route = matchlist[route_name];
274 274 // param substitution
275 275 for(var i=0; i < route[1].length; i++) {
276 276 if (!params.hasOwnProperty(route[1][i]))
277 277 throw new Error(route[1][i] + ' missing in "' + route_name + '" route generation');
278 278 }
279 279 result = sprintf(route[0], params);
280 280
281 281 var ret = [];
282 282 //extra params => GET
283 283 for(param in params){
284 284 if (route[1].indexOf(param) == -1){
285 285 ret.push(encodeURIComponent(param) + "=" + encodeURIComponent(params[param]));
286 286 }
287 287 }
288 288 var _parts = ret.join("&");
289 289 if(_parts){
290 290 result = result +'?'+ _parts
291 291 }
292 292 }
293 293
294 294 return result;
295 295 },
296 296 'register': function(route_name, route_tmpl, req_params) {
297 297 if (typeof(req_params) != 'object') {
298 298 req_params = [];
299 299 }
300 300 //fix escape
301 301 route_tmpl = unescape(route_tmpl);
302 302 keys = [];
303 for (o in req_params){
304 keys.push(req_params[o])
303 for (var i=0; i < req_params.length; i++) {
304 keys.push(req_params[i])
305 305 }
306 306 matchlist[route_name] = [
307 307 route_tmpl,
308 308 keys
309 309 ]
310 310 },
311 311 '_routes': function(){
312 312 return matchlist;
313 313 }
314 314 }
315 315 })();
316 316
317 317
318 318 /**
319 319 * GLOBAL YUI Shortcuts
320 320 */
321 321 var YUC = YAHOO.util.Connect;
322 322 var YUD = YAHOO.util.Dom;
323 323 var YUE = YAHOO.util.Event;
324 324 var YUQ = YAHOO.util.Selector.query;
325 325
326 326 /* Invoke all functions in callbacks */
327 327 var _run_callbacks = function(callbacks){
328 328 if (callbacks !== undefined){
329 329 var _l = callbacks.length;
330 330 for (var i=0;i<_l;i++){
331 331 var func = callbacks[i];
332 332 if(typeof(func)=='function'){
333 333 try{
334 334 func();
335 335 }catch (err){};
336 336 }
337 337 }
338 338 }
339 339 }
340 340
341 341 /**
342 342 * turns objects into GET query string
343 343 */
344 344 var _toQueryString = function(o) {
345 345 if(typeof o !== 'object') {
346 346 return false;
347 347 }
348 348 var _p, _qs = [];
349 349 for(_p in o) {
350 350 _qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p]));
351 351 }
352 352 return _qs.join('&');
353 353 };
354 354
355 355 /**
356 356 * Partial Ajax Implementation
357 357 *
358 358 * @param url: defines url to make partial request
359 359 * @param container: defines id of container to input partial result
360 360 * @param s_call: success callback function that takes o as arg
361 361 * o.tId
362 362 * o.status
363 363 * o.statusText
364 364 * o.getResponseHeader[ ]
365 365 * o.getAllResponseHeaders
366 366 * o.responseText
367 367 * o.responseXML
368 368 * o.argument
369 369 * @param f_call: failure callback
370 370 * @param args arguments
371 371 */
372 372 function ypjax(url,container,s_call,f_call,args){
373 373 var method='GET';
374 374 if(args===undefined){
375 375 args=null;
376 376 }
377 377 $container = $('#' + container);
378 378
379 379 // Set special header for partial ajax == HTTP_X_PARTIAL_XHR
380 380 YUC.initHeader('X-PARTIAL-XHR',true);
381 381
382 382 // wrapper of passed callback
383 383 var s_wrapper = (function(o){
384 384 return function(o){
385 385 $container.html(o.responseText);
386 386 $container.css('opacity','1.0');
387 387 //execute the given original callback
388 388 if (s_call !== undefined){
389 389 s_call(o);
390 390 }
391 391 }
392 392 })()
393 393 $container.css('opacity','0.3');
394 394 YUC.asyncRequest(method,url,{
395 395 success:s_wrapper,
396 396 failure:function(o){
397 397 console.log('ypjax failure: '+o);
398 398 $container.html('<span class="error_red">ERROR: {0}</span>'.format(o.status));
399 399 $container.css('opacity','1.0');
400 400 },
401 401 cache:false
402 402 },args);
403 403
404 404 };
405 405
406 406 var ajaxGET = function(url,success) {
407 407 // Set special header for ajax == HTTP_X_PARTIAL_XHR
408 408 YUC.initHeader('X-PARTIAL-XHR',true);
409 409
410 410 var sUrl = url;
411 411 var callback = {
412 412 success: success,
413 413 failure: function (o) {
414 414 if (o.status != 0) {
415 415 alert("error: " + o.statusText);
416 416 };
417 417 },
418 418 };
419 419
420 420 var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
421 421 return request;
422 422 };
423 423
424 424 var ajaxPOST = function(url,postData,success) {
425 425 // Set special header for ajax == HTTP_X_PARTIAL_XHR
426 426 YUC.initHeader('X-PARTIAL-XHR',true);
427 427
428 428 var sUrl = url;
429 429 var callback = {
430 430 success: success,
431 431 failure: function (o) {
432 432 alert("error");
433 433 },
434 434 };
435 435 var postData = _toQueryString(postData);
436 436 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
437 437 return request;
438 438 };
439 439
440 440
441 441 /**
442 442 * activate .show_more links
443 443 * the .show_more must have an id that is the the id of an element to hide prefixed with _
444 444 * the parentnode will be displayed
445 445 */
446 446 var show_more_event = function(){
447 447 $('.show_more').click(function(e){
448 448 var el = e.currentTarget;
449 449 $('#' + el.id.substring(1)).hide();
450 450 $(el.parentNode).show();
451 451 });
452 452 };
453 453
454 454 /**
455 455 * activate .lazy-cs mouseover for showing changeset tooltip
456 456 */
457 457 var show_changeset_tooltip = function(){
458 458 $('.lazy-cs').mouseover(function(e){
459 459 var $target = $(e.currentTarget);
460 460 var rid = $target.attr('raw_id');
461 461 var repo_name = $target.attr('repo_name');
462 462 if(rid && !$target.hasClass('tooltip')){
463 463 _show_tooltip(e, _TM['loading ...']);
464 464 var url = pyroutes.url('changeset_info', {"repo_name": repo_name, "revision": rid});
465 465 ajaxGET(url, function(o){
466 466 var json = JSON.parse(o.responseText);
467 467 $target.addClass('tooltip')
468 468 _show_tooltip(e, json['message']);
469 469 _activate_tooltip($target);
470 470 });
471 471 }
472 472 });
473 473 };
474 474
475 475 var _onSuccessFollow = function(target){
476 476 var $target = $(target);
477 477 var $f_cnt = $('#current_followers_count');
478 478 if($target.hasClass('follow')){
479 479 $target.attr('class', 'following');
480 480 $target.attr('title', _TM['Stop following this repository']);
481 481 if($f_cnt.html()){
482 482 var cnt = Number($f_cnt.html())+1;
483 483 $f_cnt.html(cnt);
484 484 }
485 485 }
486 486 else{
487 487 $target.attr('class', 'follow');
488 488 $target.attr('title', _TM['Start following this repository']);
489 489 if($f_cnt.html()){
490 490 var cnt = Number($f_cnt.html())-1;
491 491 $f_cnt.html(cnt);
492 492 }
493 493 }
494 494 }
495 495
496 496 var toggleFollowingRepo = function(target, follows_repo_id, token, user_id){
497 497 args = 'follows_repo_id='+follows_repo_id;
498 498 args+= '&amp;auth_token='+token;
499 499 if(user_id != undefined){
500 500 args+="&amp;user_id="+user_id;
501 501 }
502 502 $.post(TOGGLE_FOLLOW_URL, args, function(data){
503 503 _onSuccessFollow(target);
504 504 });
505 505 return false;
506 506 };
507 507
508 508 var showRepoSize = function(target, repo_name, token){
509 509 var args= 'auth_token='+token;
510 510
511 511 if(!$("#" + target).hasClass('loaded')){
512 512 $("#" + target).html(_TM['Loading ...']);
513 513 var url = pyroutes.url('repo_size', {"repo_name":repo_name});
514 514 $.post(url, args, function(data) {
515 515 $("#" + target).html(data);
516 516 $("#" + target).addClass('loaded');
517 517 });
518 518 }
519 519 return false;
520 520 };
521 521
522 522 /**
523 523 * tooltips
524 524 */
525 525
526 526 var tooltip_activate = function(){
527 527 $(document).ready(_init_tooltip);
528 528 };
529 529
530 530 var _activate_tooltip = function($tt){
531 531 $tt.mouseover(_show_tooltip);
532 532 $tt.mousemove(_move_tooltip);
533 533 $tt.mouseout(_close_tooltip);
534 534 };
535 535
536 536 var _init_tooltip = function(){
537 537 var $tipBox = $('#tip-box');
538 538 if(!$tipBox.length){
539 539 $tipBox = $('<div id="tip-box"></div>')
540 540 $(document.body).append($tipBox);
541 541 }
542 542
543 543 $tipBox.hide();
544 544 $tipBox.css('position', 'absolute');
545 545 $tipBox.css('max-width', '600px');
546 546
547 547 _activate_tooltip($('.tooltip'));
548 548 };
549 549
550 550 var _show_tooltip = function(e, tipText){
551 551 e.stopImmediatePropagation();
552 552 var el = e.currentTarget;
553 553 if(tipText){
554 554 // just use it
555 555 } else if(el.tagName.toLowerCase() === 'img'){
556 556 tipText = el.alt ? el.alt : '';
557 557 } else {
558 558 tipText = el.title ? el.title : '';
559 559 }
560 560
561 561 if(tipText !== ''){
562 562 // save org title
563 563 $(el).attr('tt_title', tipText);
564 564 // reset title to not show org tooltips
565 565 $(el).attr('title', '');
566 566
567 567 var $tipBox = $('#tip-box');
568 568 $tipBox.html(tipText);
569 569 $tipBox.css('display', 'block');
570 570 }
571 571 };
572 572
573 573 var _move_tooltip = function(e){
574 574 e.stopImmediatePropagation();
575 575 var $tipBox = $('#tip-box');
576 576 $tipBox.css('top', (e.pageY + 15) + 'px');
577 577 $tipBox.css('left', (e.pageX + 15) + 'px');
578 578 };
579 579
580 580 var _close_tooltip = function(e){
581 581 e.stopImmediatePropagation();
582 582 var $tipBox = $('#tip-box');
583 583 $tipBox.hide();
584 584 var el = e.currentTarget;
585 585 $(el).attr('title', $(el).attr('tt_title'));
586 586 };
587 587
588 588 /**
589 589 * Quick filter widget
590 590 *
591 591 * @param target: filter input target
592 592 * @param nodes: list of nodes in html we want to filter.
593 593 * @param display_element function that takes current node from nodes and
594 594 * does hide or show based on the node
595 595 */
596 596 var q_filter = function(target, nodes, display_element){
597 597 var nodes = nodes;
598 598 var $q_filter_field = $('#' + target);
599 599 var F = YAHOO.namespace(target);
600 600
601 601 $q_filter_field.keyup(function(e){
602 602 clearTimeout(F.filterTimeout);
603 603 F.filterTimeout = setTimeout(F.updateFilter, 600);
604 604 });
605 605
606 606 F.filterTimeout = null;
607 607
608 608 F.updateFilter = function() {
609 609 // Reset timeout
610 610 F.filterTimeout = null;
611 611
612 612 var obsolete = [];
613 613
614 614 var req = $q_filter_field.val().toLowerCase();
615 615
616 616 var l = nodes.length;
617 617 var i;
618 618 var showing = 0;
619 619
620 620 for (i=0; i<l; i++ ){
621 621 var n = nodes[i];
622 622 var target_element = display_element(n)
623 623 if(req && n.innerHTML.toLowerCase().indexOf(req) == -1){
624 624 $(target_element).hide();
625 625 }
626 626 else{
627 627 $(target_element).show();
628 628 showing += 1;
629 629 }
630 630 }
631 631
632 632 $('#repo_count').html(showing); /* FIXME: don't hardcode */
633 633 }
634 634 };
635 635
636 636 /* return jQuery expression with a tr with body in 3rd column and class cls and id named after the body */
637 637 var _table_tr = function(cls, body){
638 638 // like: <div class="comment" id="comment-8" line="o92"><div class="comment-wrapp">...
639 639 // except new inlines which are different ...
640 640 var comment_id = ($(body).attr('id') || 'comment-new').split('comment-')[1];
641 641 var tr_id = 'comment-tr-{0}'.format(comment_id);
642 642 return $(('<tr id="{0}" class="{1}">'+
643 643 '<td class="lineno-inline new-inline"></td>'+
644 644 '<td class="lineno-inline old-inline"></td>'+
645 645 '<td>{2}</td>'+
646 646 '</tr>').format(tr_id, cls, body));
647 647 };
648 648
649 649 /** return jQuery expression with new inline form based on template **/
650 650 var _createInlineForm = function(parent_tr, f_path, line) {
651 651 var $tmpl = $('#comment-inline-form-template').html().format(f_path, line);
652 652 var $form = _table_tr('comment-form-inline', $tmpl)
653 653
654 654 // create event for hide button
655 655 $form.find('.hide-inline-form').click(function(e) {
656 656 var newtr = e.currentTarget.parentNode.parentNode.parentNode.parentNode.parentNode;
657 657 if($(newtr).next().hasClass('inline-comments-button')){
658 658 $(newtr).next().show();
659 659 }
660 660 $(newtr).remove();
661 661 $(parent_tr).removeClass('form-open');
662 662 $(parent_tr).removeClass('hl-comment');
663 663 });
664 664
665 665 return $form
666 666 };
667 667
668 668 /**
669 669 * Inject inline comment for an given TR. This tr should always be a .line .
670 670 * The form will be inject after any comments.
671 671 */
672 672 var injectInlineForm = function(tr){
673 673 $tr = $(tr);
674 674 if(!$tr.hasClass('line')){
675 675 return
676 676 }
677 677 var submit_url = AJAX_COMMENT_URL;
678 678 var $td = $tr.find('.code');
679 679 if($tr.hasClass('form-open') || $tr.hasClass('context') || $td.hasClass('no-comment')){
680 680 return
681 681 }
682 682 $tr.addClass('form-open hl-comment');
683 683 var $node = $tr.parent().parent().parent().find('.full_f_path');
684 684 var f_path = $node.attr('path');
685 685 var lineno = _getLineNo(tr);
686 686 var $form = _createInlineForm(tr, f_path, lineno, submit_url);
687 687
688 688 var $parent = $tr;
689 689 while ($parent.next().hasClass('inline-comments')){
690 690 var $parent = $parent.next();
691 691 }
692 692 $form.insertAfter($parent);
693 693 var $overlay = $form.find('.overlay');
694 694 var $inlineform = $form.find('.inline-form');
695 695
696 696 $form.submit(function(e){
697 697 e.preventDefault();
698 698
699 699 if(lineno === undefined){
700 700 alert('missing line !');
701 701 return
702 702 }
703 703 if(f_path === undefined){
704 704 alert('missing file path !');
705 705 return
706 706 }
707 707
708 708 var text = $('#text_'+lineno).val();
709 709 if(text == ""){
710 710 return
711 711 }
712 712
713 713 if ($overlay.hasClass('overlay')){
714 714 $overlay.css('width', $inlineform.offsetWidth + 'px');
715 715 $overlay.css('height', $inlineform.offsetHeight + 'px');
716 716 }
717 717 $overlay.addClass('submitting');
718 718
719 719 var success = function(o){
720 720 $tr.removeClass('form-open');
721 721 $form.remove();
722 722 var json_data = JSON.parse(o.responseText);
723 723 _renderInlineComment(json_data);
724 724 };
725 725 var postData = {
726 726 'text': text,
727 727 'f_path': f_path,
728 728 'line': lineno
729 729 };
730 730 ajaxPOST(submit_url, postData, success);
731 731 });
732 732
733 733 $('#preview-btn_'+lineno).click(function(e){
734 734 var text = $('#text_'+lineno).val();
735 735 if(!text){
736 736 return
737 737 }
738 738 $('#preview-box_'+lineno).addClass('unloaded');
739 739 $('#preview-box_'+lineno).html(_TM['Loading ...']);
740 740 $('#edit-container_'+lineno).hide();
741 741 $('#preview-container_'+lineno).show();
742 742
743 743 var url = pyroutes.url('changeset_comment_preview', {'repo_name': REPO_NAME});
744 744 var post_data = {'text': text};
745 745 ajaxPOST(url, post_data, function(o){
746 746 $('#preview-box_'+lineno).html(o.responseText);
747 747 $('#preview-box_'+lineno).removeClass('unloaded');
748 748 })
749 749 })
750 750 $('#edit-btn_'+lineno).click(function(e){
751 751 $('#edit-container_'+lineno).show();
752 752 $('#preview-container_'+lineno).hide();
753 753 })
754 754
755 755 setTimeout(function(){
756 756 // callbacks
757 757 tooltip_activate();
758 758 MentionsAutoComplete('text_'+lineno, 'mentions_container_'+lineno,
759 759 _USERS_AC_DATA, _GROUPS_AC_DATA);
760 760 $('#text_'+lineno).focus();
761 761 },10)
762 762 };
763 763
764 764 var deleteComment = function(comment_id){
765 765 var url = AJAX_COMMENT_DELETE_URL.replace('__COMMENT_ID__',comment_id);
766 766 var postData = {'_method':'delete'};
767 767 var success = function(o){
768 768 var $deleted = $('#comment-tr-'+comment_id);
769 769 var $prev = $deleted.prev('tr');
770 770 $deleted.remove();
771 771 _placeAddButton($prev);
772 772 }
773 773 ajaxPOST(url,postData,success);
774 774 }
775 775
776 776 var _getLineNo = function(tr) {
777 777 var line;
778 778 var o = $(tr).children()[0].id.split('_');
779 779 var n = $(tr).children()[1].id.split('_');
780 780
781 781 if (n.length >= 2) {
782 782 line = n[n.length-1];
783 783 } else if (o.length >= 2) {
784 784 line = o[o.length-1];
785 785 }
786 786
787 787 return line
788 788 };
789 789
790 790 var _placeAddButton = function($line_tr){
791 791 var $tr = $line_tr;
792 792 while ($tr.next().hasClass('inline-comments')){
793 793 $tr.find('.add-comment').remove();
794 794 $tr = $tr.next();
795 795 }
796 796 $tr.find('.add-comment').remove();
797 797 var label = TRANSLATION_MAP['Add another comment'];
798 798 var $html_el = $('<div class="add-comment"><span class="ui-btn">{0}</span></div>'.format(label));
799 799 $html_el.click(function(e) {
800 800 injectInlineForm($line_tr);
801 801 });
802 802 $tr.find('.comment').after($html_el);
803 803 };
804 804
805 805 /**
806 806 * Places the inline comment into the changeset block in proper line position
807 807 */
808 808 var _placeInline = function(target_id, lineno, html){
809 809 var $td = $("#{0}_{1}".format(target_id, lineno));
810 810
811 811 // check if there are comments already !
812 812 var $line_tr = $td.parent(); // the tr
813 813 var $after_tr = $line_tr;
814 814 while ($after_tr.next().hasClass('inline-comments')){
815 815 $after_tr = $after_tr.next();
816 816 }
817 817 // put in the comment at the bottom
818 818 $after_tr.after(_table_tr('inline-comments', html));
819 819
820 820 // scan nodes, and attach add button to last one
821 821 _placeAddButton($line_tr);
822 822 }
823 823
824 824 /**
825 825 * make a single inline comment and place it inside
826 826 */
827 827 var _renderInlineComment = function(json_data){
828 828 var html = json_data['rendered_text'];
829 829 var lineno = json_data['line_no'];
830 830 var target_id = json_data['target_id'];
831 831 _placeInline(target_id, lineno, html);
832 832 }
833 833
834 834 /**
835 835 * Iterates over all the inlines, and places them inside proper blocks of data
836 836 */
837 837 var renderInlineComments = function(file_comments){
838 838 for (f in file_comments){
839 839 // holding all comments for a FILE
840 840 var box = file_comments[f];
841 841
842 842 var target_id = $(box).attr('target_id');
843 843 // actual comments with line numbers
844 844 var comments = box.children;
845 845 for(var i=0; i<comments.length; i++){
846 846 var data = {
847 847 'rendered_text': comments[i].outerHTML,
848 848 'line_no': $(comments[i]).attr('line'),
849 849 'target_id': target_id
850 850 }
851 851 _renderInlineComment(data);
852 852 }
853 853 }
854 854 }
855 855
856 856 /* activate files.html stuff */
857 857 var fileBrowserListeners = function(current_url, node_list_url, url_base){
858 858 var current_url_branch = "?branch=__BRANCH__";
859 859
860 860 $('#stay_at_branch').on('click',function(e){
861 861 if(e.currentTarget.checked){
862 862 var uri = current_url_branch;
863 863 uri = uri.replace('__BRANCH__',e.currentTarget.value);
864 864 window.location = uri;
865 865 }
866 866 else{
867 867 window.location = current_url;
868 868 }
869 869 })
870 870
871 871 var $node_filter = $('#node_filter');
872 872
873 873 var filterTimeout = null;
874 874 var nodes = null;
875 875
876 876 var initFilter = function(){
877 877 $('#node_filter_box_loading').show();
878 878 $('#search_activate_id').hide();
879 879 $('#add_node_id').hide();
880 880 YUC.initHeader('X-PARTIAL-XHR',true);
881 881 YUC.asyncRequest('GET', node_list_url, {
882 882 success:function(o){
883 883 nodes = JSON.parse(o.responseText).nodes;
884 884 $('#node_filter_box_loading').hide();
885 885 $('#node_filter_box').show();
886 886 $node_filter.focus();
887 887 if($node_filter.hasClass('init')){
888 888 $node_filter.val('');
889 889 $node_filter.removeClass('init');
890 890 }
891 891 },
892 892 failure:function(o){
893 893 console.log('failed to load');
894 894 }
895 895 },null);
896 896 }
897 897
898 898 var updateFilter = function(e) {
899 899 return function(){
900 900 // Reset timeout
901 901 filterTimeout = null;
902 902 var query = e.currentTarget.value.toLowerCase();
903 903 var match = [];
904 904 var matches = 0;
905 905 var matches_max = 20;
906 906 if (query != ""){
907 907 for(var i=0;i<nodes.length;i++){
908 908 var pos = nodes[i].name.toLowerCase().indexOf(query)
909 909 if(query && pos != -1){
910 910 matches++
911 911 //show only certain amount to not kill browser
912 912 if (matches > matches_max){
913 913 break;
914 914 }
915 915
916 916 var n = nodes[i].name;
917 917 var t = nodes[i].type;
918 918 var n_hl = n.substring(0,pos)
919 919 +"<b>{0}</b>".format(n.substring(pos,pos+query.length))
920 920 +n.substring(pos+query.length)
921 921 var new_url = url_base.replace('__FPATH__',n);
922 922 match.push('<tr><td><a class="browser-{0}" href="{1}">{2}</a></td><td colspan="5"></td></tr>'.format(t,new_url,n_hl));
923 923 }
924 924 if(match.length >= matches_max){
925 925 match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_TM['Search truncated']));
926 926 break;
927 927 }
928 928 }
929 929 }
930 930 if(query != ""){
931 931 $('#tbody').hide();
932 932 $('#tbody_filtered').show();
933 933
934 934 if (match.length==0){
935 935 match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_TM['No matching files']));
936 936 }
937 937
938 938 $('#tbody_filtered').html(match.join(""));
939 939 }
940 940 else{
941 941 $('#tbody').show();
942 942 $('#tbody_filtered').hide();
943 943 }
944 944 }
945 945 };
946 946
947 947 $('#filter_activate').click(function(){
948 948 initFilter();
949 949 });
950 950 $node_filter.click(function(){
951 951 if($node_filter.hasClass('init')){
952 952 $node_filter.val('');
953 953 $node_filter.removeClass('init');
954 954 }
955 955 });
956 956 $node_filter.keyup(function(e){
957 957 clearTimeout(filterTimeout);
958 958 filterTimeout = setTimeout(updateFilter(e),600);
959 959 });
960 960 };
961 961
962 962
963 963 var initCodeMirror = function(textarea_id, resetUrl){
964 964 var myCodeMirror = CodeMirror.fromTextArea($('#' + textarea_id)[0], {
965 965 mode: "null",
966 966 lineNumbers: true,
967 967 indentUnit: 4,
968 968 autofocus: true,
969 969 });
970 970 $('#reset').click(function(e){
971 971 window.location=resetUrl;
972 972 });
973 973
974 974 $('#file_enable').click(function(){
975 975 $('#editor_container').show();
976 976 $('#upload_file_container').hide();
977 977 $('#filename_container').show();
978 978 });
979 979
980 980 $('#upload_file_enable').click(function(){
981 981 $('#editor_container').hide();
982 982 $('#upload_file_container').show();
983 983 $('#filename_container').hide();
984 984 });
985 985
986 986 return myCodeMirror
987 987 };
988 988
989 989 var setCodeMirrorMode = function(codeMirrorInstance, mode) {
990 990 codeMirrorInstance.setOption("mode", mode);
991 991 CodeMirror.autoLoadMode(codeMirrorInstance, mode);
992 992 }
993 993
994 994
995 995 var _getIdentNode = function(n){
996 996 //iterate thrugh nodes until matching interesting node
997 997
998 998 if (typeof n == 'undefined'){
999 999 return -1
1000 1000 }
1001 1001
1002 1002 if(typeof n.id != "undefined" && n.id.match('L[0-9]+')){
1003 1003 return n
1004 1004 }
1005 1005 else{
1006 1006 return _getIdentNode(n.parentNode);
1007 1007 }
1008 1008 };
1009 1009
1010 1010 /* generate links for multi line selects that can be shown by files.html page_highlights.
1011 1011 * This is a mouseup handler for hlcode from CodeHtmlFormatter and pygmentize */
1012 1012 var getSelectionLink = function(e) {
1013 1013 //get selection from start/to nodes
1014 1014 if (typeof window.getSelection != "undefined") {
1015 1015 s = window.getSelection();
1016 1016
1017 1017 from = _getIdentNode(s.anchorNode);
1018 1018 till = _getIdentNode(s.focusNode);
1019 1019
1020 1020 f_int = parseInt(from.id.replace('L',''));
1021 1021 t_int = parseInt(till.id.replace('L',''));
1022 1022
1023 1023 var yoffset = 35;
1024 1024 var ranges = [parseInt(from.id.replace('L','')), parseInt(till.id.replace('L',''))];
1025 1025 if (ranges[0] > ranges[1]){
1026 1026 //highlight from bottom
1027 1027 yoffset = -yoffset;
1028 1028 ranges = [ranges[1], ranges[0]];
1029 1029 }
1030 1030 var $hl_div = $('div#linktt');
1031 1031 // if we select more than 2 lines
1032 1032 if (ranges[0] != ranges[1]){
1033 1033 if ($hl_div.length) {
1034 1034 $hl_div.html('');
1035 1035 } else {
1036 1036 $hl_div = $('<div id="linktt" class="hl-tip-box">');
1037 1037 $('body').prepend($hl_div);
1038 1038 }
1039 1039
1040 1040 $hl_div.append($('<a>').html(_TM['Selection link']).attr('href', location.href.substring(0, location.href.indexOf('#')) + '#L' + ranges[0] + '-'+ranges[1]));
1041 1041 xy = $(till).offset();
1042 1042 $hl_div.css('top', (xy.top + yoffset) + 'px').css('left', xy.left + 'px');
1043 1043 $hl_div.show();
1044 1044 }
1045 1045 else{
1046 1046 $hl_div.hide();
1047 1047 }
1048 1048 }
1049 1049 };
1050 1050
1051 1051 var deleteNotification = function(url, notification_id, callbacks){
1052 1052 var callback = {
1053 1053 success:function(o){
1054 1054 $("#notification_"+notification_id).remove();
1055 1055 _run_callbacks(callbacks);
1056 1056 },
1057 1057 failure:function(o){
1058 1058 alert("error");
1059 1059 },
1060 1060 };
1061 1061 var postData = '_method=delete';
1062 1062 var sUrl = url.replace('__NOTIFICATION_ID__',notification_id);
1063 1063 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,
1064 1064 callback, postData);
1065 1065 };
1066 1066
1067 1067 var readNotification = function(url, notification_id, callbacks){
1068 1068 var callback = {
1069 1069 success:function(o){
1070 1070 var $obj = $("#notification_"+notification_id);
1071 1071 $obj.removeClass('unread');
1072 1072 $obj.find('.read-notification').remove();
1073 1073 _run_callbacks(callbacks);
1074 1074 },
1075 1075 failure:function(o){
1076 1076 alert("error");
1077 1077 },
1078 1078 };
1079 1079 var postData = '_method=put';
1080 1080 var sUrl = url.replace('__NOTIFICATION_ID__',notification_id);
1081 1081 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,
1082 1082 callback, postData);
1083 1083 };
1084 1084
1085 1085 /** MEMBERS AUTOCOMPLETE WIDGET **/
1086 1086
1087 1087 var _MembersAutoComplete = function (divid, cont, users_list, groups_list) {
1088 1088 var myUsers = users_list;
1089 1089 var myGroups = groups_list;
1090 1090
1091 1091 // Define a custom search function for the DataSource of users
1092 1092 var matchUsers = function (sQuery) {
1093 1093 // Case insensitive matching
1094 1094 var query = sQuery.toLowerCase();
1095 1095 var i = 0;
1096 1096 var l = myUsers.length;
1097 1097 var matches = [];
1098 1098
1099 1099 // Match against each name of each contact
1100 1100 for (; i < l; i++) {
1101 1101 contact = myUsers[i];
1102 1102 if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
1103 1103 ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
1104 1104 ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
1105 1105 matches[matches.length] = contact;
1106 1106 }
1107 1107 }
1108 1108 return matches;
1109 1109 };
1110 1110
1111 1111 // Define a custom search function for the DataSource of userGroups
1112 1112 var matchGroups = function (sQuery) {
1113 1113 // Case insensitive matching
1114 1114 var query = sQuery.toLowerCase();
1115 1115 var i = 0;
1116 1116 var l = myGroups.length;
1117 1117 var matches = [];
1118 1118
1119 1119 // Match against each name of each contact
1120 1120 for (; i < l; i++) {
1121 1121 matched_group = myGroups[i];
1122 1122 if (matched_group.grname.toLowerCase().indexOf(query) > -1) {
1123 1123 matches[matches.length] = matched_group;
1124 1124 }
1125 1125 }
1126 1126 return matches;
1127 1127 };
1128 1128
1129 1129 //match all
1130 1130 var matchAll = function (sQuery) {
1131 1131 u = matchUsers(sQuery);
1132 1132 g = matchGroups(sQuery);
1133 1133 return u.concat(g);
1134 1134 };
1135 1135
1136 1136 // DataScheme for members
1137 1137 var memberDS = new YAHOO.util.FunctionDataSource(matchAll);
1138 1138 memberDS.responseSchema = {
1139 1139 fields: ["id", "fname", "lname", "nname", "grname", "grmembers", "gravatar_lnk"]
1140 1140 };
1141 1141
1142 1142 // DataScheme for owner
1143 1143 var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
1144 1144 ownerDS.responseSchema = {
1145 1145 fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
1146 1146 };
1147 1147
1148 1148 // Instantiate AutoComplete for perms
1149 1149 var membersAC = new YAHOO.widget.AutoComplete(divid, cont, memberDS);
1150 1150 membersAC.useShadow = false;
1151 1151 membersAC.resultTypeList = false;
1152 1152 membersAC.animVert = false;
1153 1153 membersAC.animHoriz = false;
1154 1154 membersAC.animSpeed = 0.1;
1155 1155
1156 1156 // Instantiate AutoComplete for owner
1157 1157 var ownerAC = new YAHOO.widget.AutoComplete("user", "owner_container", ownerDS);
1158 1158 ownerAC.useShadow = false;
1159 1159 ownerAC.resultTypeList = false;
1160 1160 ownerAC.animVert = false;
1161 1161 ownerAC.animHoriz = false;
1162 1162 ownerAC.animSpeed = 0.1;
1163 1163
1164 1164 // Helper highlight function for the formatter
1165 1165 var highlightMatch = function (full, snippet, matchindex) {
1166 1166 return full.substring(0, matchindex)
1167 1167 + "<span class='match'>"
1168 1168 + full.substr(matchindex, snippet.length)
1169 1169 + "</span>" + full.substring(matchindex + snippet.length);
1170 1170 };
1171 1171
1172 1172 // Custom formatter to highlight the matching letters
1173 1173 var custom_formatter = function (oResultData, sQuery, sResultMatch) {
1174 1174 var query = sQuery.toLowerCase();
1175 1175 var _gravatar = function(res, em, group){
1176 1176 if (group !== undefined){
1177 1177 em = '/images/icons/group.png'
1178 1178 }
1179 1179 tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
1180 1180 return tmpl.format(em,res)
1181 1181 }
1182 1182 // group
1183 1183 if (oResultData.grname != undefined) {
1184 1184 var grname = oResultData.grname;
1185 1185 var grmembers = oResultData.grmembers;
1186 1186 var grnameMatchIndex = grname.toLowerCase().indexOf(query);
1187 1187 var grprefix = "{0}: ".format(_TM['Group']);
1188 1188 var grsuffix = " (" + grmembers + " )";
1189 1189 var grsuffix = " ({0} {1})".format(grmembers, _TM['members']);
1190 1190
1191 1191 if (grnameMatchIndex > -1) {
1192 1192 return _gravatar(grprefix + highlightMatch(grname, query, grnameMatchIndex) + grsuffix,null,true);
1193 1193 }
1194 1194 return _gravatar(grprefix + oResultData.grname + grsuffix, null,true);
1195 1195 // Users
1196 1196 } else if (oResultData.nname != undefined) {
1197 1197 var fname = oResultData.fname || "";
1198 1198 var lname = oResultData.lname || "";
1199 1199 var nname = oResultData.nname;
1200 1200
1201 1201 // Guard against null value
1202 1202 var fnameMatchIndex = fname.toLowerCase().indexOf(query),
1203 1203 lnameMatchIndex = lname.toLowerCase().indexOf(query),
1204 1204 nnameMatchIndex = nname.toLowerCase().indexOf(query),
1205 1205 displayfname, displaylname, displaynname;
1206 1206
1207 1207 if (fnameMatchIndex > -1) {
1208 1208 displayfname = highlightMatch(fname, query, fnameMatchIndex);
1209 1209 } else {
1210 1210 displayfname = fname;
1211 1211 }
1212 1212
1213 1213 if (lnameMatchIndex > -1) {
1214 1214 displaylname = highlightMatch(lname, query, lnameMatchIndex);
1215 1215 } else {
1216 1216 displaylname = lname;
1217 1217 }
1218 1218
1219 1219 if (nnameMatchIndex > -1) {
1220 1220 displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
1221 1221 } else {
1222 1222 displaynname = nname ? "(" + nname + ")" : "";
1223 1223 }
1224 1224
1225 1225 return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
1226 1226 } else {
1227 1227 return '';
1228 1228 }
1229 1229 };
1230 1230 membersAC.formatResult = custom_formatter;
1231 1231 ownerAC.formatResult = custom_formatter;
1232 1232
1233 1233 var myHandler = function (sType, aArgs) {
1234 1234 var nextId = divid.split('perm_new_member_name_')[1];
1235 1235 var myAC = aArgs[0]; // reference back to the AC instance
1236 1236 var elLI = aArgs[1]; // reference to the selected LI element
1237 1237 var oData = aArgs[2]; // object literal of selected item's result data
1238 1238 //fill the autocomplete with value
1239 1239 if (oData.nname != undefined) {
1240 1240 //users
1241 1241 myAC.getInputEl().value = oData.nname;
1242 1242 $('#perm_new_member_type_'+nextId).val('user');
1243 1243 } else {
1244 1244 //groups
1245 1245 myAC.getInputEl().value = oData.grname;
1246 1246 $('#perm_new_member_type_'+nextId).val('users_group');
1247 1247 }
1248 1248 };
1249 1249
1250 1250 membersAC.itemSelectEvent.subscribe(myHandler);
1251 1251 if(ownerAC.itemSelectEvent){
1252 1252 ownerAC.itemSelectEvent.subscribe(myHandler);
1253 1253 }
1254 1254
1255 1255 return {
1256 1256 memberDS: memberDS,
1257 1257 ownerDS: ownerDS,
1258 1258 membersAC: membersAC,
1259 1259 ownerAC: ownerAC,
1260 1260 };
1261 1261 }
1262 1262
1263 1263 var MentionsAutoComplete = function (divid, cont, users_list, groups_list) {
1264 1264 var myUsers = users_list;
1265 1265 var myGroups = groups_list;
1266 1266
1267 1267 // Define a custom search function for the DataSource of users
1268 1268 var matchUsers = function (sQuery) {
1269 1269 var org_sQuery = sQuery;
1270 1270 if(this.mentionQuery == null){
1271 1271 return []
1272 1272 }
1273 1273 sQuery = this.mentionQuery;
1274 1274 // Case insensitive matching
1275 1275 var query = sQuery.toLowerCase();
1276 1276 var i = 0;
1277 1277 var l = myUsers.length;
1278 1278 var matches = [];
1279 1279
1280 1280 // Match against each name of each contact
1281 1281 for (; i < l; i++) {
1282 1282 contact = myUsers[i];
1283 1283 if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
1284 1284 ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
1285 1285 ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
1286 1286 matches[matches.length] = contact;
1287 1287 }
1288 1288 }
1289 1289 return matches
1290 1290 };
1291 1291
1292 1292 //match all
1293 1293 var matchAll = function (sQuery) {
1294 1294 u = matchUsers(sQuery);
1295 1295 return u
1296 1296 };
1297 1297
1298 1298 // DataScheme for owner
1299 1299 var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
1300 1300
1301 1301 ownerDS.responseSchema = {
1302 1302 fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
1303 1303 };
1304 1304
1305 1305 // Instantiate AutoComplete for mentions
1306 1306 var ownerAC = new YAHOO.widget.AutoComplete(divid, cont, ownerDS);
1307 1307 ownerAC.useShadow = false;
1308 1308 ownerAC.resultTypeList = false;
1309 1309 ownerAC.suppressInputUpdate = true;
1310 1310 ownerAC.animVert = false;
1311 1311 ownerAC.animHoriz = false;
1312 1312 ownerAC.animSpeed = 0.1;
1313 1313
1314 1314 // Helper highlight function for the formatter
1315 1315 var highlightMatch = function (full, snippet, matchindex) {
1316 1316 return full.substring(0, matchindex)
1317 1317 + "<span class='match'>"
1318 1318 + full.substr(matchindex, snippet.length)
1319 1319 + "</span>" + full.substring(matchindex + snippet.length);
1320 1320 };
1321 1321
1322 1322 // Custom formatter to highlight the matching letters
1323 1323 ownerAC.formatResult = function (oResultData, sQuery, sResultMatch) {
1324 1324 var org_sQuery = sQuery;
1325 1325 if(this.dataSource.mentionQuery != null){
1326 1326 sQuery = this.dataSource.mentionQuery;
1327 1327 }
1328 1328
1329 1329 var query = sQuery.toLowerCase();
1330 1330 var _gravatar = function(res, em, group){
1331 1331 if (group !== undefined){
1332 1332 em = '/images/icons/group.png'
1333 1333 }
1334 1334 tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
1335 1335 return tmpl.format(em,res)
1336 1336 }
1337 1337 if (oResultData.nname != undefined) {
1338 1338 var fname = oResultData.fname || "";
1339 1339 var lname = oResultData.lname || "";
1340 1340 var nname = oResultData.nname;
1341 1341
1342 1342 // Guard against null value
1343 1343 var fnameMatchIndex = fname.toLowerCase().indexOf(query),
1344 1344 lnameMatchIndex = lname.toLowerCase().indexOf(query),
1345 1345 nnameMatchIndex = nname.toLowerCase().indexOf(query),
1346 1346 displayfname, displaylname, displaynname;
1347 1347
1348 1348 if (fnameMatchIndex > -1) {
1349 1349 displayfname = highlightMatch(fname, query, fnameMatchIndex);
1350 1350 } else {
1351 1351 displayfname = fname;
1352 1352 }
1353 1353
1354 1354 if (lnameMatchIndex > -1) {
1355 1355 displaylname = highlightMatch(lname, query, lnameMatchIndex);
1356 1356 } else {
1357 1357 displaylname = lname;
1358 1358 }
1359 1359
1360 1360 if (nnameMatchIndex > -1) {
1361 1361 displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
1362 1362 } else {
1363 1363 displaynname = nname ? "(" + nname + ")" : "";
1364 1364 }
1365 1365
1366 1366 return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
1367 1367 } else {
1368 1368 return '';
1369 1369 }
1370 1370 };
1371 1371
1372 1372 if(ownerAC.itemSelectEvent){
1373 1373 ownerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
1374 1374
1375 1375 var myAC = aArgs[0]; // reference back to the AC instance
1376 1376 var elLI = aArgs[1]; // reference to the selected LI element
1377 1377 var oData = aArgs[2]; // object literal of selected item's result data
1378 1378 //fill the autocomplete with value
1379 1379 if (oData.nname != undefined) {
1380 1380 //users
1381 1381 //Replace the mention name with replaced
1382 1382 var re = new RegExp();
1383 1383 var org = myAC.getInputEl().value;
1384 1384 var chunks = myAC.dataSource.chunks
1385 1385 // replace middle chunk(the search term) with actuall match
1386 1386 chunks[1] = chunks[1].replace('@'+myAC.dataSource.mentionQuery,
1387 1387 '@'+oData.nname+' ');
1388 1388 myAC.getInputEl().value = chunks.join('')
1389 1389 myAC.getInputEl().focus(); // Y U NO WORK !?
1390 1390 } else {
1391 1391 //groups
1392 1392 myAC.getInputEl().value = oData.grname;
1393 1393 $('#perm_new_member_type').val('users_group');
1394 1394 }
1395 1395 });
1396 1396 }
1397 1397
1398 1398 // in this keybuffer we will gather current value of search !
1399 1399 // since we need to get this just when someone does `@` then we do the
1400 1400 // search
1401 1401 ownerAC.dataSource.chunks = [];
1402 1402 ownerAC.dataSource.mentionQuery = null;
1403 1403
1404 1404 ownerAC.get_mention = function(msg, max_pos) {
1405 1405 var org = msg;
1406 1406 var re = new RegExp('(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)$')
1407 1407 var chunks = [];
1408 1408
1409 1409 // cut first chunk until curret pos
1410 1410 var to_max = msg.substr(0, max_pos);
1411 1411 var at_pos = Math.max(0,to_max.lastIndexOf('@')-1);
1412 1412 var msg2 = to_max.substr(at_pos);
1413 1413
1414 1414 chunks.push(org.substr(0,at_pos))// prefix chunk
1415 1415 chunks.push(msg2) // search chunk
1416 1416 chunks.push(org.substr(max_pos)) // postfix chunk
1417 1417
1418 1418 // clean up msg2 for filtering and regex match
1419 1419 var msg2 = msg2.lstrip(' ').lstrip('\n');
1420 1420
1421 1421 if(re.test(msg2)){
1422 1422 var unam = re.exec(msg2)[1];
1423 1423 return [unam, chunks];
1424 1424 }
1425 1425 return [null, null];
1426 1426 };
1427 1427
1428 1428 if (ownerAC.textboxKeyUpEvent){
1429 1429 ownerAC.textboxKeyUpEvent.subscribe(function(type, args){
1430 1430
1431 1431 var ac_obj = args[0];
1432 1432 var currentMessage = args[1];
1433 1433 var currentCaretPosition = args[0]._elTextbox.selectionStart;
1434 1434
1435 1435 var unam = ownerAC.get_mention(currentMessage, currentCaretPosition);
1436 1436 var curr_search = null;
1437 1437 if(unam[0]){
1438 1438 curr_search = unam[0];
1439 1439 }
1440 1440
1441 1441 ownerAC.dataSource.chunks = unam[1];
1442 1442 ownerAC.dataSource.mentionQuery = curr_search;
1443 1443
1444 1444 })
1445 1445 }
1446 1446 return {
1447 1447 ownerDS: ownerDS,
1448 1448 ownerAC: ownerAC,
1449 1449 };
1450 1450 }
1451 1451
1452 1452 var addReviewMember = function(id,fname,lname,nname,gravatar_link){
1453 1453 var tmpl = '<li id="reviewer_{2}">'+
1454 1454 '<div class="reviewers_member">'+
1455 1455 '<div class="gravatar"><img alt="gravatar" src="{0}"/> </div>'+
1456 1456 '<div style="float:left">{1}</div>'+
1457 1457 '<input type="hidden" value="{2}" name="review_members" />'+
1458 1458 '<span class="delete_icon action_button" onclick="removeReviewMember({2})"></span>'+
1459 1459 '</div>'+
1460 1460 '</li>' ;
1461 1461 var displayname = "{0} {1} ({2})".format(fname,lname,nname);
1462 1462 var element = tmpl.format(gravatar_link,displayname,id);
1463 1463 // check if we don't have this ID already in
1464 1464 var ids = [];
1465 1465 $('#review_members').find('li').each(function() {
1466 1466 ids.push(this.id);
1467 1467 });
1468 1468 if(ids.indexOf('reviewer_'+id) == -1){
1469 1469 //only add if it's not there
1470 1470 $('#review_members').append(element);
1471 1471 }
1472 1472 }
1473 1473
1474 1474 var removeReviewMember = function(reviewer_id, repo_name, pull_request_id){
1475 1475 $('#reviewer_{0}'.format(reviewer_id)).remove();
1476 1476 }
1477 1477
1478 1478 /* handle "Save Changes" of addReviewMember and removeReviewMember on PR */
1479 1479 var updateReviewers = function(reviewers_ids, repo_name, pull_request_id){
1480 1480 if (reviewers_ids === undefined){
1481 1481 var reviewers_ids = [];
1482 1482 $('#review_members').find('input').each(function(){
1483 1483 reviewers_ids.push(this.value);
1484 1484 });
1485 1485 }
1486 1486 var url = pyroutes.url('pullrequest_update', {"repo_name":repo_name,
1487 1487 "pull_request_id": pull_request_id});
1488 1488 var postData = {'_method':'put',
1489 1489 'reviewers_ids': reviewers_ids};
1490 1490 var success = function(o){
1491 1491 window.location.reload();
1492 1492 }
1493 1493 ajaxPOST(url,postData,success);
1494 1494 }
1495 1495
1496 1496 /* activate auto completion of users and groups ... but only used for users as PR reviewers */
1497 1497 var PullRequestAutoComplete = function (divid, cont, users_list, groups_list) {
1498 1498 var myUsers = users_list;
1499 1499 var myGroups = groups_list;
1500 1500
1501 1501 // Define a custom search function for the DataSource of users
1502 1502 var matchUsers = function (sQuery) {
1503 1503 // Case insensitive matching
1504 1504 var query = sQuery.toLowerCase();
1505 1505 var i = 0;
1506 1506 var l = myUsers.length;
1507 1507 var matches = [];
1508 1508
1509 1509 // Match against each name of each contact
1510 1510 for (; i < l; i++) {
1511 1511 contact = myUsers[i];
1512 1512 if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
1513 1513 ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
1514 1514 ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
1515 1515 matches[matches.length] = contact;
1516 1516 }
1517 1517 }
1518 1518 return matches;
1519 1519 };
1520 1520
1521 1521 // Define a custom search function for the DataSource of userGroups
1522 1522 var matchGroups = function (sQuery) {
1523 1523 // Case insensitive matching
1524 1524 var query = sQuery.toLowerCase();
1525 1525 var i = 0;
1526 1526 var l = myGroups.length;
1527 1527 var matches = [];
1528 1528
1529 1529 // Match against each name of each contact
1530 1530 for (; i < l; i++) {
1531 1531 matched_group = myGroups[i];
1532 1532 if (matched_group.grname.toLowerCase().indexOf(query) > -1) {
1533 1533 matches[matches.length] = matched_group;
1534 1534 }
1535 1535 }
1536 1536 return matches;
1537 1537 };
1538 1538
1539 1539 //match all
1540 1540 var matchAll = function (sQuery) {
1541 1541 u = matchUsers(sQuery);
1542 1542 return u
1543 1543 };
1544 1544
1545 1545 // DataScheme for owner
1546 1546 var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
1547 1547
1548 1548 ownerDS.responseSchema = {
1549 1549 fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
1550 1550 };
1551 1551
1552 1552 // Instantiate AutoComplete for mentions
1553 1553 var reviewerAC = new YAHOO.widget.AutoComplete(divid, cont, ownerDS);
1554 1554 reviewerAC.useShadow = false;
1555 1555 reviewerAC.resultTypeList = false;
1556 1556 reviewerAC.suppressInputUpdate = true;
1557 1557 reviewerAC.animVert = false;
1558 1558 reviewerAC.animHoriz = false;
1559 1559 reviewerAC.animSpeed = 0.1;
1560 1560
1561 1561 // Helper highlight function for the formatter
1562 1562 var highlightMatch = function (full, snippet, matchindex) {
1563 1563 return full.substring(0, matchindex)
1564 1564 + "<span class='match'>"
1565 1565 + full.substr(matchindex, snippet.length)
1566 1566 + "</span>" + full.substring(matchindex + snippet.length);
1567 1567 };
1568 1568
1569 1569 // Custom formatter to highlight the matching letters
1570 1570 reviewerAC.formatResult = function (oResultData, sQuery, sResultMatch) {
1571 1571 var org_sQuery = sQuery;
1572 1572 if(this.dataSource.mentionQuery != null){
1573 1573 sQuery = this.dataSource.mentionQuery;
1574 1574 }
1575 1575
1576 1576 var query = sQuery.toLowerCase();
1577 1577 var _gravatar = function(res, em, group){
1578 1578 if (group !== undefined){
1579 1579 em = '/images/icons/group.png'
1580 1580 }
1581 1581 tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
1582 1582 return tmpl.format(em,res)
1583 1583 }
1584 1584 if (oResultData.nname != undefined) {
1585 1585 var fname = oResultData.fname || "";
1586 1586 var lname = oResultData.lname || "";
1587 1587 var nname = oResultData.nname;
1588 1588
1589 1589 // Guard against null value
1590 1590 var fnameMatchIndex = fname.toLowerCase().indexOf(query),
1591 1591 lnameMatchIndex = lname.toLowerCase().indexOf(query),
1592 1592 nnameMatchIndex = nname.toLowerCase().indexOf(query),
1593 1593 displayfname, displaylname, displaynname;
1594 1594
1595 1595 if (fnameMatchIndex > -1) {
1596 1596 displayfname = highlightMatch(fname, query, fnameMatchIndex);
1597 1597 } else {
1598 1598 displayfname = fname;
1599 1599 }
1600 1600
1601 1601 if (lnameMatchIndex > -1) {
1602 1602 displaylname = highlightMatch(lname, query, lnameMatchIndex);
1603 1603 } else {
1604 1604 displaylname = lname;
1605 1605 }
1606 1606
1607 1607 if (nnameMatchIndex > -1) {
1608 1608 displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
1609 1609 } else {
1610 1610 displaynname = nname ? "(" + nname + ")" : "";
1611 1611 }
1612 1612
1613 1613 return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
1614 1614 } else {
1615 1615 return '';
1616 1616 }
1617 1617 };
1618 1618
1619 1619 //members cache to catch duplicates
1620 1620 reviewerAC.dataSource.cache = [];
1621 1621 // hack into select event
1622 1622 if(reviewerAC.itemSelectEvent){
1623 1623 reviewerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
1624 1624
1625 1625 var myAC = aArgs[0]; // reference back to the AC instance
1626 1626 var elLI = aArgs[1]; // reference to the selected LI element
1627 1627 var oData = aArgs[2]; // object literal of selected item's result data
1628 1628
1629 1629 //fill the autocomplete with value
1630 1630 if (oData.nname != undefined) {
1631 1631 addReviewMember(oData.id, oData.fname, oData.lname, oData.nname,
1632 1632 oData.gravatar_lnk);
1633 1633 myAC.dataSource.cache.push(oData.id);
1634 1634 $('#user').val('');
1635 1635 }
1636 1636 });
1637 1637 }
1638 1638 }
1639 1639
1640 1640 /**
1641 1641 * Activate .quick_repo_menu
1642 1642 */
1643 1643 var quick_repo_menu = function(){
1644 1644 $(".quick_repo_menu").mouseenter(function(e) {
1645 1645 var $menu = $(e.currentTarget).children().first().children().first();
1646 1646 if($menu.hasClass('hidden')){
1647 1647 $menu.removeClass('hidden').addClass('active');
1648 1648 $(e.currentTarget).removeClass('hidden').addClass('active');
1649 1649 }
1650 1650 })
1651 1651 $(".quick_repo_menu").mouseleave(function(e) {
1652 1652 var $menu = $(e.currentTarget).children().first().children().first();
1653 1653 if($menu.hasClass('active')){
1654 1654 $menu.removeClass('active').addClass('hidden');
1655 1655 $(e.currentTarget).removeClass('active').addClass('hidden');
1656 1656 }
1657 1657 })
1658 1658 };
1659 1659
1660 1660
1661 1661 /**
1662 1662 * TABLE SORTING
1663 1663 */
1664 1664
1665 1665 var revisionSort = function(a, b, desc, field) {
1666 1666 var a_ = parseInt(a.getData('last_rev_raw') || 0);
1667 1667 var b_ = parseInt(b.getData('last_rev_raw') || 0);
1668 1668
1669 1669 return YAHOO.util.Sort.compare(a_, b_, desc);
1670 1670 };
1671 1671
1672 1672 var ageSort = function(a, b, desc, field) {
1673 1673 // data is like: <span class="tooltip" date="2014-06-04 18:18:55.325474" title="Wed, 04 Jun 2014 18:18:55">1 day and 23 hours ago</span>
1674 1674 var a_ = $(a.getData(field)).attr('date');
1675 1675 var b_ = $(b.getData(field)).attr('date');
1676 1676
1677 1677 return YAHOO.util.Sort.compare(a_, b_, desc);
1678 1678 };
1679 1679
1680 1680 var lastLoginSort = function(a, b, desc, field) {
1681 1681 var a_ = parseFloat(a.getData('last_login_raw') || 0);
1682 1682 var b_ = parseFloat(b.getData('last_login_raw') || 0);
1683 1683
1684 1684 return YAHOO.util.Sort.compare(a_, b_, desc);
1685 1685 };
1686 1686
1687 1687 var nameSort = function(a, b, desc, field) {
1688 1688 var a_ = a.getData('raw_name') || 0;
1689 1689 var b_ = b.getData('raw_name') || 0;
1690 1690
1691 1691 return YAHOO.util.Sort.compare(a_, b_, desc);
1692 1692 };
1693 1693
1694 1694 var dateSort = function(a, b, desc, field) {
1695 1695 var a_ = parseFloat(a.getData('raw_date') || 0);
1696 1696 var b_ = parseFloat(b.getData('raw_date') || 0);
1697 1697
1698 1698 return YAHOO.util.Sort.compare(a_, b_, desc);
1699 1699 };
1700 1700
1701 1701 var addPermAction = function(_html, users_list, groups_list){
1702 1702 var $last_node = $('.last_new_member').last(); // empty tr between last and add
1703 1703 var next_id = $('.new_members').length;
1704 1704 $last_node.before($('<tr class="new_members">').append(_html.format(next_id)));
1705 1705 _MembersAutoComplete("perm_new_member_name_"+next_id,
1706 1706 "perm_container_"+next_id, users_list, groups_list);
1707 1707 }
1708 1708
1709 1709 function ajaxActionRevokePermission(url, obj_id, obj_type, field_id, extra_data) {
1710 1710 var callback = {
1711 1711 success: function (o) {
1712 1712 $('#' + field_id).remove();
1713 1713 },
1714 1714 failure: function (o) {
1715 1715 alert(_TM['Failed to remoke permission'] + ": " + o.status);
1716 1716 },
1717 1717 };
1718 1718 query_params = {
1719 1719 '_method': 'delete'
1720 1720 }
1721 1721 // put extra data into POST
1722 1722 if (extra_data !== undefined && (typeof extra_data === 'object')){
1723 1723 for(k in extra_data){
1724 1724 query_params[k] = extra_data[k];
1725 1725 }
1726 1726 }
1727 1727
1728 1728 if (obj_type=='user'){
1729 1729 query_params['user_id'] = obj_id;
1730 1730 query_params['obj_type'] = 'user';
1731 1731 }
1732 1732 else if (obj_type=='user_group'){
1733 1733 query_params['user_group_id'] = obj_id;
1734 1734 query_params['obj_type'] = 'user_group';
1735 1735 }
1736 1736
1737 1737 var request = YAHOO.util.Connect.asyncRequest('POST', url, callback,
1738 1738 _toQueryString(query_params));
1739 1739 };
1740 1740
1741 1741 /* Multi selectors */
1742 1742
1743 1743 var MultiSelectWidget = function(selected_id, available_id, form_id){
1744 1744 var $availableselect = $('#' + available_id);
1745 1745 var $selectedselect = $('#' + selected_id);
1746 1746
1747 1747 //fill available only with those not in selected
1748 1748 var $selectedoptions = $selectedselect.children('option');
1749 1749 $availableselect.children('option').filter(function(i, e){
1750 1750 for(var j = 0, node; node = $selectedoptions[j]; j++){
1751 1751 if(node.value == e.value){
1752 1752 return true;
1753 1753 }
1754 1754 }
1755 1755 return false;
1756 1756 }).remove();
1757 1757
1758 1758 $('#add_element').click(function(e){
1759 1759 $selectedselect.append($availableselect.children('option:selected'));
1760 1760 });
1761 1761 $('#remove_element').click(function(e){
1762 1762 $availableselect.append($selectedselect.children('option:selected'));
1763 1763 });
1764 1764 $('#add_all_elements').click(function(e){
1765 1765 $selectedselect.append($availableselect.children('option'));
1766 1766 });
1767 1767 $('#remove_all_elements').click(function(e){
1768 1768 $availableselect.append($selectedselect.children('option'));
1769 1769 });
1770 1770
1771 1771 $('#'+form_id).submit(function(){
1772 1772 $selectedselect.children('option').each(function(i, e){
1773 1773 e.selected = 'selected';
1774 1774 });
1775 1775 });
1776 1776 }
1777 1777
1778 1778 // custom paginator
1779 1779 var YUI_paginator = function(links_per_page, containers){
1780 1780
1781 1781 (function () {
1782 1782
1783 1783 var Paginator = YAHOO.widget.Paginator,
1784 1784 l = YAHOO.lang,
1785 1785 setId = YAHOO.util.Dom.generateId;
1786 1786
1787 1787 Paginator.ui.MyFirstPageLink = function (p) {
1788 1788 this.paginator = p;
1789 1789
1790 1790 p.subscribe('recordOffsetChange',this.update,this,true);
1791 1791 p.subscribe('rowsPerPageChange',this.update,this,true);
1792 1792 p.subscribe('totalRecordsChange',this.update,this,true);
1793 1793 p.subscribe('destroy',this.destroy,this,true);
1794 1794
1795 1795 // TODO: make this work
1796 1796 p.subscribe('firstPageLinkLabelChange',this.update,this,true);
1797 1797 p.subscribe('firstPageLinkClassChange',this.update,this,true);
1798 1798 };
1799 1799
1800 1800 Paginator.ui.MyFirstPageLink.init = function (p) {
1801 1801 p.setAttributeConfig('firstPageLinkLabel', {
1802 1802 value : 1,
1803 1803 validator : l.isString
1804 1804 });
1805 1805 p.setAttributeConfig('firstPageLinkClass', {
1806 1806 value : 'yui-pg-first',
1807 1807 validator : l.isString
1808 1808 });
1809 1809 p.setAttributeConfig('firstPageLinkTitle', {
1810 1810 value : 'First Page',
1811 1811 validator : l.isString
1812 1812 });
1813 1813 };
1814 1814
1815 1815 // Instance members and methods
1816 1816 Paginator.ui.MyFirstPageLink.prototype = {
1817 1817 current : null,
1818 1818 leftmost_page: null,
1819 1819 rightmost_page: null,
1820 1820 link : null,
1821 1821 span : null,
1822 1822 dotdot : null,
1823 1823 getPos : function(cur_page, max_page, items){
1824 1824 var edge = parseInt(items / 2) + 1;
1825 1825 if (cur_page <= edge){
1826 1826 var radius = Math.max(parseInt(items / 2), items - cur_page);
1827 1827 }
1828 1828 else if ((max_page - cur_page) < edge) {
1829 1829 var radius = (items - 1) - (max_page - cur_page);
1830 1830 }
1831 1831 else{
1832 1832 var radius = parseInt(items / 2);
1833 1833 }
1834 1834
1835 1835 var left = Math.max(1, (cur_page - (radius)))
1836 1836 var right = Math.min(max_page, cur_page + (radius))
1837 1837 return [left, cur_page, right]
1838 1838 },
1839 1839 render : function (id_base) {
1840 1840 var p = this.paginator,
1841 1841 c = p.get('firstPageLinkClass'),
1842 1842 label = p.get('firstPageLinkLabel'),
1843 1843 title = p.get('firstPageLinkTitle');
1844 1844
1845 1845 this.link = document.createElement('a');
1846 1846 this.span = document.createElement('span');
1847 1847 $(this.span).hide();
1848 1848
1849 1849 var _pos = this.getPos(p.getCurrentPage(), p.getTotalPages(), 5);
1850 1850 this.leftmost_page = _pos[0];
1851 1851 this.rightmost_page = _pos[2];
1852 1852
1853 1853 setId(this.link, id_base + '-first-link');
1854 1854 this.link.href = '#';
1855 1855 this.link.className = c;
1856 1856 this.link.innerHTML = label;
1857 1857 this.link.title = title;
1858 1858 YUE.on(this.link,'click',this.onClick,this,true);
1859 1859
1860 1860 setId(this.span, id_base + '-first-span');
1861 1861 this.span.className = c;
1862 1862 this.span.innerHTML = label;
1863 1863
1864 1864 this.current = p.getCurrentPage() > 1 ? this.link : this.span;
1865 1865 return this.current;
1866 1866 },
1867 1867 update : function (e) {
1868 1868 var p = this.paginator;
1869 1869 var _pos = this.getPos(p.getCurrentPage(), p.getTotalPages(), 5);
1870 1870 this.leftmost_page = _pos[0];
1871 1871 this.rightmost_page = _pos[2];
1872 1872
1873 1873 if (e && e.prevValue === e.newValue) {
1874 1874 return;
1875 1875 }
1876 1876
1877 1877 var par = this.current ? this.current.parentNode : null;
1878 1878 if (this.leftmost_page > 1) {
1879 1879 if (par && this.current === this.span) {
1880 1880 par.replaceChild(this.link,this.current);
1881 1881 this.current = this.link;
1882 1882 }
1883 1883 } else {
1884 1884 if (par && this.current === this.link) {
1885 1885 par.replaceChild(this.span,this.current);
1886 1886 this.current = this.span;
1887 1887 }
1888 1888 }
1889 1889 },
1890 1890 destroy : function () {
1891 1891 YUE.purgeElement(this.link);
1892 1892 this.current.parentNode.removeChild(this.current);
1893 1893 this.link = this.span = null;
1894 1894 },
1895 1895 onClick : function (e) {
1896 1896 YUE.stopEvent(e);
1897 1897 this.paginator.setPage(1);
1898 1898 }
1899 1899 };
1900 1900
1901 1901 })();
1902 1902
1903 1903 (function () {
1904 1904
1905 1905 var Paginator = YAHOO.widget.Paginator,
1906 1906 l = YAHOO.lang,
1907 1907 setId = YAHOO.util.Dom.generateId;
1908 1908
1909 1909 Paginator.ui.MyLastPageLink = function (p) {
1910 1910 this.paginator = p;
1911 1911
1912 1912 p.subscribe('recordOffsetChange',this.update,this,true);
1913 1913 p.subscribe('rowsPerPageChange',this.update,this,true);
1914 1914 p.subscribe('totalRecordsChange',this.update,this,true);
1915 1915 p.subscribe('destroy',this.destroy,this,true);
1916 1916
1917 1917 // TODO: make this work
1918 1918 p.subscribe('lastPageLinkLabelChange',this.update,this,true);
1919 1919 p.subscribe('lastPageLinkClassChange', this.update,this,true);
1920 1920 };
1921 1921
1922 1922 Paginator.ui.MyLastPageLink.init = function (p) {
1923 1923 p.setAttributeConfig('lastPageLinkLabel', {
1924 1924 value : -1,
1925 1925 validator : l.isString
1926 1926 });
1927 1927 p.setAttributeConfig('lastPageLinkClass', {
1928 1928 value : 'yui-pg-last',
1929 1929 validator : l.isString
1930 1930 });
1931 1931 p.setAttributeConfig('lastPageLinkTitle', {
1932 1932 value : 'Last Page',
1933 1933 validator : l.isString
1934 1934 });
1935 1935
1936 1936 };
1937 1937
1938 1938 Paginator.ui.MyLastPageLink.prototype = {
1939 1939
1940 1940 current : null,
1941 1941 leftmost_page: null,
1942 1942 rightmost_page: null,
1943 1943 link : null,
1944 1944 span : null,
1945 1945 dotdot : null,
1946 1946 na : null,
1947 1947 getPos : function(cur_page, max_page, items){
1948 1948 var edge = parseInt(items / 2) + 1;
1949 1949 if (cur_page <= edge){
1950 1950 var radius = Math.max(parseInt(items / 2), items - cur_page);
1951 1951 }
1952 1952 else if ((max_page - cur_page) < edge) {
1953 1953 var radius = (items - 1) - (max_page - cur_page);
1954 1954 }
1955 1955 else{
1956 1956 var radius = parseInt(items / 2);
1957 1957 }
1958 1958
1959 1959 var left = Math.max(1, (cur_page - (radius)))
1960 1960 var right = Math.min(max_page, cur_page + (radius))
1961 1961 return [left, cur_page, right]
1962 1962 },
1963 1963 render : function (id_base) {
1964 1964 var p = this.paginator,
1965 1965 c = p.get('lastPageLinkClass'),
1966 1966 label = p.get('lastPageLinkLabel'),
1967 1967 last = p.getTotalPages(),
1968 1968 title = p.get('lastPageLinkTitle');
1969 1969
1970 1970 var _pos = this.getPos(p.getCurrentPage(), p.getTotalPages(), 5);
1971 1971 this.leftmost_page = _pos[0];
1972 1972 this.rightmost_page = _pos[2];
1973 1973
1974 1974 this.link = document.createElement('a');
1975 1975 this.span = document.createElement('span');
1976 1976 $(this.span).hide();
1977 1977
1978 1978 this.na = this.span.cloneNode(false);
1979 1979
1980 1980 setId(this.link, id_base + '-last-link');
1981 1981 this.link.href = '#';
1982 1982 this.link.className = c;
1983 1983 this.link.innerHTML = label;
1984 1984 this.link.title = title;
1985 1985 YUE.on(this.link,'click',this.onClick,this,true);
1986 1986
1987 1987 setId(this.span, id_base + '-last-span');
1988 1988 this.span.className = c;
1989 1989 this.span.innerHTML = label;
1990 1990
1991 1991 setId(this.na, id_base + '-last-na');
1992 1992
1993 1993 if (this.rightmost_page < p.getTotalPages()){
1994 1994 this.current = this.link;
1995 1995 }
1996 1996 else{
1997 1997 this.current = this.span;
1998 1998 }
1999 1999
2000 2000 this.current.innerHTML = p.getTotalPages();
2001 2001 return this.current;
2002 2002 },
2003 2003
2004 2004 update : function (e) {
2005 2005 var p = this.paginator;
2006 2006
2007 2007 var _pos = this.getPos(p.getCurrentPage(), p.getTotalPages(), 5);
2008 2008 this.leftmost_page = _pos[0];
2009 2009 this.rightmost_page = _pos[2];
2010 2010
2011 2011 if (e && e.prevValue === e.newValue) {
2012 2012 return;
2013 2013 }
2014 2014
2015 2015 var par = this.current ? this.current.parentNode : null,
2016 2016 after = this.link;
2017 2017 if (par) {
2018 2018
2019 2019 // only show the last page if the rightmost one is
2020 2020 // lower, so we don't have doubled entries at the end
2021 2021 if (!(this.rightmost_page < p.getTotalPages())){
2022 2022 after = this.span
2023 2023 }
2024 2024
2025 2025 if (this.current !== after) {
2026 2026 par.replaceChild(after,this.current);
2027 2027 this.current = after;
2028 2028 }
2029 2029 }
2030 2030 this.current.innerHTML = this.paginator.getTotalPages();
2031 2031
2032 2032 },
2033 2033 destroy : function () {
2034 2034 YUE.purgeElement(this.link);
2035 2035 this.current.parentNode.removeChild(this.current);
2036 2036 this.link = this.span = null;
2037 2037 },
2038 2038 onClick : function (e) {
2039 2039 YUE.stopEvent(e);
2040 2040 this.paginator.setPage(this.paginator.getTotalPages());
2041 2041 }
2042 2042 };
2043 2043
2044 2044 })();
2045 2045
2046 2046 var pagi = new YAHOO.widget.Paginator({
2047 2047 rowsPerPage: links_per_page,
2048 2048 alwaysVisible: false,
2049 2049 template : "{PreviousPageLink} {MyFirstPageLink} {PageLinks} {MyLastPageLink} {NextPageLink}",
2050 2050 pageLinks: 5,
2051 2051 containerClass: 'pagination-wh',
2052 2052 currentPageClass: 'pager_curpage',
2053 2053 pageLinkClass: 'pager_link',
2054 2054 nextPageLinkLabel: '&gt;',
2055 2055 previousPageLinkLabel: '&lt;',
2056 2056 containers:containers
2057 2057 })
2058 2058
2059 2059 return pagi
2060 2060 }
2061 2061
2062 2062 var YUI_datatable = function(data, fields, columns, countnode, sortkey, rows){
2063 2063 var myDataSource = new YAHOO.util.DataSource(data);
2064 2064 myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
2065 2065 myDataSource.responseSchema = {
2066 2066 resultsList: "records",
2067 2067 fields: fields,
2068 2068 };
2069 2069 myDataSource.doBeforeCallback = function(req, raw, res, cb) {
2070 2070 // This is the filter function
2071 2071 var data = res.results || [],
2072 2072 filtered = [],
2073 2073 i, l;
2074 2074
2075 2075 if (req) {
2076 2076 req = req.toLowerCase();
2077 2077 for (i = 0; i<data.length; i++) {
2078 2078 var pos = data[i].raw_name.toLowerCase().indexOf(req)
2079 2079 if (pos != -1) {
2080 2080 filtered.push(data[i]);
2081 2081 }
2082 2082 }
2083 2083 res.results = filtered;
2084 2084 }
2085 2085 $(countnode).html(res.results.length);
2086 2086 return res;
2087 2087 }
2088 2088
2089 2089 var myDataTable = new YAHOO.widget.DataTable("datatable_list_wrap", columns, myDataSource, {
2090 2090 sortedBy: {key:sortkey, dir:"asc"},
2091 2091 paginator: YUI_paginator(rows !== undefined && rows ? rows : 25, ['user-paginator']),
2092 2092 MSG_SORTASC: _TM['MSG_SORTASC'],
2093 2093 MSG_SORTDESC: _TM['MSG_SORTDESC'],
2094 2094 MSG_EMPTY: _TM['MSG_EMPTY'],
2095 2095 MSG_ERROR: _TM['MSG_ERROR'],
2096 2096 MSG_LOADING: _TM['MSG_LOADING'],
2097 2097 });
2098 2098 myDataTable.subscribe('postRenderEvent',function(oArgs) {
2099 2099 tooltip_activate();
2100 2100 quick_repo_menu();
2101 2101 });
2102 2102
2103 2103 var filterTimeout = null;
2104 2104 var $q_filter = $('#q_filter');
2105 2105
2106 2106 updateFilter = function () {
2107 2107 // Reset timeout
2108 2108 filterTimeout = null;
2109 2109
2110 2110 // Reset sort
2111 2111 var state = myDataTable.getState();
2112 2112 state.sortedBy = {key:sortkey, dir:YAHOO.widget.DataTable.CLASS_ASC};
2113 2113
2114 2114 // Get filtered data
2115 2115 myDataSource.sendRequest($q_filter.val(), {
2116 2116 success : myDataTable.onDataReturnInitializeTable,
2117 2117 failure : myDataTable.onDataReturnInitializeTable,
2118 2118 scope : myDataTable,
2119 2119 argument: state});
2120 2120 };
2121 2121
2122 2122 $q_filter.click(function(){
2123 2123 if(!$q_filter.hasClass('loaded')){
2124 2124 //TODO: load here full list later to do search within groups
2125 2125 $q_filter.addClass('loaded');
2126 2126 }
2127 2127 });
2128 2128
2129 2129 $q_filter.keyup(function (e) {
2130 2130 clearTimeout(filterTimeout);
2131 2131 filterTimeout = setTimeout(updateFilter, 600);
2132 2132 });
2133 2133 }
2134 2134
2135 2135 // global hooks after DOM is loaded
2136 2136
2137 2137 $(document).ready(function(){
2138 2138 $('.diff-collapse-button').click(function(e) {
2139 2139 var $button = $(e.currentTarget);
2140 2140 var $target = $('#' + $button.attr('target'));
2141 2141 if($target.hasClass('hidden')){
2142 2142 $target.removeClass('hidden');
2143 2143 $button.html("&uarr; {0} &uarr;".format(_TM['Collapse diff']));
2144 2144 }
2145 2145 else if(!$target.hasClass('hidden')){
2146 2146 $target.addClass('hidden');
2147 2147 $button.html("&darr; {0} &darr;".format(_TM['Expand diff']));
2148 2148 }
2149 2149 });
2150 2150 });
General Comments 0
You need to be logged in to leave comments. Login now