##// END OF EJS Templates
Add cache to ypjax calls to overcome chrome caching issues of requests
marcink -
r2694:78694f9a beta
parent child Browse files
Show More
@@ -1,1712 +1,1713 b''
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 var str_repeat = function(i, m) {
11 11 for (var o = []; m > 0; o[--m] = i);
12 12 return o.join('');
13 13 };
14 14
15 15 /**
16 16 * INJECT .format function into String
17 17 * Usage: "My name is {0} {1}".format("Johny","Bravo")
18 18 * Return "My name is Johny Bravo"
19 19 * Inspired by https://gist.github.com/1049426
20 20 */
21 21 String.prototype.format = function() {
22 22
23 23 function format() {
24 24 var str = this;
25 25 var len = arguments.length+1;
26 26 var safe = undefined;
27 27 var arg = undefined;
28 28
29 29 // For each {0} {1} {n...} replace with the argument in that position. If
30 30 // the argument is an object or an array it will be stringified to JSON.
31 31 for (var i=0; i < len; arg = arguments[i++]) {
32 32 safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
33 33 str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
34 34 }
35 35 return str;
36 36 }
37 37
38 38 // Save a reference of what may already exist under the property native.
39 39 // Allows for doing something like: if("".format.native) { /* use native */ }
40 40 format.native = String.prototype.format;
41 41
42 42 // Replace the prototype property
43 43 return format;
44 44
45 45 }();
46 46
47 47 String.prototype.strip = function(char) {
48 48 if(char === undefined){
49 49 char = '\\s';
50 50 }
51 51 return this.replace(new RegExp('^'+char+'+|'+char+'+$','g'), '');
52 52 }
53 53 String.prototype.lstrip = function(char) {
54 54 if(char === undefined){
55 55 char = '\\s';
56 56 }
57 57 return this.replace(new RegExp('^'+char+'+'),'');
58 58 }
59 59 String.prototype.rstrip = function(char) {
60 60 if(char === undefined){
61 61 char = '\\s';
62 62 }
63 63 return this.replace(new RegExp(''+char+'+$'),'');
64 64 }
65 65
66 66
67 67 if(!Array.prototype.indexOf) {
68 68 Array.prototype.indexOf = function(needle) {
69 69 for(var i = 0; i < this.length; i++) {
70 70 if(this[i] === needle) {
71 71 return i;
72 72 }
73 73 }
74 74 return -1;
75 75 };
76 76 }
77 77
78 78 /**
79 79 * SmartColorGenerator
80 80 *
81 81 *usage::
82 82 * var CG = new ColorGenerator();
83 83 * var col = CG.getColor(key); //returns array of RGB
84 84 * 'rgb({0})'.format(col.join(',')
85 85 *
86 86 * @returns {ColorGenerator}
87 87 */
88 88 var ColorGenerator = function(){
89 89 this.GOLDEN_RATIO = 0.618033988749895;
90 90 this.CURRENT_RATIO = 0.22717784590367374 // this can be random
91 91 this.HSV_1 = 0.75;//saturation
92 92 this.HSV_2 = 0.95;
93 93 this.color;
94 94 this.cacheColorMap = {};
95 95 };
96 96
97 97 ColorGenerator.prototype = {
98 98 getColor:function(key){
99 99 if(this.cacheColorMap[key] !== undefined){
100 100 return this.cacheColorMap[key];
101 101 }
102 102 else{
103 103 this.cacheColorMap[key] = this.generateColor();
104 104 return this.cacheColorMap[key];
105 105 }
106 106 },
107 107 _hsvToRgb:function(h,s,v){
108 108 if (s == 0.0)
109 109 return [v, v, v];
110 110 i = parseInt(h * 6.0)
111 111 f = (h * 6.0) - i
112 112 p = v * (1.0 - s)
113 113 q = v * (1.0 - s * f)
114 114 t = v * (1.0 - s * (1.0 - f))
115 115 i = i % 6
116 116 if (i == 0)
117 117 return [v, t, p]
118 118 if (i == 1)
119 119 return [q, v, p]
120 120 if (i == 2)
121 121 return [p, v, t]
122 122 if (i == 3)
123 123 return [p, q, v]
124 124 if (i == 4)
125 125 return [t, p, v]
126 126 if (i == 5)
127 127 return [v, p, q]
128 128 },
129 129 generateColor:function(){
130 130 this.CURRENT_RATIO = this.CURRENT_RATIO+this.GOLDEN_RATIO;
131 131 this.CURRENT_RATIO = this.CURRENT_RATIO %= 1;
132 132 HSV_tuple = [this.CURRENT_RATIO, this.HSV_1, this.HSV_2]
133 133 RGB_tuple = this._hsvToRgb(HSV_tuple[0],HSV_tuple[1],HSV_tuple[2]);
134 134 function toRgb(v){
135 135 return ""+parseInt(v*256)
136 136 }
137 137 return [toRgb(RGB_tuple[0]),toRgb(RGB_tuple[1]),toRgb(RGB_tuple[2])];
138 138
139 139 }
140 140 }
141 141
142 142
143 143
144 144
145 145
146 146 /**
147 147 * GLOBAL YUI Shortcuts
148 148 */
149 149 var YUC = YAHOO.util.Connect;
150 150 var YUD = YAHOO.util.Dom;
151 151 var YUE = YAHOO.util.Event;
152 152 var YUQ = YAHOO.util.Selector.query;
153 153
154 154 // defines if push state is enabled for this browser ?
155 155 var push_state_enabled = Boolean(
156 156 window.history && window.history.pushState && window.history.replaceState
157 157 && !( /* disable for versions of iOS before version 4.3 (8F190) */
158 158 (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(navigator.userAgent)
159 159 /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
160 160 || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(navigator.userAgent)
161 161 )
162 162 );
163 163
164 164 var _run_callbacks = function(callbacks){
165 165 if (callbacks !== undefined){
166 166 var _l = callbacks.length;
167 167 for (var i=0;i<_l;i++){
168 168 var func = callbacks[i];
169 169 if(typeof(func)=='function'){
170 170 try{
171 171 func();
172 172 }catch (err){};
173 173 }
174 174 }
175 175 }
176 176 }
177 177
178 178 /**
179 179 * Partial Ajax Implementation
180 180 *
181 181 * @param url: defines url to make partial request
182 182 * @param container: defines id of container to input partial result
183 183 * @param s_call: success callback function that takes o as arg
184 184 * o.tId
185 185 * o.status
186 186 * o.statusText
187 187 * o.getResponseHeader[ ]
188 188 * o.getAllResponseHeaders
189 189 * o.responseText
190 190 * o.responseXML
191 191 * o.argument
192 192 * @param f_call: failure callback
193 193 * @param args arguments
194 194 */
195 195 function ypjax(url,container,s_call,f_call,args){
196 196 var method='GET';
197 197 if(args===undefined){
198 198 args=null;
199 199 }
200 200
201 201 // Set special header for partial ajax == HTTP_X_PARTIAL_XHR
202 202 YUC.initHeader('X-PARTIAL-XHR',true);
203 203
204 204 // wrapper of passed callback
205 205 var s_wrapper = (function(o){
206 206 return function(o){
207 207 YUD.get(container).innerHTML=o.responseText;
208 208 YUD.setStyle(container,'opacity','1.0');
209 209 //execute the given original callback
210 210 if (s_call !== undefined){
211 211 s_call(o);
212 212 }
213 213 }
214 214 })()
215 215 YUD.setStyle(container,'opacity','0.3');
216 216 YUC.asyncRequest(method,url,{
217 217 success:s_wrapper,
218 218 failure:function(o){
219 219 console.log(o);
220 220 YUD.get(container).innerHTML='<span class="error_red">ERROR: {0}</span>'.format(o.status);
221 221 YUD.setStyle(container,'opacity','1.0');
222 }
222 },
223 cache:false
223 224 },args);
224 225
225 226 };
226 227
227 228 var ajaxPOST = function(url,postData,success) {
228 229 // Set special header for ajax == HTTP_X_PARTIAL_XHR
229 230 YUC.initHeader('X-PARTIAL-XHR',true);
230 231
231 232 var toQueryString = function(o) {
232 233 if(typeof o !== 'object') {
233 234 return false;
234 235 }
235 236 var _p, _qs = [];
236 237 for(_p in o) {
237 238 _qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p]));
238 239 }
239 240 return _qs.join('&');
240 241 };
241 242
242 243 var sUrl = url;
243 244 var callback = {
244 245 success: success,
245 246 failure: function (o) {
246 247 alert("error");
247 248 },
248 249 };
249 250 var postData = toQueryString(postData);
250 251 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
251 252 return request;
252 253 };
253 254
254 255
255 256 /**
256 257 * tooltip activate
257 258 */
258 259 var tooltip_activate = function(){
259 260 function toolTipsId(){
260 261 var ids = [];
261 262 var tts = YUQ('.tooltip');
262 263 for (var i = 0; i < tts.length; i++) {
263 264 // if element doesn't not have and id
264 265 // autogenerate one for tooltip
265 266 if (!tts[i].id){
266 267 tts[i].id='tt'+((i*100)+tts.length);
267 268 }
268 269 ids.push(tts[i].id);
269 270 }
270 271 return ids
271 272 };
272 273 var myToolTips = new YAHOO.widget.Tooltip("tooltip", {
273 274 context: [[toolTipsId()],"tl","bl",null,[0,5]],
274 275 monitorresize:false,
275 276 xyoffset :[0,0],
276 277 autodismissdelay:300000,
277 278 hidedelay:5,
278 279 showdelay:20,
279 280 });
280 281 };
281 282
282 283 /**
283 284 * show more
284 285 */
285 286 var show_more_event = function(){
286 287 YUE.on(YUD.getElementsByClassName('show_more'),'click',function(e){
287 288 var el = e.target;
288 289 YUD.setStyle(YUD.get(el.id.substring(1)),'display','');
289 290 YUD.setStyle(el.parentNode,'display','none');
290 291 });
291 292 };
292 293
293 294
294 295 /**
295 296 * Quick filter widget
296 297 *
297 298 * @param target: filter input target
298 299 * @param nodes: list of nodes in html we want to filter.
299 300 * @param display_element function that takes current node from nodes and
300 301 * does hide or show based on the node
301 302 *
302 303 */
303 304 var q_filter = function(target,nodes,display_element){
304 305
305 306 var nodes = nodes;
306 307 var q_filter_field = YUD.get(target);
307 308 var F = YAHOO.namespace(target);
308 309
309 310 YUE.on(q_filter_field,'click',function(){
310 311 q_filter_field.value = '';
311 312 });
312 313
313 314 YUE.on(q_filter_field,'keyup',function(e){
314 315 clearTimeout(F.filterTimeout);
315 316 F.filterTimeout = setTimeout(F.updateFilter,600);
316 317 });
317 318
318 319 F.filterTimeout = null;
319 320
320 321 var show_node = function(node){
321 322 YUD.setStyle(node,'display','')
322 323 }
323 324 var hide_node = function(node){
324 325 YUD.setStyle(node,'display','none');
325 326 }
326 327
327 328 F.updateFilter = function() {
328 329 // Reset timeout
329 330 F.filterTimeout = null;
330 331
331 332 var obsolete = [];
332 333
333 334 var req = q_filter_field.value.toLowerCase();
334 335
335 336 var l = nodes.length;
336 337 var i;
337 338 var showing = 0;
338 339
339 340 for (i=0;i<l;i++ ){
340 341 var n = nodes[i];
341 342 var target_element = display_element(n)
342 343 if(req && n.innerHTML.toLowerCase().indexOf(req) == -1){
343 344 hide_node(target_element);
344 345 }
345 346 else{
346 347 show_node(target_element);
347 348 showing+=1;
348 349 }
349 350 }
350 351
351 352 // if repo_count is set update the number
352 353 var cnt = YUD.get('repo_count');
353 354 if(cnt){
354 355 YUD.get('repo_count').innerHTML = showing;
355 356 }
356 357
357 358 }
358 359 };
359 360
360 361 var tableTr = function(cls,body){
361 362 var tr = document.createElement('tr');
362 363 YUD.addClass(tr, cls);
363 364
364 365
365 366 var cont = new YAHOO.util.Element(body);
366 367 var comment_id = fromHTML(body).children[0].id.split('comment-')[1];
367 368 tr.id = 'comment-tr-{0}'.format(comment_id);
368 369 tr.innerHTML = '<td class="lineno-inline new-inline"></td>'+
369 370 '<td class="lineno-inline old-inline"></td>'+
370 371 '<td>{0}</td>'.format(body);
371 372 return tr;
372 373 };
373 374
374 375 /** comments **/
375 376 var removeInlineForm = function(form) {
376 377 form.parentNode.removeChild(form);
377 378 };
378 379
379 380 var createInlineForm = function(parent_tr, f_path, line) {
380 381 var tmpl = YUD.get('comment-inline-form-template').innerHTML;
381 382 tmpl = tmpl.format(f_path, line);
382 383 var form = tableTr('comment-form-inline',tmpl)
383 384
384 385 // create event for hide button
385 386 form = new YAHOO.util.Element(form);
386 387 var form_hide_button = new YAHOO.util.Element(form.getElementsByClassName('hide-inline-form')[0]);
387 388 form_hide_button.on('click', function(e) {
388 389 var newtr = e.currentTarget.parentNode.parentNode.parentNode.parentNode.parentNode;
389 390 if(YUD.hasClass(newtr.nextElementSibling,'inline-comments-button')){
390 391 YUD.setStyle(newtr.nextElementSibling,'display','');
391 392 }
392 393 removeInlineForm(newtr);
393 394 YUD.removeClass(parent_tr, 'form-open');
394 395
395 396 });
396 397
397 398 return form
398 399 };
399 400
400 401 /**
401 402 * Inject inline comment for on given TR this tr should be always an .line
402 403 * tr containing the line. Code will detect comment, and always put the comment
403 404 * block at the very bottom
404 405 */
405 406 var injectInlineForm = function(tr){
406 407 if(!YUD.hasClass(tr, 'line')){
407 408 return
408 409 }
409 410 var submit_url = AJAX_COMMENT_URL;
410 411 var _td = tr.getElementsByClassName('code')[0];
411 412 if(YUD.hasClass(tr,'form-open') || YUD.hasClass(tr,'context') || YUD.hasClass(_td,'no-comment')){
412 413 return
413 414 }
414 415 YUD.addClass(tr,'form-open');
415 416 var node = tr.parentNode.parentNode.parentNode.getElementsByClassName('full_f_path')[0];
416 417 var f_path = YUD.getAttribute(node,'path');
417 418 var lineno = getLineNo(tr);
418 419 var form = createInlineForm(tr, f_path, lineno, submit_url);
419 420
420 421 var parent = tr;
421 422 while (1){
422 423 var n = parent.nextElementSibling;
423 424 // next element are comments !
424 425 if(YUD.hasClass(n,'inline-comments')){
425 426 parent = n;
426 427 }
427 428 else{
428 429 break;
429 430 }
430 431 }
431 432 YUD.insertAfter(form,parent);
432 433
433 434 var f = YUD.get(form);
434 435
435 436 var overlay = f.getElementsByClassName('overlay')[0];
436 437 var _form = f.getElementsByClassName('inline-form')[0];
437 438
438 439 form.on('submit',function(e){
439 440 YUE.preventDefault(e);
440 441
441 442 //ajax submit
442 443 var text = YUD.get('text_'+lineno).value;
443 444 var postData = {
444 445 'text':text,
445 446 'f_path':f_path,
446 447 'line':lineno
447 448 };
448 449
449 450 if(lineno === undefined){
450 451 alert('missing line !');
451 452 return
452 453 }
453 454 if(f_path === undefined){
454 455 alert('missing file path !');
455 456 return
456 457 }
457 458
458 459 if(text == ""){
459 460 return
460 461 }
461 462
462 463 var success = function(o){
463 464 YUD.removeClass(tr, 'form-open');
464 465 removeInlineForm(f);
465 466 var json_data = JSON.parse(o.responseText);
466 467 renderInlineComment(json_data);
467 468 };
468 469
469 470 if (YUD.hasClass(overlay,'overlay')){
470 471 var w = _form.offsetWidth;
471 472 var h = _form.offsetHeight;
472 473 YUD.setStyle(overlay,'width',w+'px');
473 474 YUD.setStyle(overlay,'height',h+'px');
474 475 }
475 476 YUD.addClass(overlay, 'submitting');
476 477
477 478 ajaxPOST(submit_url, postData, success);
478 479 });
479 480
480 481 setTimeout(function(){
481 482 // callbacks
482 483 tooltip_activate();
483 484 MentionsAutoComplete('text_'+lineno, 'mentions_container_'+lineno,
484 485 _USERS_AC_DATA, _GROUPS_AC_DATA);
485 486 YUD.get('text_'+lineno).focus();
486 487 },10)
487 488 };
488 489
489 490 var deleteComment = function(comment_id){
490 491 var url = AJAX_COMMENT_DELETE_URL.replace('__COMMENT_ID__',comment_id);
491 492 var postData = {'_method':'delete'};
492 493 var success = function(o){
493 494 var n = YUD.get('comment-tr-'+comment_id);
494 495 var root = n.previousElementSibling.previousElementSibling;
495 496 n.parentNode.removeChild(n);
496 497
497 498 // scann nodes, and attach add button to last one
498 499 placeAddButton(root);
499 500 }
500 501 ajaxPOST(url,postData,success);
501 502 }
502 503
503 504 var updateReviewers = function(reviewers_ids){
504 505 var url = AJAX_UPDATE_PULLREQUEST;
505 506 var postData = {'_method':'put',
506 507 'reviewers_ids': reviewers_ids};
507 508 var success = function(o){
508 509 window.location.reload();
509 510 }
510 511 ajaxPOST(url,postData,success);
511 512 }
512 513
513 514 var createInlineAddButton = function(tr){
514 515
515 516 var label = TRANSLATION_MAP['add another comment'];
516 517
517 518 var html_el = document.createElement('div');
518 519 YUD.addClass(html_el, 'add-comment');
519 520 html_el.innerHTML = '<span class="ui-btn">{0}</span>'.format(label);
520 521
521 522 var add = new YAHOO.util.Element(html_el);
522 523 add.on('click', function(e) {
523 524 injectInlineForm(tr);
524 525 });
525 526 return add;
526 527 };
527 528
528 529 var getLineNo = function(tr) {
529 530 var line;
530 531 var o = tr.children[0].id.split('_');
531 532 var n = tr.children[1].id.split('_');
532 533
533 534 if (n.length >= 2) {
534 535 line = n[n.length-1];
535 536 } else if (o.length >= 2) {
536 537 line = o[o.length-1];
537 538 }
538 539
539 540 return line
540 541 };
541 542
542 543 var placeAddButton = function(target_tr){
543 544 if(!target_tr){
544 545 return
545 546 }
546 547 var last_node = target_tr;
547 548 //scann
548 549 while (1){
549 550 var n = last_node.nextElementSibling;
550 551 // next element are comments !
551 552 if(YUD.hasClass(n,'inline-comments')){
552 553 last_node = n;
553 554 //also remove the comment button from previos
554 555 var comment_add_buttons = last_node.getElementsByClassName('add-comment');
555 556 for(var i=0;i<comment_add_buttons.length;i++){
556 557 var b = comment_add_buttons[i];
557 558 b.parentNode.removeChild(b);
558 559 }
559 560 }
560 561 else{
561 562 break;
562 563 }
563 564 }
564 565
565 566 var add = createInlineAddButton(target_tr);
566 567 // get the comment div
567 568 var comment_block = last_node.getElementsByClassName('comment')[0];
568 569 // attach add button
569 570 YUD.insertAfter(add,comment_block);
570 571 }
571 572
572 573 /**
573 574 * Places the inline comment into the changeset block in proper line position
574 575 */
575 576 var placeInline = function(target_container,lineno,html){
576 577 var lineid = "{0}_{1}".format(target_container,lineno);
577 578 var target_line = YUD.get(lineid);
578 579 var comment = new YAHOO.util.Element(tableTr('inline-comments',html))
579 580
580 581 // check if there are comments already !
581 582 var parent = target_line.parentNode;
582 583 var root_parent = parent;
583 584 while (1){
584 585 var n = parent.nextElementSibling;
585 586 // next element are comments !
586 587 if(YUD.hasClass(n,'inline-comments')){
587 588 parent = n;
588 589 }
589 590 else{
590 591 break;
591 592 }
592 593 }
593 594 // put in the comment at the bottom
594 595 YUD.insertAfter(comment,parent);
595 596
596 597 // scann nodes, and attach add button to last one
597 598 placeAddButton(root_parent);
598 599
599 600 return target_line;
600 601 }
601 602
602 603 /**
603 604 * make a single inline comment and place it inside
604 605 */
605 606 var renderInlineComment = function(json_data){
606 607 try{
607 608 var html = json_data['rendered_text'];
608 609 var lineno = json_data['line_no'];
609 610 var target_id = json_data['target_id'];
610 611 placeInline(target_id, lineno, html);
611 612
612 613 }catch(e){
613 614 console.log(e);
614 615 }
615 616 }
616 617
617 618 /**
618 619 * Iterates over all the inlines, and places them inside proper blocks of data
619 620 */
620 621 var renderInlineComments = function(file_comments){
621 622 for (f in file_comments){
622 623 // holding all comments for a FILE
623 624 var box = file_comments[f];
624 625
625 626 var target_id = YUD.getAttribute(box,'target_id');
626 627 // actually comments with line numbers
627 628 var comments = box.children;
628 629 for(var i=0; i<comments.length; i++){
629 630 var data = {
630 631 'rendered_text': comments[i].outerHTML,
631 632 'line_no': YUD.getAttribute(comments[i],'line'),
632 633 'target_id': target_id
633 634 }
634 635 renderInlineComment(data);
635 636 }
636 637 }
637 638 }
638 639
639 640 var removeReviewer = function(reviewer_id){
640 641 var el = YUD.get('reviewer_{0}'.format(reviewer_id));
641 642 if (el.parentNode !== undefined){
642 643 el.parentNode.removeChild(el);
643 644 }
644 645 }
645 646
646 647 var fileBrowserListeners = function(current_url, node_list_url, url_base){
647 648
648 649 var current_url_branch = +"?branch=__BRANCH__";
649 650 var url = url_base;
650 651 var node_url = node_list_url;
651 652
652 653 YUE.on('stay_at_branch','click',function(e){
653 654 if(e.target.checked){
654 655 var uri = current_url_branch;
655 656 uri = uri.replace('__BRANCH__',e.target.value);
656 657 window.location = uri;
657 658 }
658 659 else{
659 660 window.location = current_url;
660 661 }
661 662 })
662 663
663 664 var n_filter = YUD.get('node_filter');
664 665 var F = YAHOO.namespace('node_filter');
665 666
666 667 F.filterTimeout = null;
667 668 var nodes = null;
668 669
669 670 F.initFilter = function(){
670 671 YUD.setStyle('node_filter_box_loading','display','');
671 672 YUD.setStyle('search_activate_id','display','none');
672 673 YUD.setStyle('add_node_id','display','none');
673 674 YUC.initHeader('X-PARTIAL-XHR',true);
674 675 YUC.asyncRequest('GET',url,{
675 676 success:function(o){
676 677 nodes = JSON.parse(o.responseText).nodes;
677 678 YUD.setStyle('node_filter_box_loading','display','none');
678 679 YUD.setStyle('node_filter_box','display','');
679 680 n_filter.focus();
680 681 if(YUD.hasClass(n_filter,'init')){
681 682 n_filter.value = '';
682 683 YUD.removeClass(n_filter,'init');
683 684 }
684 685 },
685 686 failure:function(o){
686 687 console.log('failed to load');
687 688 }
688 689 },null);
689 690 }
690 691
691 692 F.updateFilter = function(e) {
692 693
693 694 return function(){
694 695 // Reset timeout
695 696 F.filterTimeout = null;
696 697 var query = e.target.value.toLowerCase();
697 698 var match = [];
698 699 var matches = 0;
699 700 var matches_max = 20;
700 701 if (query != ""){
701 702 for(var i=0;i<nodes.length;i++){
702 703
703 704 var pos = nodes[i].name.toLowerCase().indexOf(query)
704 705 if(query && pos != -1){
705 706
706 707 matches++
707 708 //show only certain amount to not kill browser
708 709 if (matches > matches_max){
709 710 break;
710 711 }
711 712
712 713 var n = nodes[i].name;
713 714 var t = nodes[i].type;
714 715 var n_hl = n.substring(0,pos)
715 716 +"<b>{0}</b>".format(n.substring(pos,pos+query.length))
716 717 +n.substring(pos+query.length)
717 718 node_url = node_url.replace('__FPATH__',n);
718 719 match.push('<tr><td><a class="browser-{0}" href="{1}">{2}</a></td><td colspan="5"></td></tr>'.format(t,node_url,n_hl));
719 720 }
720 721 if(match.length >= matches_max){
721 722 match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_TM['search truncated']));
722 723 }
723 724 }
724 725 }
725 726 if(query != ""){
726 727 YUD.setStyle('tbody','display','none');
727 728 YUD.setStyle('tbody_filtered','display','');
728 729
729 730 if (match.length==0){
730 731 match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_TM['no matching files']));
731 732 }
732 733
733 734 YUD.get('tbody_filtered').innerHTML = match.join("");
734 735 }
735 736 else{
736 737 YUD.setStyle('tbody','display','');
737 738 YUD.setStyle('tbody_filtered','display','none');
738 739 }
739 740
740 741 }
741 742 };
742 743
743 744 YUE.on(YUD.get('filter_activate'),'click',function(){
744 745 F.initFilter();
745 746 })
746 747 YUE.on(n_filter,'click',function(){
747 748 if(YUD.hasClass(n_filter,'init')){
748 749 n_filter.value = '';
749 750 YUD.removeClass(n_filter,'init');
750 751 }
751 752 });
752 753 YUE.on(n_filter,'keyup',function(e){
753 754 clearTimeout(F.filterTimeout);
754 755 F.filterTimeout = setTimeout(F.updateFilter(e),600);
755 756 });
756 757 };
757 758
758 759
759 760 var initCodeMirror = function(textAreadId,resetUrl){
760 761 var myCodeMirror = CodeMirror.fromTextArea(YUD.get(textAreadId),{
761 762 mode: "null",
762 763 lineNumbers:true
763 764 });
764 765 YUE.on('reset','click',function(e){
765 766 window.location=resetUrl
766 767 });
767 768
768 769 YUE.on('file_enable','click',function(){
769 770 YUD.setStyle('editor_container','display','');
770 771 YUD.setStyle('upload_file_container','display','none');
771 772 YUD.setStyle('filename_container','display','');
772 773 });
773 774
774 775 YUE.on('upload_file_enable','click',function(){
775 776 YUD.setStyle('editor_container','display','none');
776 777 YUD.setStyle('upload_file_container','display','');
777 778 YUD.setStyle('filename_container','display','none');
778 779 });
779 780 };
780 781
781 782
782 783
783 784 var getIdentNode = function(n){
784 785 //iterate thru nodes untill matched interesting node !
785 786
786 787 if (typeof n == 'undefined'){
787 788 return -1
788 789 }
789 790
790 791 if(typeof n.id != "undefined" && n.id.match('L[0-9]+')){
791 792 return n
792 793 }
793 794 else{
794 795 return getIdentNode(n.parentNode);
795 796 }
796 797 };
797 798
798 799 var getSelectionLink = function(selection_link_label) {
799 800 return function(){
800 801 //get selection from start/to nodes
801 802 if (typeof window.getSelection != "undefined") {
802 803 s = window.getSelection();
803 804
804 805 from = getIdentNode(s.anchorNode);
805 806 till = getIdentNode(s.focusNode);
806 807
807 808 f_int = parseInt(from.id.replace('L',''));
808 809 t_int = parseInt(till.id.replace('L',''));
809 810
810 811 if (f_int > t_int){
811 812 //highlight from bottom
812 813 offset = -35;
813 814 ranges = [t_int,f_int];
814 815
815 816 }
816 817 else{
817 818 //highligth from top
818 819 offset = 35;
819 820 ranges = [f_int,t_int];
820 821 }
821 822
822 823 if (ranges[0] != ranges[1]){
823 824 if(YUD.get('linktt') == null){
824 825 hl_div = document.createElement('div');
825 826 hl_div.id = 'linktt';
826 827 }
827 828 anchor = '#L'+ranges[0]+'-'+ranges[1];
828 829 hl_div.innerHTML = '';
829 830 l = document.createElement('a');
830 831 l.href = location.href.substring(0,location.href.indexOf('#'))+anchor;
831 832 l.innerHTML = selection_link_label;
832 833 hl_div.appendChild(l);
833 834
834 835 YUD.get('body').appendChild(hl_div);
835 836
836 837 xy = YUD.getXY(till.id);
837 838
838 839 YUD.addClass('linktt','yui-tt');
839 840 YUD.setStyle('linktt','top',xy[1]+offset+'px');
840 841 YUD.setStyle('linktt','left',xy[0]+'px');
841 842 YUD.setStyle('linktt','visibility','visible');
842 843 }
843 844 else{
844 845 YUD.setStyle('linktt','visibility','hidden');
845 846 }
846 847 }
847 848 }
848 849 };
849 850
850 851 var deleteNotification = function(url, notification_id,callbacks){
851 852 var callback = {
852 853 success:function(o){
853 854 var obj = YUD.get(String("notification_"+notification_id));
854 855 if(obj.parentNode !== undefined){
855 856 obj.parentNode.removeChild(obj);
856 857 }
857 858 _run_callbacks(callbacks);
858 859 },
859 860 failure:function(o){
860 861 alert("error");
861 862 },
862 863 };
863 864 var postData = '_method=delete';
864 865 var sUrl = url.replace('__NOTIFICATION_ID__',notification_id);
865 866 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,
866 867 callback, postData);
867 868 };
868 869
869 870 var readNotification = function(url, notification_id,callbacks){
870 871 var callback = {
871 872 success:function(o){
872 873 var obj = YUD.get(String("notification_"+notification_id));
873 874 YUD.removeClass(obj, 'unread');
874 875 var r_button = obj.children[0].getElementsByClassName('read-notification')[0]
875 876
876 877 if(r_button.parentNode !== undefined){
877 878 r_button.parentNode.removeChild(r_button);
878 879 }
879 880 _run_callbacks(callbacks);
880 881 },
881 882 failure:function(o){
882 883 alert("error");
883 884 },
884 885 };
885 886 var postData = '_method=put';
886 887 var sUrl = url.replace('__NOTIFICATION_ID__',notification_id);
887 888 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,
888 889 callback, postData);
889 890 };
890 891
891 892 /** MEMBERS AUTOCOMPLETE WIDGET **/
892 893
893 894 var MembersAutoComplete = function (users_list, groups_list) {
894 895 var myUsers = users_list;
895 896 var myGroups = groups_list;
896 897
897 898 // Define a custom search function for the DataSource of users
898 899 var matchUsers = function (sQuery) {
899 900 // Case insensitive matching
900 901 var query = sQuery.toLowerCase();
901 902 var i = 0;
902 903 var l = myUsers.length;
903 904 var matches = [];
904 905
905 906 // Match against each name of each contact
906 907 for (; i < l; i++) {
907 908 contact = myUsers[i];
908 909 if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
909 910 ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
910 911 ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
911 912 matches[matches.length] = contact;
912 913 }
913 914 }
914 915 return matches;
915 916 };
916 917
917 918 // Define a custom search function for the DataSource of usersGroups
918 919 var matchGroups = function (sQuery) {
919 920 // Case insensitive matching
920 921 var query = sQuery.toLowerCase();
921 922 var i = 0;
922 923 var l = myGroups.length;
923 924 var matches = [];
924 925
925 926 // Match against each name of each contact
926 927 for (; i < l; i++) {
927 928 matched_group = myGroups[i];
928 929 if (matched_group.grname.toLowerCase().indexOf(query) > -1) {
929 930 matches[matches.length] = matched_group;
930 931 }
931 932 }
932 933 return matches;
933 934 };
934 935
935 936 //match all
936 937 var matchAll = function (sQuery) {
937 938 u = matchUsers(sQuery);
938 939 g = matchGroups(sQuery);
939 940 return u.concat(g);
940 941 };
941 942
942 943 // DataScheme for members
943 944 var memberDS = new YAHOO.util.FunctionDataSource(matchAll);
944 945 memberDS.responseSchema = {
945 946 fields: ["id", "fname", "lname", "nname", "grname", "grmembers", "gravatar_lnk"]
946 947 };
947 948
948 949 // DataScheme for owner
949 950 var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
950 951 ownerDS.responseSchema = {
951 952 fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
952 953 };
953 954
954 955 // Instantiate AutoComplete for perms
955 956 var membersAC = new YAHOO.widget.AutoComplete("perm_new_member_name", "perm_container", memberDS);
956 957 membersAC.useShadow = false;
957 958 membersAC.resultTypeList = false;
958 959 membersAC.animVert = false;
959 960 membersAC.animHoriz = false;
960 961 membersAC.animSpeed = 0.1;
961 962
962 963 // Instantiate AutoComplete for owner
963 964 var ownerAC = new YAHOO.widget.AutoComplete("user", "owner_container", ownerDS);
964 965 ownerAC.useShadow = false;
965 966 ownerAC.resultTypeList = false;
966 967 ownerAC.animVert = false;
967 968 ownerAC.animHoriz = false;
968 969 ownerAC.animSpeed = 0.1;
969 970
970 971 // Helper highlight function for the formatter
971 972 var highlightMatch = function (full, snippet, matchindex) {
972 973 return full.substring(0, matchindex)
973 974 + "<span class='match'>"
974 975 + full.substr(matchindex, snippet.length)
975 976 + "</span>" + full.substring(matchindex + snippet.length);
976 977 };
977 978
978 979 // Custom formatter to highlight the matching letters
979 980 var custom_formatter = function (oResultData, sQuery, sResultMatch) {
980 981 var query = sQuery.toLowerCase();
981 982 var _gravatar = function(res, em, group){
982 983 if (group !== undefined){
983 984 em = '/images/icons/group.png'
984 985 }
985 986 tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
986 987 return tmpl.format(em,res)
987 988 }
988 989 // group
989 990 if (oResultData.grname != undefined) {
990 991 var grname = oResultData.grname;
991 992 var grmembers = oResultData.grmembers;
992 993 var grnameMatchIndex = grname.toLowerCase().indexOf(query);
993 994 var grprefix = "{0}: ".format(_TM['Group']);
994 995 var grsuffix = " (" + grmembers + " )";
995 996 var grsuffix = " ({0} {1})".format(grmembers, _TM['members']);
996 997
997 998 if (grnameMatchIndex > -1) {
998 999 return _gravatar(grprefix + highlightMatch(grname, query, grnameMatchIndex) + grsuffix,null,true);
999 1000 }
1000 1001 return _gravatar(grprefix + oResultData.grname + grsuffix, null,true);
1001 1002 // Users
1002 1003 } else if (oResultData.nname != undefined) {
1003 1004 var fname = oResultData.fname || "";
1004 1005 var lname = oResultData.lname || "";
1005 1006 var nname = oResultData.nname;
1006 1007
1007 1008 // Guard against null value
1008 1009 var fnameMatchIndex = fname.toLowerCase().indexOf(query),
1009 1010 lnameMatchIndex = lname.toLowerCase().indexOf(query),
1010 1011 nnameMatchIndex = nname.toLowerCase().indexOf(query),
1011 1012 displayfname, displaylname, displaynname;
1012 1013
1013 1014 if (fnameMatchIndex > -1) {
1014 1015 displayfname = highlightMatch(fname, query, fnameMatchIndex);
1015 1016 } else {
1016 1017 displayfname = fname;
1017 1018 }
1018 1019
1019 1020 if (lnameMatchIndex > -1) {
1020 1021 displaylname = highlightMatch(lname, query, lnameMatchIndex);
1021 1022 } else {
1022 1023 displaylname = lname;
1023 1024 }
1024 1025
1025 1026 if (nnameMatchIndex > -1) {
1026 1027 displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
1027 1028 } else {
1028 1029 displaynname = nname ? "(" + nname + ")" : "";
1029 1030 }
1030 1031
1031 1032 return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
1032 1033 } else {
1033 1034 return '';
1034 1035 }
1035 1036 };
1036 1037 membersAC.formatResult = custom_formatter;
1037 1038 ownerAC.formatResult = custom_formatter;
1038 1039
1039 1040 var myHandler = function (sType, aArgs) {
1040 1041
1041 1042 var myAC = aArgs[0]; // reference back to the AC instance
1042 1043 var elLI = aArgs[1]; // reference to the selected LI element
1043 1044 var oData = aArgs[2]; // object literal of selected item's result data
1044 1045 //fill the autocomplete with value
1045 1046 if (oData.nname != undefined) {
1046 1047 //users
1047 1048 myAC.getInputEl().value = oData.nname;
1048 1049 YUD.get('perm_new_member_type').value = 'user';
1049 1050 } else {
1050 1051 //groups
1051 1052 myAC.getInputEl().value = oData.grname;
1052 1053 YUD.get('perm_new_member_type').value = 'users_group';
1053 1054 }
1054 1055 };
1055 1056
1056 1057 membersAC.itemSelectEvent.subscribe(myHandler);
1057 1058 if(ownerAC.itemSelectEvent){
1058 1059 ownerAC.itemSelectEvent.subscribe(myHandler);
1059 1060 }
1060 1061
1061 1062 return {
1062 1063 memberDS: memberDS,
1063 1064 ownerDS: ownerDS,
1064 1065 membersAC: membersAC,
1065 1066 ownerAC: ownerAC,
1066 1067 };
1067 1068 }
1068 1069
1069 1070
1070 1071 var MentionsAutoComplete = function (divid, cont, users_list, groups_list) {
1071 1072 var myUsers = users_list;
1072 1073 var myGroups = groups_list;
1073 1074
1074 1075 // Define a custom search function for the DataSource of users
1075 1076 var matchUsers = function (sQuery) {
1076 1077 var org_sQuery = sQuery;
1077 1078 if(this.mentionQuery == null){
1078 1079 return []
1079 1080 }
1080 1081 sQuery = this.mentionQuery;
1081 1082 // Case insensitive matching
1082 1083 var query = sQuery.toLowerCase();
1083 1084 var i = 0;
1084 1085 var l = myUsers.length;
1085 1086 var matches = [];
1086 1087
1087 1088 // Match against each name of each contact
1088 1089 for (; i < l; i++) {
1089 1090 contact = myUsers[i];
1090 1091 if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
1091 1092 ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
1092 1093 ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
1093 1094 matches[matches.length] = contact;
1094 1095 }
1095 1096 }
1096 1097 return matches
1097 1098 };
1098 1099
1099 1100 //match all
1100 1101 var matchAll = function (sQuery) {
1101 1102 u = matchUsers(sQuery);
1102 1103 return u
1103 1104 };
1104 1105
1105 1106 // DataScheme for owner
1106 1107 var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
1107 1108
1108 1109 ownerDS.responseSchema = {
1109 1110 fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
1110 1111 };
1111 1112
1112 1113 // Instantiate AutoComplete for mentions
1113 1114 var ownerAC = new YAHOO.widget.AutoComplete(divid, cont, ownerDS);
1114 1115 ownerAC.useShadow = false;
1115 1116 ownerAC.resultTypeList = false;
1116 1117 ownerAC.suppressInputUpdate = true;
1117 1118 ownerAC.animVert = false;
1118 1119 ownerAC.animHoriz = false;
1119 1120 ownerAC.animSpeed = 0.1;
1120 1121
1121 1122 // Helper highlight function for the formatter
1122 1123 var highlightMatch = function (full, snippet, matchindex) {
1123 1124 return full.substring(0, matchindex)
1124 1125 + "<span class='match'>"
1125 1126 + full.substr(matchindex, snippet.length)
1126 1127 + "</span>" + full.substring(matchindex + snippet.length);
1127 1128 };
1128 1129
1129 1130 // Custom formatter to highlight the matching letters
1130 1131 ownerAC.formatResult = function (oResultData, sQuery, sResultMatch) {
1131 1132 var org_sQuery = sQuery;
1132 1133 if(this.dataSource.mentionQuery != null){
1133 1134 sQuery = this.dataSource.mentionQuery;
1134 1135 }
1135 1136
1136 1137 var query = sQuery.toLowerCase();
1137 1138 var _gravatar = function(res, em, group){
1138 1139 if (group !== undefined){
1139 1140 em = '/images/icons/group.png'
1140 1141 }
1141 1142 tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
1142 1143 return tmpl.format(em,res)
1143 1144 }
1144 1145 if (oResultData.nname != undefined) {
1145 1146 var fname = oResultData.fname || "";
1146 1147 var lname = oResultData.lname || "";
1147 1148 var nname = oResultData.nname;
1148 1149
1149 1150 // Guard against null value
1150 1151 var fnameMatchIndex = fname.toLowerCase().indexOf(query),
1151 1152 lnameMatchIndex = lname.toLowerCase().indexOf(query),
1152 1153 nnameMatchIndex = nname.toLowerCase().indexOf(query),
1153 1154 displayfname, displaylname, displaynname;
1154 1155
1155 1156 if (fnameMatchIndex > -1) {
1156 1157 displayfname = highlightMatch(fname, query, fnameMatchIndex);
1157 1158 } else {
1158 1159 displayfname = fname;
1159 1160 }
1160 1161
1161 1162 if (lnameMatchIndex > -1) {
1162 1163 displaylname = highlightMatch(lname, query, lnameMatchIndex);
1163 1164 } else {
1164 1165 displaylname = lname;
1165 1166 }
1166 1167
1167 1168 if (nnameMatchIndex > -1) {
1168 1169 displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
1169 1170 } else {
1170 1171 displaynname = nname ? "(" + nname + ")" : "";
1171 1172 }
1172 1173
1173 1174 return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
1174 1175 } else {
1175 1176 return '';
1176 1177 }
1177 1178 };
1178 1179
1179 1180 if(ownerAC.itemSelectEvent){
1180 1181 ownerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
1181 1182
1182 1183 var myAC = aArgs[0]; // reference back to the AC instance
1183 1184 var elLI = aArgs[1]; // reference to the selected LI element
1184 1185 var oData = aArgs[2]; // object literal of selected item's result data
1185 1186 //fill the autocomplete with value
1186 1187 if (oData.nname != undefined) {
1187 1188 //users
1188 1189 //Replace the mention name with replaced
1189 1190 var re = new RegExp();
1190 1191 var org = myAC.getInputEl().value;
1191 1192 var chunks = myAC.dataSource.chunks
1192 1193 // replace middle chunk(the search term) with actuall match
1193 1194 chunks[1] = chunks[1].replace('@'+myAC.dataSource.mentionQuery,
1194 1195 '@'+oData.nname+' ');
1195 1196 myAC.getInputEl().value = chunks.join('')
1196 1197 YUD.get(myAC.getInputEl()).focus(); // Y U NO WORK !?
1197 1198 } else {
1198 1199 //groups
1199 1200 myAC.getInputEl().value = oData.grname;
1200 1201 YUD.get('perm_new_member_type').value = 'users_group';
1201 1202 }
1202 1203 });
1203 1204 }
1204 1205
1205 1206 // in this keybuffer we will gather current value of search !
1206 1207 // since we need to get this just when someone does `@` then we do the
1207 1208 // search
1208 1209 ownerAC.dataSource.chunks = [];
1209 1210 ownerAC.dataSource.mentionQuery = null;
1210 1211
1211 1212 ownerAC.get_mention = function(msg, max_pos) {
1212 1213 var org = msg;
1213 1214 var re = new RegExp('(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)$')
1214 1215 var chunks = [];
1215 1216
1216 1217
1217 1218 // cut first chunk until curret pos
1218 1219 var to_max = msg.substr(0, max_pos);
1219 1220 var at_pos = Math.max(0,to_max.lastIndexOf('@')-1);
1220 1221 var msg2 = to_max.substr(at_pos);
1221 1222
1222 1223 chunks.push(org.substr(0,at_pos))// prefix chunk
1223 1224 chunks.push(msg2) // search chunk
1224 1225 chunks.push(org.substr(max_pos)) // postfix chunk
1225 1226
1226 1227 // clean up msg2 for filtering and regex match
1227 1228 var msg2 = msg2.lstrip(' ').lstrip('\n');
1228 1229
1229 1230 if(re.test(msg2)){
1230 1231 var unam = re.exec(msg2)[1];
1231 1232 return [unam, chunks];
1232 1233 }
1233 1234 return [null, null];
1234 1235 };
1235 1236
1236 1237 ownerAC.textboxKeyUpEvent.subscribe(function(type, args){
1237 1238
1238 1239 var ac_obj = args[0];
1239 1240 var currentMessage = args[1];
1240 1241 var currentCaretPosition = args[0]._elTextbox.selectionStart;
1241 1242
1242 1243 var unam = ownerAC.get_mention(currentMessage, currentCaretPosition);
1243 1244 var curr_search = null;
1244 1245 if(unam[0]){
1245 1246 curr_search = unam[0];
1246 1247 }
1247 1248
1248 1249 ownerAC.dataSource.chunks = unam[1];
1249 1250 ownerAC.dataSource.mentionQuery = curr_search;
1250 1251
1251 1252 })
1252 1253
1253 1254 return {
1254 1255 ownerDS: ownerDS,
1255 1256 ownerAC: ownerAC,
1256 1257 };
1257 1258 }
1258 1259
1259 1260
1260 1261 var PullRequestAutoComplete = function (divid, cont, users_list, groups_list) {
1261 1262 var myUsers = users_list;
1262 1263 var myGroups = groups_list;
1263 1264
1264 1265 // Define a custom search function for the DataSource of users
1265 1266 var matchUsers = function (sQuery) {
1266 1267 // Case insensitive matching
1267 1268 var query = sQuery.toLowerCase();
1268 1269 var i = 0;
1269 1270 var l = myUsers.length;
1270 1271 var matches = [];
1271 1272
1272 1273 // Match against each name of each contact
1273 1274 for (; i < l; i++) {
1274 1275 contact = myUsers[i];
1275 1276 if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
1276 1277 ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
1277 1278 ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
1278 1279 matches[matches.length] = contact;
1279 1280 }
1280 1281 }
1281 1282 return matches;
1282 1283 };
1283 1284
1284 1285 // Define a custom search function for the DataSource of usersGroups
1285 1286 var matchGroups = function (sQuery) {
1286 1287 // Case insensitive matching
1287 1288 var query = sQuery.toLowerCase();
1288 1289 var i = 0;
1289 1290 var l = myGroups.length;
1290 1291 var matches = [];
1291 1292
1292 1293 // Match against each name of each contact
1293 1294 for (; i < l; i++) {
1294 1295 matched_group = myGroups[i];
1295 1296 if (matched_group.grname.toLowerCase().indexOf(query) > -1) {
1296 1297 matches[matches.length] = matched_group;
1297 1298 }
1298 1299 }
1299 1300 return matches;
1300 1301 };
1301 1302
1302 1303 //match all
1303 1304 var matchAll = function (sQuery) {
1304 1305 u = matchUsers(sQuery);
1305 1306 return u
1306 1307 };
1307 1308
1308 1309 // DataScheme for owner
1309 1310 var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
1310 1311
1311 1312 ownerDS.responseSchema = {
1312 1313 fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
1313 1314 };
1314 1315
1315 1316 // Instantiate AutoComplete for mentions
1316 1317 var reviewerAC = new YAHOO.widget.AutoComplete(divid, cont, ownerDS);
1317 1318 reviewerAC.useShadow = false;
1318 1319 reviewerAC.resultTypeList = false;
1319 1320 reviewerAC.suppressInputUpdate = true;
1320 1321 reviewerAC.animVert = false;
1321 1322 reviewerAC.animHoriz = false;
1322 1323 reviewerAC.animSpeed = 0.1;
1323 1324
1324 1325 // Helper highlight function for the formatter
1325 1326 var highlightMatch = function (full, snippet, matchindex) {
1326 1327 return full.substring(0, matchindex)
1327 1328 + "<span class='match'>"
1328 1329 + full.substr(matchindex, snippet.length)
1329 1330 + "</span>" + full.substring(matchindex + snippet.length);
1330 1331 };
1331 1332
1332 1333 // Custom formatter to highlight the matching letters
1333 1334 reviewerAC.formatResult = function (oResultData, sQuery, sResultMatch) {
1334 1335 var org_sQuery = sQuery;
1335 1336 if(this.dataSource.mentionQuery != null){
1336 1337 sQuery = this.dataSource.mentionQuery;
1337 1338 }
1338 1339
1339 1340 var query = sQuery.toLowerCase();
1340 1341 var _gravatar = function(res, em, group){
1341 1342 if (group !== undefined){
1342 1343 em = '/images/icons/group.png'
1343 1344 }
1344 1345 tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
1345 1346 return tmpl.format(em,res)
1346 1347 }
1347 1348 if (oResultData.nname != undefined) {
1348 1349 var fname = oResultData.fname || "";
1349 1350 var lname = oResultData.lname || "";
1350 1351 var nname = oResultData.nname;
1351 1352
1352 1353 // Guard against null value
1353 1354 var fnameMatchIndex = fname.toLowerCase().indexOf(query),
1354 1355 lnameMatchIndex = lname.toLowerCase().indexOf(query),
1355 1356 nnameMatchIndex = nname.toLowerCase().indexOf(query),
1356 1357 displayfname, displaylname, displaynname;
1357 1358
1358 1359 if (fnameMatchIndex > -1) {
1359 1360 displayfname = highlightMatch(fname, query, fnameMatchIndex);
1360 1361 } else {
1361 1362 displayfname = fname;
1362 1363 }
1363 1364
1364 1365 if (lnameMatchIndex > -1) {
1365 1366 displaylname = highlightMatch(lname, query, lnameMatchIndex);
1366 1367 } else {
1367 1368 displaylname = lname;
1368 1369 }
1369 1370
1370 1371 if (nnameMatchIndex > -1) {
1371 1372 displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
1372 1373 } else {
1373 1374 displaynname = nname ? "(" + nname + ")" : "";
1374 1375 }
1375 1376
1376 1377 return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
1377 1378 } else {
1378 1379 return '';
1379 1380 }
1380 1381 };
1381 1382
1382 1383 //members cache to catch duplicates
1383 1384 reviewerAC.dataSource.cache = [];
1384 1385 // hack into select event
1385 1386 if(reviewerAC.itemSelectEvent){
1386 1387 reviewerAC.itemSelectEvent.subscribe(function (sType, aArgs) {
1387 1388
1388 1389 var myAC = aArgs[0]; // reference back to the AC instance
1389 1390 var elLI = aArgs[1]; // reference to the selected LI element
1390 1391 var oData = aArgs[2]; // object literal of selected item's result data
1391 1392 var members = YUD.get('review_members');
1392 1393 //fill the autocomplete with value
1393 1394
1394 1395 if (oData.nname != undefined) {
1395 1396 if (myAC.dataSource.cache.indexOf(oData.id) != -1){
1396 1397 return
1397 1398 }
1398 1399
1399 1400 var tmpl = '<li id="reviewer_{2}">'+
1400 1401 '<div class="reviewers_member">'+
1401 1402 '<div class="gravatar"><img alt="gravatar" src="{0}"/> </div>'+
1402 1403 '<div style="float:left">{1}</div>'+
1403 1404 '<input type="hidden" value="{2}" name="review_members" />'+
1404 1405 '<span class="delete_icon action_button" onclick="removeReviewer({2})"></span>'+
1405 1406 '</div>'+
1406 1407 '</li>'
1407 1408
1408 1409 var displayname = "{0} {1} ({2})".format(oData.fname,oData.lname,oData.nname);
1409 1410 var element = tmpl.format(oData.gravatar_lnk,displayname,oData.id);
1410 1411 members.innerHTML += element;
1411 1412 myAC.dataSource.cache.push(oData.id);
1412 1413 YUD.get('user').value = ''
1413 1414 }
1414 1415 });
1415 1416 }
1416 1417 return {
1417 1418 ownerDS: ownerDS,
1418 1419 reviewerAC: reviewerAC,
1419 1420 };
1420 1421 }
1421 1422
1422 1423
1423 1424 /**
1424 1425 * QUICK REPO MENU
1425 1426 */
1426 1427 var quick_repo_menu = function(){
1427 1428 YUE.on(YUQ('.quick_repo_menu'),'mouseenter',function(e){
1428 1429 var menu = e.currentTarget.firstElementChild.firstElementChild;
1429 1430 if(YUD.hasClass(menu,'hidden')){
1430 1431 YUD.replaceClass(e.currentTarget,'hidden', 'active');
1431 1432 YUD.replaceClass(menu, 'hidden', 'active');
1432 1433 }
1433 1434 })
1434 1435 YUE.on(YUQ('.quick_repo_menu'),'mouseleave',function(e){
1435 1436 var menu = e.currentTarget.firstElementChild.firstElementChild;
1436 1437 if(YUD.hasClass(menu,'active')){
1437 1438 YUD.replaceClass(e.currentTarget, 'active', 'hidden');
1438 1439 YUD.replaceClass(menu, 'active', 'hidden');
1439 1440 }
1440 1441 })
1441 1442 };
1442 1443
1443 1444
1444 1445 /**
1445 1446 * TABLE SORTING
1446 1447 */
1447 1448
1448 1449 // returns a node from given html;
1449 1450 var fromHTML = function(html){
1450 1451 var _html = document.createElement('element');
1451 1452 _html.innerHTML = html;
1452 1453 return _html;
1453 1454 }
1454 1455 var get_rev = function(node){
1455 1456 var n = node.firstElementChild.firstElementChild;
1456 1457
1457 1458 if (n===null){
1458 1459 return -1
1459 1460 }
1460 1461 else{
1461 1462 out = n.firstElementChild.innerHTML.split(':')[0].replace('r','');
1462 1463 return parseInt(out);
1463 1464 }
1464 1465 }
1465 1466
1466 1467 var get_name = function(node){
1467 1468 var name = node.firstElementChild.children[2].innerHTML;
1468 1469 return name
1469 1470 }
1470 1471 var get_group_name = function(node){
1471 1472 var name = node.firstElementChild.children[1].innerHTML;
1472 1473 return name
1473 1474 }
1474 1475 var get_date = function(node){
1475 1476 var date_ = YUD.getAttribute(node.firstElementChild,'date');
1476 1477 return date_
1477 1478 }
1478 1479
1479 1480 var get_age = function(node){
1480 1481 return node
1481 1482 }
1482 1483
1483 1484 var get_link = function(node){
1484 1485 return node.firstElementChild.text;
1485 1486 }
1486 1487
1487 1488 var revisionSort = function(a, b, desc, field) {
1488 1489
1489 1490 var a_ = fromHTML(a.getData(field));
1490 1491 var b_ = fromHTML(b.getData(field));
1491 1492
1492 1493 // extract revisions from string nodes
1493 1494 a_ = get_rev(a_)
1494 1495 b_ = get_rev(b_)
1495 1496
1496 1497 var comp = YAHOO.util.Sort.compare;
1497 1498 var compState = comp(a_, b_, desc);
1498 1499 return compState;
1499 1500 };
1500 1501 var ageSort = function(a, b, desc, field) {
1501 1502 var a_ = fromHTML(a.getData(field));
1502 1503 var b_ = fromHTML(b.getData(field));
1503 1504
1504 1505 // extract name from table
1505 1506 a_ = get_date(a_)
1506 1507 b_ = get_date(b_)
1507 1508
1508 1509 var comp = YAHOO.util.Sort.compare;
1509 1510 var compState = comp(a_, b_, desc);
1510 1511 return compState;
1511 1512 };
1512 1513
1513 1514 var nameSort = function(a, b, desc, field) {
1514 1515 var a_ = fromHTML(a.getData(field));
1515 1516 var b_ = fromHTML(b.getData(field));
1516 1517
1517 1518 // extract name from table
1518 1519 a_ = get_name(a_)
1519 1520 b_ = get_name(b_)
1520 1521
1521 1522 var comp = YAHOO.util.Sort.compare;
1522 1523 var compState = comp(a_, b_, desc);
1523 1524 return compState;
1524 1525 };
1525 1526
1526 1527 var permNameSort = function(a, b, desc, field) {
1527 1528 var a_ = fromHTML(a.getData(field));
1528 1529 var b_ = fromHTML(b.getData(field));
1529 1530 // extract name from table
1530 1531
1531 1532 a_ = a_.children[0].innerHTML;
1532 1533 b_ = b_.children[0].innerHTML;
1533 1534
1534 1535 var comp = YAHOO.util.Sort.compare;
1535 1536 var compState = comp(a_, b_, desc);
1536 1537 return compState;
1537 1538 };
1538 1539
1539 1540 var groupNameSort = function(a, b, desc, field) {
1540 1541 var a_ = fromHTML(a.getData(field));
1541 1542 var b_ = fromHTML(b.getData(field));
1542 1543
1543 1544 // extract name from table
1544 1545 a_ = get_group_name(a_)
1545 1546 b_ = get_group_name(b_)
1546 1547
1547 1548 var comp = YAHOO.util.Sort.compare;
1548 1549 var compState = comp(a_, b_, desc);
1549 1550 return compState;
1550 1551 };
1551 1552 var dateSort = function(a, b, desc, field) {
1552 1553 var a_ = fromHTML(a.getData(field));
1553 1554 var b_ = fromHTML(b.getData(field));
1554 1555
1555 1556 // extract name from table
1556 1557 a_ = get_date(a_)
1557 1558 b_ = get_date(b_)
1558 1559
1559 1560 var comp = YAHOO.util.Sort.compare;
1560 1561 var compState = comp(a_, b_, desc);
1561 1562 return compState;
1562 1563 };
1563 1564
1564 1565 var linkSort = function(a, b, desc, field) {
1565 1566 var a_ = fromHTML(a.getData(field));
1566 1567 var b_ = fromHTML(a.getData(field));
1567 1568
1568 1569 // extract url text from string nodes
1569 1570 a_ = get_link(a_)
1570 1571 b_ = get_link(b_)
1571 1572
1572 1573 var comp = YAHOO.util.Sort.compare;
1573 1574 var compState = comp(a_, b_, desc);
1574 1575 return compState;
1575 1576 }
1576 1577
1577 1578
1578 1579 /* Multi selectors */
1579 1580
1580 1581 var MultiSelectWidget = function(selected_id, available_id, form_id){
1581 1582
1582 1583
1583 1584 //definition of containers ID's
1584 1585 var selected_container = selected_id;
1585 1586 var available_container = available_id;
1586 1587
1587 1588 //temp container for selected storage.
1588 1589 var cache = new Array();
1589 1590 var av_cache = new Array();
1590 1591 var c = YUD.get(selected_container);
1591 1592 var ac = YUD.get(available_container);
1592 1593
1593 1594 //get only selected options for further fullfilment
1594 1595 for(var i = 0;node =c.options[i];i++){
1595 1596 if(node.selected){
1596 1597 //push selected to my temp storage left overs :)
1597 1598 cache.push(node);
1598 1599 }
1599 1600 }
1600 1601
1601 1602 //get all available options to cache
1602 1603 for(var i = 0;node =ac.options[i];i++){
1603 1604 //push selected to my temp storage left overs :)
1604 1605 av_cache.push(node);
1605 1606 }
1606 1607
1607 1608 //fill available only with those not in choosen
1608 1609 ac.options.length=0;
1609 1610 tmp_cache = new Array();
1610 1611
1611 1612 for(var i = 0;node = av_cache[i];i++){
1612 1613 var add = true;
1613 1614 for(var i2 = 0;node_2 = cache[i2];i2++){
1614 1615 if(node.value == node_2.value){
1615 1616 add=false;
1616 1617 break;
1617 1618 }
1618 1619 }
1619 1620 if(add){
1620 1621 tmp_cache.push(new Option(node.text, node.value, false, false));
1621 1622 }
1622 1623 }
1623 1624
1624 1625 for(var i = 0;node = tmp_cache[i];i++){
1625 1626 ac.options[i] = node;
1626 1627 }
1627 1628
1628 1629 function prompts_action_callback(e){
1629 1630
1630 1631 var choosen = YUD.get(selected_container);
1631 1632 var available = YUD.get(available_container);
1632 1633
1633 1634 //get checked and unchecked options from field
1634 1635 function get_checked(from_field){
1635 1636 //temp container for storage.
1636 1637 var sel_cache = new Array();
1637 1638 var oth_cache = new Array();
1638 1639
1639 1640 for(var i = 0;node = from_field.options[i];i++){
1640 1641 if(node.selected){
1641 1642 //push selected fields :)
1642 1643 sel_cache.push(node);
1643 1644 }
1644 1645 else{
1645 1646 oth_cache.push(node)
1646 1647 }
1647 1648 }
1648 1649
1649 1650 return [sel_cache,oth_cache]
1650 1651 }
1651 1652
1652 1653 //fill the field with given options
1653 1654 function fill_with(field,options){
1654 1655 //clear firtst
1655 1656 field.options.length=0;
1656 1657 for(var i = 0;node = options[i];i++){
1657 1658 field.options[i]=new Option(node.text, node.value,
1658 1659 false, false);
1659 1660 }
1660 1661
1661 1662 }
1662 1663 //adds to current field
1663 1664 function add_to(field,options){
1664 1665 for(var i = 0;node = options[i];i++){
1665 1666 field.appendChild(new Option(node.text, node.value,
1666 1667 false, false));
1667 1668 }
1668 1669 }
1669 1670
1670 1671 // add action
1671 1672 if (this.id=='add_element'){
1672 1673 var c = get_checked(available);
1673 1674 add_to(choosen,c[0]);
1674 1675 fill_with(available,c[1]);
1675 1676 }
1676 1677 // remove action
1677 1678 if (this.id=='remove_element'){
1678 1679 var c = get_checked(choosen);
1679 1680 add_to(available,c[0]);
1680 1681 fill_with(choosen,c[1]);
1681 1682 }
1682 1683 // add all elements
1683 1684 if(this.id=='add_all_elements'){
1684 1685 for(var i=0; node = available.options[i];i++){
1685 1686 choosen.appendChild(new Option(node.text,
1686 1687 node.value, false, false));
1687 1688 }
1688 1689 available.options.length = 0;
1689 1690 }
1690 1691 //remove all elements
1691 1692 if(this.id=='remove_all_elements'){
1692 1693 for(var i=0; node = choosen.options[i];i++){
1693 1694 available.appendChild(new Option(node.text,
1694 1695 node.value, false, false));
1695 1696 }
1696 1697 choosen.options.length = 0;
1697 1698 }
1698 1699
1699 1700 }
1700 1701
1701 1702 YUE.addListener(['add_element','remove_element',
1702 1703 'add_all_elements','remove_all_elements'],'click',
1703 1704 prompts_action_callback)
1704 1705 if (form_id !== undefined) {
1705 1706 YUE.addListener(form_id,'submit',function(){
1706 1707 var choosen = YUD.get(selected_container);
1707 1708 for (var i = 0; i < choosen.options.length; i++) {
1708 1709 choosen.options[i].selected = 'selected';
1709 1710 }
1710 1711 });
1711 1712 }
1712 1713 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now