1(function (global) { 2 3 function FavoriteWidget(args) { 4 let $this = this; 5 this.args = args; 6 this.widget = $(this.args.selector); 7 8 this.has(); 9 10 this.widget.on('click', function () { 11 let $singleWidget = $(this); 12 if ($singleWidget.hasClass('added')) { 13 $this.remove($singleWidget); 14 } else { 15 $this.add($singleWidget); 16 } 17 18 }); 19 20 return this; 21 } 22 23 FavoriteWidget.prototype.getTextWrapper = function(widget) 24 { 25 return (this.args.wrapperSelector !== undefined) 26 ? widget.find(this.args.wrapperSelector) 27 : widget; 28 }; 29 30 FavoriteWidget.prototype.has = function () { 31 let $this = this; 32 33 this.widget.each(function (index, singleWidget) { 34 let $singleWidget = $(singleWidget); 35 let url = $singleWidget.attr('data-has-url'); 36 37 $.ajax({ 38 url: url, 39 cache: false, 40 success: function (result) { 41 if (result.added) { 42 $singleWidget.addClass('added'); 43 $singleWidget.prop('title', $singleWidget.attr('data-subject-added')); 44 $this.getTextWrapper($singleWidget).text($singleWidget.attr('data-subject-remove')); 45 } 46 47 if (!result.canAddMore && !result.added) { 48 $singleWidget.prop('disabled', 'disabled'); 49 $singleWidget.find('.error').removeClass('is-hidden'); 50 } 51 } 52 }); 53 }); 54 }; 55 56 FavoriteWidget.prototype.add = function ($singleWidget) { 57 $singleWidget.addClass('added'); 58 $singleWidget.prop('title', $singleWidget.attr('data-subject-added')); 59 this.getTextWrapper($singleWidget).text($singleWidget.attr('data-subject-remove')); 60 let url = $singleWidget.attr('data-add-url'); 61 this.ajaxCall(url); 62 return this; 63 }; 64 65 FavoriteWidget.prototype.remove = function ($singleWidget) { 66 $singleWidget.removeClass('added'); 67 $singleWidget.prop('title', $singleWidget.attr('data-subject-removed')); 68 this.getTextWrapper($singleWidget).text($singleWidget.attr('data-subject-add')); 69 let url = $singleWidget.attr('data-remove-url'); 70 this.ajaxCall(url); 71 return this; 72 }; 73 74 FavoriteWidget.prototype.ajaxCall = function (url) { 75 let $this = this; 76 $.ajax({ 77 method: 'POST', 78 url: url, 79 cache: false, 80 success: function (result) { 81 console.log(result); 82 }, 83 error: function (jqXHR, textStatus, errorThrow) { 84 console.log(jqXHR, textStatus, errorThrow); 85 } 86 }); 87 }; 88 89 let favoriteWidget = function (args) { 90 return new FavoriteWidget(args); 91 }; 92 93 global.favoriteWidget = favoriteWidget; 94 95})(this);
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.