PageSourceSearch

https://cdnn.1prime.ru//js/libs/ria.vote.js

js 1prime.ru collected 2026-10-01 08:34:02 UTC 6,857 bytes, 168 lines download raw bytes

1;(function ( $, window, document, undefined ) {
2
3    var riaVote = function(el, options) {
4        var defaults = {};
5        var _this = this;
6        _this.el = $(el);
7        _this.btn = _this.el.find('button');
8        _this.id = _this.el.data('vote-id').toString();
9        _this.defOptions = defaults;
10        _this.userOptions = options;
11        _this.options = $.extend(true, {}, defaults, options);
12        _this.init();
13    };
14
15    riaVote.prototype.init = function() {
16        var _this = this;
17
18        // грузим результаты если на голосовалке класс m-vote-disabled или в локалсторе есть идентификатор что голосовали
19        if ( _this.el.hasClass('m-vote-disabled') || _this.checkVoted( _this.id ) ) {
20            $.ajax({
21                url: '/services/vote/get_result/',
22                dataType: 'json',
23                data: {
24                    "vote_id": _this.id
25                }
26            }).done( function( response ) {
27                _this.showResults( response, true );
28            }).error( function(){
29                console.log( 'vote ajax error - cant get result /services/vote/get_result/' );
30            });
31            // _this.showResults( {}, true );
32        }
33
34        // клики по input в форме выдора варианта
35        _this.el.find('.js-vote-answer').on( 'click', function() {
36            if ( _this.el.find( '.js-vote-answer input:checked:not(:disabled)' ).length ) {
37                _this.btn.removeAttr( 'disabled' );
38            } else {
39                _this.btn.attr( 'disabled', 'disabled' );
40            }
41        });
42
43        // клик по кнопке показать результат и обратно
44        _this.el.find('.js-vote-switch-button').on( 'click', function() {
45            if ( _this.el.hasClass( 'm-vote-results' ) ) {
46                _this.el.removeClass( 'm-vote-results' );
47            } else {
48                $.ajax({
49                    url: '/services/vote/get_result/',
50                    dataType: 'json',
51                    data: {
52                        "vote_id": _this.id
53                    }
54                }).done( function( response ) {
55                    _this.showResults( response );
56                }).error( function(){
57                    console.log( 'vote ajax error - switch error /services/vote/get_result/' );
58                });
59            }
60        });
61
62        // form submit
63        _this.el.find('form').on('submit', function(e) {
64            e.preventDefault();
65            // disable на кнопку
66            _this.btn.attr( 'disabled', 'disabled' );
67            if ( !_this.checkVoted( _this.id ) ) {
68                checkCaptchaToken({
69                    callback: function(token) {
70                        $( 'input[name=g-recaptcha-response]', _this.el ).val( token );
71                        _this.onSubmitAjax();
72                    }
73                });
74            } else {
75                // если нет id на голосовалке кнопку в disabled и грузим результаты
76                _this.showResults( {}, true );
77            }
78        });
79
80    };
81
82    riaVote.prototype.onSubmitAjax = function(){
83        var _this = this;
84        var $form = _this.el.find( 'form' );
85        var formData = $form.serializeArray();
86        $.ajax({
87            url: $form.attr( 'action' ),
88            dataType: 'json',
89            data: formData,
90            type: 'POST',
91            beforeSend: function() {
92                _this.btn.attr( 'disabled', 'disabled' );
93            }
94        }).done( function( response ) {
95            _this.showResults( response, true );
96            if ( response.success == 1 ){
97                var answers = [];
98                $.each( formData, function() {
99                    if ( this.name == 'vote_answer_id[]' ) {
100                        answers.push( this.value );
101                    }
102                });
103                _this.updateVoted( _this.id, answers );
104            } else {
105                // _this.btn.removeAttr( 'disabled');
106            }
107        }).error( function(){
108            console.log( 'vote ajax error - submit error /services/vote/add/' );
109            _this.btn.removeAttr( 'disabled');
110        });
111    }
112
113    riaVote.prototype.showResults = function( response, dis ) {
114        //TODO dev test console.log( ' response = ', response );
115        var _this = this;
116        var lsd = JSON.parse( ria.lsGet( 'ria-votes_' + _this.id ) ) || []; //Получаем id ответа пользовалетя по данной голосовалке
117        _this.el.find( '.js-vote-answer' ).each( function() {
118            if ( typeof response.answers !== 'undefined' && typeof response.answers[ $( this ).data( 'id' ) ] !== 'undefined' ) {
119                var val = response.answers[ $( this ).data( 'id' ) ].percent;
120                $( this ).find( '.js-vote-line' ).width( val+'%' );
121                $( this ).find( '.js-vote-percent span' ).text( val );
122            }
123            if ( dis ) {
124                $( this ).find( 'input' ).attr( 'disabled', 'disabled' );
125                if ( lsd.indexOf( $( this ).data( 'id' ).toString() ) != -1 ) {
126                    $( this ).find( 'input' ).attr( 'checked', 'checked');
127                }
128            }
129        });
130        if ( response.total > 0 || _this.el.find( '.js-vote-count-val' ).text() != '' ) {
131            _this.el.find( '.js-vote-count-val' ).text( response.total );
132            _this.el.find( '.js-vote-count' ).removeClass( 'm-hidden' );
133        } else {
134            _this.el.find( '.js-vote-count' ).addClass( 'm-hidden' );
135        }
136        _this.el.addClass( 'm-vote-results' );
137    };
138
139    // Проверка наличия id голосовалки в локалсторе
140    riaVote.prototype.checkVoted = function( id ) {
141        var lsd = JSON.parse( ria.lsGet( '1prime.ru__ria-votes' ) ) || [];
142        return lsd.indexOf( id ) > -1;
143    };
144
145    riaVote.prototype.updateVoted = function( id, answers ) {
146        var lsd = JSON.parse( ria.lsGet( '1prime.ru__ria-votes' ) ) || [];
147        var i = lsd.indexOf( id );
148        if (i > -1 && i < lsd.length) {
149            lsd.splice(i, 1);
150        }
151        lsd.push( id );
152        if ( lsd.length > 100) {
153            var removed_id = lsd.shift();
154            ria.lsRemove( 'ria-votes_' + removed_id );
155        }
156        ria.lsSet( '1prime.ru__ria-votes', JSON.stringify( lsd ) );
157        ria.lsSet( 'ria-votes_' + id,  JSON.stringify( answers ) );
158    };
159
160    $.fn.riaVote = function( options ) {
161        return this.each( function(){
162            if( !$.data( this, 'riaVote' )){
163                $.data( this, 'riaVote', new riaVote( this, options ));
164            }
165        });
166    };
167
168})( jQuery, window, document );

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.