Skip to content
This repository
tree: 6eb970c5f9
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 210 lines (191 sloc) 9.31 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{% load i18n videos_tags %}

<button class="follow-button"></button>
  
<script type="text/javascript">
    jQuery(function($){
        $.widget("unisub.followVideoButton", {
            options: {
                videoId: null,
                languageId: null,
                languageName: null,
                isAuthenticated: false,
                isVideoFollower: false,
                isLanguageFollower: false,
                isFollower: false,
                
                followCls: 'follow',
                unfollowCls: 'unfollow',
                loginUrl: ''
              },
            _create: function() {
                var that = this;
                this.element.bind('click', function(){that._onClick.apply(that, arguments)})
                this._render();
            },
            _setFollowCls: function(){
                this.element.removeClass(this.options.unfollowCls)
                            .addClass(this.options.followCls)
            },
            _setUnfollowCls: function(){
                this.element.removeClass(this.options.followCls)
                            .addClass(this.options.unfollowCls)
            },
            _render: function(){
                if((!this.options.languageId && this.options.isVideoFollower) || (this.options.languageId && this.options.isLanguageFollower)){
                    this.options.isFollower = true;
                    this.element.html('{% trans "✓ Following"|escapejs %}');
                    this._setUnfollowCls();
                } else {
                    this.element.html('{% trans " Not Following"|escapejs %}');
                    this._setFollowCls();
                }
            },
            _onClick: function(e){
                if (!this.options.isAuthenticated){
                    this.onUnauthenticateClick();
                    return;
                };

                if(this.options.isFollower) {
                    this.showUnfollowDialog();
                } else {
                    this.showFollowDialog();
                };
                
                e.preventDefault();
            },
            _unfollowDialogCallback: function(button){
                var that = this;
                if (button === 'ALL'){
                    VideosApi.unfollow(this.options.videoId, function(response){
                        if (response.error){
                            $.jGrowl.error(response.error);
                        }else{
                            $.jGrowl(response.msg);
                            that.options.isVideoFollower = false;
                            that.options.isLanguageFollower = false;
                            that._render();
                        }
                    });
                }else if(button === 'OK' && this.options.languageId){
                    VideosApi.unfollow_language(this.options.languageId, function(response){
                        if (response.error){
                            $.jGrowl.error(response.error);
                        }else{
                            $.jGrowl(response.msg);
                            that.options.isLanguageFollower = false;
                            that._render();
                        };
                    });
                }
            },
            showUnfollowDialog: function(){
                if (this.options.isVideoFollower){
                    var title = '{% trans "You will stop receiving notifications when any subtitles for this video are edited. You can choose to follow again at any time."|escapejs %}';
                    var button_name = "{% trans 'Stop following this video'|escapejs %}";
                    
                }else{
                    var title = '{% trans "You will stop receiving notification when "|escapejs %}'
                        +this.options.languageName
                        +' {% trans " subtitles for this video are edited. You can choose to follow again at any time."|escapejs %}';
                    var button_name = "{% trans 'Stop following '|escapejs %}"
                        +this.options.languageName
                        +"{% trans ' subtitles'|escapejs %}";
                };
                
                var options = {
                    extraButtons: []
                };
                
                if (this.options.isVideoFollower){
                    options.okButton = null;
                    options.extraButtons.push({
                        name: '{% trans "Unfollow all languages"|escapejs %}',
                        id: 'unfollow-all-languages-button',
                        type: 'ALL'
                    });
                };
                
                var that = this;
                
                $.jConfirm(title, button_name, function(button){
                    that._unfollowDialogCallback(button);
                }, options);
            },
            _followDialogCallback: function(button){
                var that = this;
                if (button === 'ALL'){
                    VideosApi.follow(this.options.videoId, function(response){
                        if (response.error){
                            $.jGrowl.error(response.error);
                        }else{
                            $.jGrowl(response.msg);
                            that.options.isVideoFollower = true;
                            that._render();
                        }
                    });
                }else if(button === 'OK' && this.options.languageId){
                    VideosApi.follow_language(this.options.languageId, function(response){
                        if (response.error){
                            $.jGrowl.error(response.error);
                        }else{
                            $.jGrowl(response.msg);
                            that.options.isLanguageFollower = true;
                            that._render();
                        };
                    });
                }
            },
            showFollowDialog: function(){
                if (this.options.languageId){
                    var title = '{% trans "You will receive notifications when anyone edits "|escapejs %}'
                        +this.options.languageName
                        +' {% trans " subtitles for this video. You can stop these emails at any time."|escapejs %}';
                    var button_name = "{% trans 'Follow '|escapejs %}"
                        +this.options.languageName
                        +"{% trans ' subtitles'|escapejs %}";
                }else{
                    var title = '{% trans "You will receive notifications when anyone edits any subtitles for this video. You can stop these emails at any time."|escapejs %}';
                    var button_name = "{% trans 'Follow this video'|escapejs %}";
                };
                
                var options = {
                    extraButtons: []
                };

                options.extraButtons.push({
                    name: '{% trans "Follow all languages"|escapejs %}',
                    id: 'unfollow-all-languages-button',
                    type: 'ALL'
                });
                
                if ( ! this.options.languageId){
                    options.okButton = null;
                };
                
                var that = this;
                
                $.jConfirm(title, button_name, function(button){
                    that._followDialogCallback(button);
                }, options);
            },
            onUnauthenticateClick: function(){
                var that = this;
                $.jAlert('{% trans "You must log in to follow a video."|escapejs %}',
                         '{% trans "Authentication required"|escapejs %}',
                         function(button){
                             if (button === 'OK'){
                                 window.location.href = that.options.loginUrl;
                             }
                         });
            }
        });
        
        $('.follow-button').followVideoButton({
            videoId: '{{ video.pk }}',
            languageId: '{{ language.pk }}',
            languageName: '{{ language.language_display }}',
            isAuthenticated: {% if user.is_authenticated %} true{% else %}false{% endif %},
            isVideoFollower: {% if video|is_follower:user %}true{% else %}false{% endif %},
            isLanguageFollower: {% if language|is_follower:user %}true{% else %}false{% endif %},
            loginUrl: '{% url auth:login %}?next={{ request.path_info|urlencode }}'
        });

        $('.follow-button').each(function(){
            var orig = $(this).text();
            
            $(this).hover(function(){
                if($(this).hasClass('unfollow')){
                    $(this).text('{% trans "x Stop Following"|escapejs %}');
                } else {
                    $(this).text('{% trans "✓ Click to Follow"|escapejs %}');
                }
            }, function(){
                $(this).text(orig);
            });
        });
    });
</script>
Something went wrong with that request. Please try again.