1(function($) { 2 "use strict"; 3 $.breakingNews = function(element, options) { 4 5 var defaults = { 6 effect : 'scroll', 7 direction : 'ltr', 8 height : 40, 9 fontSize : "default", 10 themeColor : "default", 11 background : "default", 12 borderWidth : 1, 13 radius : 2, 14 source : "html", 15 play : true, 16 delayTimer : 4000, 17 scrollSpeed : 2, 18 stopOnHover : true, 19 position : 'auto', 20 zIndex : 99999 21 } 22 23 var ticker = this; 24 ticker.settings = {} 25 26 var $element = $(element); 27 var element = element; 28 29 var _label = $element.children(".bn-label"), 30 _news = $element.children(".bn-news"), 31 _ul = _news.children("ul"), 32 _li = _ul.children("li"), 33 _controls = $element.children(".bn-controls"), 34 _prev = _controls.find(".bn-prev").parent(), 35 _action = _controls.find(".bn-action").parent(), 36 _next = _controls.find(".bn-next").parent(); 37 38 var _pause = false; 39 var _controlsIsActive = true; 40 var _totalNews = _ul.children("li").length; 41 var _activeNews = 0; 42 var _interval = false; 43 44 /****************************************************/ 45 /**PRIVATE METHODS***********************************/ 46 /****************************************************/ 47 var initializeSettings = function() { 48 if (ticker.settings.position === 'fixed-top') 49 $element.addClass('bn-fixed-top').css({'z-index': ticker.settings.zIndex}); 50 else if (ticker.settings.position === 'fixed-bottom') 51 $element.addClass('bn-fixed-bottom').css({'z-index': ticker.settings.zIndex}); 52 53 if (ticker.settings.fontSize != 'default') 54 { 55 $element.css({ 56 'font-size' : ticker.settings.fontSize 57 }); 58 } 59 60 if (ticker.settings.themeColor != 'default') 61 { 62 $element.css({ 63 'border-color' : ticker.settings.themeColor, 64 'color' : ticker.settings.themeColor 65 }); 66 67 _label.css({ 68 'background' : ticker.settings.themeColor 69 }); 70 } 71 72 if (ticker.settings.background != 'default') 73 { 74 $element.css({ 75 'background' : ticker.settings.background 76 }); 77 } 78 79 $element.css({ 80 'height' : ticker.settings.height, 81 'line-height' : (ticker.settings.height-(ticker.settings.borderWidth*2))+'px', 82 'border-radius' : ticker.settings.radius, 83 'border-width' : ticker.settings.borderWidth 84 }); 85 86 87 _li.find('.bn-seperator').css({ 88 'height': ticker.settings.height-(ticker.settings.borderWidth*2) 89 }); 90 91 } 92 93 var setContainerWidth = function(){ 94 if (_label.length > 0){ 95 if (ticker.settings.direction == 'rtl') 96 _news.css({"right":_label.outerWidth()}); 97 else 98 _news.css({"left":_label.outerWidth()}); 99 } 100 101 if (_controls.length > 0){ 102 var controlsWidth = _controls.outerWidth(); 103 if (ticker.settings.direction == 'rtl') 104 _news.css({"left":controlsWidth}); 105 else 106 _news.css({"right":controlsWidth}); 107 } 108 109 if (ticker.settings.effect === 'scroll') 110 { 111 var totalW = 0; 112 _li.each(function(){ 113 totalW += $(this).outerWidth(); 114 }); 115 totalW += 10; 116 _ul.css({'width':totalW}); 117 } 118 } 119 120 var loadDataWithRSS2JSON = function(){ 121 var xhr = new XMLHttpRequest(); 122 123 xhr.onreadystatechange = function(){ 124 if (xhr.readyState==4 && xhr.status==200) 125 { 126 var obj = JSON.parse(xhr.responseText); 127 var items = ''; 128 var showingField = ''; 129 switch (ticker.settings.source.showingField){ 130 case 'title': 131 showingField = 'title'; 132 break; 133 case 'description': 134 showingField = 'description'; 135 break; 136 case 'link': 137 showingField = 'link'; 138 break; 139 default: 140 showingField = 'title'; 141 } 142 var seperator = ''; 143 if (typeof ticker.settings.source.seperator != 'undefined' && typeof ticker.settings.source.seperator !== undefined) 144 seperator = ticker.settings.source.seperator; 145 146 147 for (var i = 0; i < obj.items.length; i++){ 148 if (ticker.settings.source.linkEnabled) 149 items += '<li><a target="'+ticker.settings.source.target+'" href="'+obj.items[i].link+'">'+seperator+obj.items[i][showingField]+'</a></li>'; 150 else 151 items += '<li><a>'+seperator+obj.items[i][showingField]+'</a></li>'; 152 } 153 _ul.empty().append(items); 154 _li = _ul.children("li"); 155 _totalNews = _ul.children("li").length; 156 setContainerWidth(); 157 if (ticker.settings.effect != 'scroll') 158 showThis(); 159 160 _li.find('.bn-seperator').css({ 161 'height': ticker.settings.height-(ticker.settings.borderWidth*2) 162 }); 163
164 playHandler(); 165 } 166 }; 167 xhr.open( 168 'GET', 169 'https://api.rss2json.com/v1/api.json?rss_url='+ticker.settings.source.url, 170 true 171 ); 172 xhr.send(); 173 } 174 175 var loadDataWithYQL = function(){ 176 var xmlhttp = new XMLHttpRequest(); 177 xmlhttp.open('GET', 'https://query.yahooapis.com/v1/public/yql?q='+encodeURIComponent('select * from rss where url="'+ticker.settings.source.url+'" limit '+ticker.settings.source.limit)+'&format=json', true); 178 xmlhttp.onreadystatechange = function() { 179 if (xmlhttp.readyState == 4) { 180 if(xmlhttp.status == 200) { 181 var obj = JSON.parse(xmlhttp.responseText); 182 var items = ''; 183 var showingField = ''; 184 switch (ticker.settings.source.showingField){ 185 case 'title': 186 showingField = 'title'; 187 break; 188 case 'description': 189 showingField = 'description'; 190 break; 191 case 'link': 192 showingField = 'link'; 193 break; 194 default: 195 showingField = 'title'; 196 } 197 var seperator = ''; 198 if (ticker.settings.source.seperator != 'undefined' && ticker.settings.source.seperator !== undefined) 199 seperator = ticker.settings.source.seperator; 200 201 for (var i = 0; i < obj.query.results.item.length; i++){ 202 if (ticker.settings.source.linkEnabled) 203 items += '<li><a target="'+ticker.settings.source.target+'" href="'+obj.query.results.item[i].link+'">'+seperator+obj.query.results.item[i][showingField]+'</a></li>'; 204 else 205 items += '<li><a>'+seperator+obj.query.results.item[i][showingField]+'</a></li>'; 206 } 207 _ul.empty().append(items); 208 _li = _ul.children("li"); 209 _totalNews = _ul.children("li").length; 210 setContainerWidth(); 211 if (ticker.settings.effect != 'scroll') 212 showThis(); 213 214 _li.find('.bn-seperator').css({ 215 'height': ticker.settings.height-(ticker.settings.borderWidth*2) 216 }); 217 218 playHandler(); 219 } 220 else 221 _ul.empty().append('<li><span class="bn-loader-text">'+ticker.settings.source.errorMsg+'</span></li>'); 222 } 223 }; 224 xmlhttp.send(null); 225 } 226 227 var loadJSON = function(){ 228 $.getJSON( ticker.settings.source.url, function( data ) { 229 var items = ''; 230 231 var showingField = ''; 232 if (ticker.settings.source.showingField === 'undefined') 233 showingField = 'title'; 234 else 235 showingField = ticker.settings.source.showingField; 236 237 var seperator = ''; 238 if (typeof ticker.settings.source.seperator != 'undefined' && typeof ticker.settings.source.seperator !== undefined) 239 seperator = ticker.settings.source.seperator; 240 241 for (var i = 0; i < data.length; i++){ 242 if (i >= ticker.settings.source.limit) 243 break; 244 245 if (ticker.settings.source.linkEnabled) 246 items += ( '<li><a target="'+ticker.settings.source.target+'" href="'+data[i].link+'">'+seperator+data[i][showingField]+'</a></li>' ); 247 else 248 items += ( '<li><a>'+seperator+data[i][showingField]+'</a></li>' ); 249 if (data[i][showingField] === 'undefined') 250 console.log('"'+showingField+'" does not exist in this json.') 251 } 252 253 _ul.empty().append(items); 254 _li = _ul.children("li"); 255 _totalNews = _ul.children("li").length; 256 setContainerWidth(); 257 if (ticker.settings.effect != 'scroll') 258 showThis(); 259 260 _li.find('.bn-seperator').css({ 261 'height': ticker.settings.height-(ticker.settings.borderWidth*2) 262 }); 263
264 playHandler(); 265 }); 266 } 267 268 var startScrollAnimationLTR = function(){ 269 var _ulPosition = parseFloat(_ul.css('marginLeft')); 270 _ulPosition -= ticker.settings.scrollSpeed/2; 271 _ul.css({'marginLeft': _ulPosition }); 272 273 if (_ulPosition <= -_ul.find('li:first-child').outerWidth()) 274 { 275 _ul.find('li:first-child').insertAfter(_ul.find('li:last-child')); 276 _ul.css({'marginLeft': 0 }); 277 } 278 if (_pause === false) 279 (window.requestAnimationFrame && requestAnimationFrame(startScrollAnimationLTR)) || setTimeout(startScrollAnimationLTR, 16); 280 } 281 282 var startScrollAnimationRTL = function(){ 283 var _ulPosition = parseFloat(_ul.css('marginRight')); 284 _ulPosition -= ticker.settings.scrollSpeed/2; 285 _ul.css({'marginRight': _ulPosition }); 286 287 if (_ulPosition <= -_ul.find('li:first-child').outerWidth()) 288 { 289 _ul.find('li:first-child').insertAfter(_ul.find('li:last-child')); 290 _ul.css({'marginRight': 0 }); 291 } 292 if (_pause === false) 293 (window.requestAnimationFrame && requestAnimationFrame(startScrollAnimationRTL)) || setTimeout(startScrollAnimationRTL, 16); 294 } 295 296 var scrollPlaying = function(){ 297 if (ticker.settings.direction === 'rtl') 298 startScrollAnimationRTL(); 299 else 300 startScrollAnimationLTR(); 301 } 302 303 var scrollGoNextLTR = function(){ 304 _ul.stop().animate({ 305 marginLeft : - _ul.find('li:first-child').outerWidth() 306 },300, function(){ 307 _ul.find('li:first-child').insertAfter(_ul.find('li:last-child')); 308 _ul.css({'marginLeft': 0 }); 309 _controlsIsActive = true; 310 }); 311 } 312 313 var scrollGoNextRTL = function(){ 314 _ul.stop().animate({ 315 marginRight : - _ul.find('li:first-child').outerWidth() 316 },300, function(){ 317 _ul.find('li:first-child').insertAfter(_ul.find('li:last-child')); 318 _ul.css({'marginRight': 0 }); 319 _controlsIsActive = true; 320 }); 321 } 322 323 var scrollGoPrevLTR = function(){ 324 var _ulPosition = parseInt(_ul.css('marginLeft'),10); 325 if (_ulPosition >= 0) 326 { 327 _ul.css({'margin-left' : -_ul.find('li:last-child').outerWidth()}); 328 _ul.find('li:last-child').insertBefore(_ul.find('li:first-child')); 329 } 330 331 _ul.stop().animate({ 332 marginLeft : 0 333 },300, function(){ 334 _controlsIsActive = true; 335 }); 336 } 337 338 var scrollGoPrevRTL = function(){ 339 var _ulPosition = parseInt(_ul.css('marginRight'),10); 340 if (_ulPosition >= 0) 341 { 342 _ul.css({'margin-right' : -_ul.find('li:last-child').outerWidth()}); 343 _ul.find('li:last-child').insertBefore(_ul.find('li:first-child')); 344 } 345 346 _ul.stop().animate({ 347 marginRight : 0 348 },300, function(){ 349 _controlsIsActive = true; 350 }); 351 } 352 353 var scrollNext = function(){ 354 if (ticker.settings.direction === 'rtl') 355 scrollGoNextRTL(); 356 else 357 scrollGoNextLTR(); 358 } 359 360 var scrollPrev = function(){ 361 if (ticker.settings.direction === 'rtl') 362 scrollGoPrevRTL(); 363 else 364 scrollGoPrevLTR(); 365 } 366 367 var effectTypography = function(){ 368 _ul.find('li').hide(); 369 _ul.find('li').eq(_activeNews).width(30).show(); 370 _ul.find('li').eq(_activeNews).animate({ 371 width: '100%', 372 opacity : 1 373 },1500); 374 } 375 376 var effectFade = function(){ 377 _ul.find('li').hide(); 378 _ul.find('li').eq(_activeNews).fadeIn(); 379 } 380 381 var effectSlideDown = function(){ 382 383 _ul.find('li:visible').animate({ 384 'top':30, 385 'opacity':0 386 },300, function(){ 387 $(this).hide(); 388 }); 389 390 _ul.find('li').eq(_activeNews).css({ 391 'top': -30, 392 'opacity' : 0 393 }).show(); 394 395 _ul.find('li').eq(_activeNews).animate({ 396 'top': 0, 397 'opacity' : 1 398 },300); 399 } 400 401 var effectSlideUp = function(){ 402 403 _ul.find('li:visible').animate({ 404 'top':-30, 405 'opacity':0 406 },300, function(){ 407 $(this).hide(); 408 }); 409 410 _ul.find('li').eq(_activeNews).css({ 411 'top': 30, 412 'opacity' : 0 413 }).show(); 414 415 _ul.find('li').eq(_activeNews).animate({ 416 'top': 0, 417 'opacity' : 1 418 },300); 419 } 420 421 var effectSlideLeft = function(){ 422 423 _ul.find('li:visible').animate({ 424 'left':'50%', 425 'opacity':0 426 },300, function(){ 427 $(this).hide(); 428 }); 429 430 _ul.find('li').eq(_activeNews).css({ 431 'left': -50, 432 'opacity' : 0 433 }).show(); 434 435 _ul.find('li').eq(_activeNews).animate({ 436 'left': 0, 437 'opacity' : 1 438 },300); 439 } 440 441 var effectSlideRight = function(){ 442 443 _ul.find('li:visible').animate({ 444 'left':'-50%', 445 'opacity':0 446 },300, function(){ 447 $(this).hide(); 448 }); 449 450 _ul.find('li').eq(_activeNews).css({ 451 'left': '50%', 452 'opacity' : 0 453 }).show(); 454 455 _ul.find('li').eq(_activeNews).animate({ 456 'left': 0, 457 'opacity' : 1 458 },300); 459 } 460 461 462 var showThis = function(){ 463 _controlsIsActive = true; 464 465 switch (ticker.settings.effect){ 466 case 'typography': 467 effectTypography(); 468 break; 469 case 'fade': 470 effectFade(); 471 break; 472 case 'slide-down': 473 effectSlideDown(); 474 break; 475 case 'slide-up': 476 effectSlideUp(); 477 break; 478 case 'slide-left': 479 effectSlideLeft(); 480 break; 481 case 'slide-right': 482 effectSlideRight(); 483 break; 484 default: 485 _ul.find('li').hide(); 486 _ul.find('li').eq(_activeNews).show(); 487 } 488 489 } 490 491 var nextHandler = function(){ 492 switch (ticker.settings.effect){ 493 case 'scroll': 494 scrollNext(); 495 break; 496 default: 497 _activeNews++; 498 if (_activeNews >= _totalNews) 499 _activeNews = 0; 500 501 showThis(); 502 503 } 504 } 505 506 var prevHandler = function(){ 507 switch (ticker.settings.effect){ 508 case 'scroll': 509 scrollPrev(); 510 break; 511 default: 512 _activeNews--; 513 if (_activeNews < 0) 514 _activeNews = _totalNews-1; 515 516 showThis(); 517 } 518 } 519 520 var playHandler = function(){ 521 _pause = false; 522 if (ticker.settings.play) 523 { 524 switch (ticker.settings.effect){ 525 case 'scroll': 526 scrollPlaying(); 527 break; 528 default: 529 ticker.pause(); 530 _interval = setInterval(function(){ 531 ticker.next(); 532 },ticker.settings.delayTimer); 533 } 534 } 535 } 536 537 /****************************************************/ 538 /**PUBLIC METHODS************************************/ 539 /****************************************************/ 540 ticker.init = function() { 541 ticker.settings = $.extend({}, defaults, options); 542 543 //set ticker positions 544 initializeSettings();
545 546 //adding effect type class 547 $element.addClass('bn-effect-'+ticker.settings.effect+' bn-direction-'+ticker.settings.direction); 548 549 setContainerWidth(); 550 551 //if external data, load first 552 if (typeof ticker.settings.source === 'object'){ 553 switch (ticker.settings.source.type){ 554 case 'rss': 555 if (ticker.settings.source.usingApi === 'rss2json') 556 loadDataWithRSS2JSON(); 557 else 558 loadDataWithYQL(); 559 break; 560 case 'json': 561 loadJSON(); 562 break; 563 default: 564 console.log('Please check your "source" object parameter. Incorrect Value'); 565 } 566 } 567 else if (ticker.settings.source === 'html'){ 568 if (ticker.settings.effect != 'scroll') 569 showThis(); 570 571 playHandler(); 572 } 573 else{ 574 console.log('Please check your "source" parameter. Incorrect Value'); 575 } 576 577 //set playing status class 578 if (!ticker.settings.play) 579 _action.find('span').removeClass('bn-pause').addClass('bn-play'); 580 else 581 _action.find('span').removeClass('bn-play').addClass('bn-pause'); 582 583 584 $element.on('mouseleave', function(e){ 585 var activePosition = $(document.elementFromPoint(e.clientX, e.clientY)).parents('.bn-breaking-news')[0]; 586 if ($(this)[0] === activePosition) { 587 return; 588 } 589 590 591 if (ticker.settings.stopOnHover === true) 592 { 593 if (ticker.settings.play === true) 594 ticker.play(); 595 } 596 else 597 { 598 if (ticker.settings.play === true && _pause === true) 599 ticker.play(); 600 } 601 602 }); 603 604 $element.on('mouseenter', function(){ 605 if (ticker.settings.stopOnHover === true) 606 ticker.pause(); 607 }); 608 609 _next.on('click', function(){ 610 if (_controlsIsActive){ 611 _controlsIsActive = false; 612 ticker.pause(); 613 ticker.next(); 614 } 615 }); 616 617 _prev.on('click', function(){ 618 if (_controlsIsActive){ 619 _controlsIsActive = false; 620 ticker.pause(); 621 ticker.prev(); 622 } 623 }); 624 625 _action.on('click', function(){ 626 if (_controlsIsActive){ 627 if (_action.find('span').hasClass('bn-pause')) 628 { 629 _action.find('span').removeClass('bn-pause').addClass('bn-play'); 630 ticker.stop(); 631 } 632 else 633 { 634 ticker.settings.play = true; 635 _action.find('span').removeClass('bn-play').addClass('bn-pause'); 636 //_pause = false; 637 } 638 } 639 }); 640 641 $(window).on('resize', function(){ 642 if ($element.width() < 480){ 643 _label.hide(); 644 if (ticker.settings.direction == 'rtl') 645 _news.css({"right":0}); 646 else 647 _news.css({"left":0}); 648 } 649 else{ 650 _label.show(); 651 if (ticker.settings.direction == 'rtl') 652 _news.css({"right":_label.outerWidth()}); 653 else 654 _news.css({"left":_label.outerWidth()}); 655 } 656 }); 657 658 } 659 660 ticker.pause = function() { 661 _pause = true; 662 clearInterval(_interval); 663 } 664 665 ticker.stop = function() { 666 _pause = true; 667 ticker.settings.play = false; 668 } 669 670 ticker.play = function() {
671 playHandler(); 672 } 673 674 ticker.next = function() { 675 nextHandler(); 676 } 677 678 ticker.prev = function() { 679 prevHandler(); 680 } 681 /****************************************************/ 682 /****************************************************/ 683 /****************************************************/ 684 ticker.init(); 685 686 } 687 688 $.fn.breakingNews = function(options) { 689 690 return this.each(function() { 691 if (undefined == $(this).data('breakingNews')) { 692 var ticker = new $.breakingNews(this, options); 693 $(this).data('breakingNews', ticker); 694 } 695 }); 696 697 } 698 699})(jQuery); 700 701 702 703//http://stefangabos.ro/jquery/jquery-ticker-boilerplate-revisited/
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.