Вход Регистрация
Файл: public/js/ips.poll.js
Строк: 324
<?php
/************************************************/
/* IPB3 Javascript                                */
/* -------------------------------------------- */
/* ips.poll.js - Poll code                        */
/* (c) IPS, Inc 2008                            */
/* -------------------------------------------- */
/* Author: Rikki Tissier                        */
/************************************************/

var _poll window.IPBoard;

_poll.prototype.poll = {
    
maxQuestions0,
    
maxChoices0,
    
    
questions$H(),
    
choices$H(),
    
_choices$H(),
    
votes$H(),
    
multi$H(),
    
popupfalse,
    
    
effectDuration0.3,
    
    
/*------------------------------*/
    /* Constructor                     */
    
init: function()
    {
        
Debug.write("Initializing ips.poll.js");
        
        
document.observe("dom:loaded", function(){
            
ipb.poll.initPoll();
        });
    },
    
    
/* ------------------------------ */
    /**
     * Initialize the poll, mainly to check
     * for an existing poll
    */
    
initPoll: function()
    {
        
ipb.poll.popup = new ipb.Popup'poll_popup', {    type'pane',
                                                        
initial: $('poll_wrap'),
                                                        
modaltrue,
                                                        
hideAtStarttrue,
                                                        
hideClosetrue,
                                                        
deferfalse,
                                                        
w'700px',
                                                        
h300
                                                     
});
                                                    
        
//if( $('check_public_poll') ){ $('check_public_poll').tooltip(); }
        //if( $('check_poll_only') ){ $('check_poll_only').tooltip(); }
        
        // Are there any questions so far?
        
if( ipb.poll.questions.size() > )
        {
            
// Build the choices array
            
ipb.poll.choices.each( function( )
            {
                var 
match c.key.match(/([0-9]+)_([0-9]+)/);
                
                if( 
Object.isUndefinedmatch[1] ) || Object.isUndefinedmatch[2] ) ){
                    return 
false;
                }
                
                if( !
Object.isHashipb.poll._choices.getmatch[1] ) ) ){
                    
ipb.poll._choices.setparseIntmatch[1] ), $H() );
                }
                
                var 
votes ipb.poll.votes.getmatch[0] ) || 0;
                
                
ipb.poll._choices.getparseIntmatch[1] ) ).setparseIntmatch[2] ), $H({ valuec.valuevotesvotes }) );
            });
            
            
// Now parse questions
            
ipb.poll.questions.each( function( )
            {
                var 
qid q.key;
                var 
question q.value;
                
                
// Get the choices
                
                
var html ipb.templates['poll_question'].evaluate( { qidqidvaluequestion } );
                $( 
'poll_container' ).inserthtml );
                
                
Debug.write( $('poll_container').innerHTML );
                
                
// Allowing multiple choice?
                
if( ipb.poll.multi.getqid ) == ){
                    $(
'multi_' qid).checked true;
                }
                
                var 
choices ipb.poll._choices.getqid );
                            
                
choices.each( function( )
                {
                    var 
item ipb.templates['poll_choice'].evaluate( { qidqidcidc.keychoicec.value.get('value'), votesc.value.get('votes') } );
                    
                    $(
'choices_for_' qid).insertitem );
                    
                    
// Add Events
                    
$('remove_' qid '_' c.key).observe('click'ipb.poll.removeChoice.bindAsEventListenerthisqidc.key ));
                    
                    if( !
ipb.poll.isMod || ipb.poll.isPublicPoll ){
                        $(
'poll_' qid '_' c.key '_votes').remove();
                    }
                    
                    
// And display
                    
$('poll_' qid '_' c.key '_wrap').show();
                });
                
                
// Add events on the question wrap
                
if( $('add_choice_' qid ) )
                {
                    if( 
choices.size() >= ipb.poll.maxChoices ){
                        $(
'add_choice_' qid).hide();
                    } else {
                        $(
'add_choice_' qid).observe('click'ipb.poll.addChoice.bindAsEventListenerthisqid ) );
                    }
                }
                
                if( $(
'remove_question_' qid) )
                {
                    $(
'remove_question_' qid).observe('click'ipb.poll.removeQuestion.bindAsEventListenerthisqid ) );
                }
                
                $(
'question_' qid '_wrap').show();
            });
        }
        
        
// Update our text
        
ipb.poll.updateStatus();
        
        if( $(
'add_new_question') ){
            $(
'add_new_question').on('click'ipb.poll.addQuestion );
        }
        
        $(
'add_poll').observe('click'ipb.poll.toggleForm);
        $(
'close_poll').observe('click'ipb.poll.toggleForm);
        
        
// We need to hook an event onto the form for this poll,
        // so we can make sure they have entered enough choices
        
var form = $('add_poll').up('form');
        
        if( !
Object.isUndefinedform ) ){
            $( 
form ).observe('submit'ipb.poll.submitCheckPoll );
        }
    },
    
    
/* ------------------------------ */
    /**
     * Checks poll pre-submit to ensure proper choices
     * are entered.
     * 
     * @param    {event}        e        The event
    */
    
submitCheckPoll: function(e)
    {
        var 
stop  false;
        var 
reason 'choices';
        
        if( 
ipb.poll._choices.size() > )
        {
            
ipb.poll._choices.each( function( ){
                if( 
q.value.size() < ){
                    
stop true;
                    return;
                }
                
                
// Bug #31129 - check for filled values
                
var filled_values q.value.select( function( ){
                    if( $(
'poll_' q.key '_' c.key) ){
                        return !$(
'poll_' q.key '_' c.key).value.empty();
                    }
                });
                
                if( 
filled_values.length ){
                    
stop true;
                    return;
                }
                
                if ( ! $(
'question_' q.key '_wrap').down('input.input_text').value )
                {
                    
stop   true;
                    
reason 'questions';
                    return;
                }
            });

            if( 
stop == true )
            {
                
ipb.poll.popup.show();
                
alert( ( reason == 'choices' ) ? ipb.lang['poll_not_enough_choices'] : ipb.lang['poll_questions_missing'] );
                
Event.stop(e);
                return 
false;
            }
        }
        
        
// Move the popup contents back into the form
        
try {
            $(
'poll_wrap').hide();
            $(
'add_poll').insert({ after: $('poll_wrap') });
        } catch(
err){ }
    },
    
    
/* ------------------------------ */
    /**
     * Updates the status text
    */
    
updateStatus: function()
    {
        if( !$(
'poll_stats') ){ return; }
        if( !
ipb.lang['poll_stats'] ){ $('poll_stats').remove(); }
        
        var 
questions parseIntipb.poll.maxQuestions ) - ipb.poll._choices.size();
        var 
choices parseIntipb.poll.maxChoices );
        
        var 
string ipb.lang['poll_stats'].gsub(/[q]/, questions).gsub(/[c]/, choices);
        
        $(
'poll_stats').update( string );        
    },
        
    
/* ------------------------------ */
    /**
     * Toggle the poll form
    */
    
toggleForm: function(e)
    {
        
Event.stop(e);
        
        if( 
ipb.poll.popup.getObj().visible() ){
            
ipb.poll.popup.hide();
        } else {
            
// Add a default question
            
if( ipb.poll._choices.size() == ){
                
ipb.poll.addQuestione);
            }
            
            
ipb.poll.popup.show();
        }
    },
    
    
/* ------------------------------ */
    /**
     * Destroy poll
    */
    
destroyPoll: function( )
    {
        
//ipb.poll.removeQuestion
    
},
    
    
    
/* ------------------------------ */
    /**
     * Add a new choice
     * 
     * @param    {event}        event        The event
     * @param    {int}        qid            The question ID
     * @param    {boolean}    instant        Slide down or show instantly?
    */
    
addChoice: function(eqidinstant)
    {
        
Event.stop(e);
        
        if( !
qid || !$('choices_for_' qid) ){ return; }
        
        var 
newid ipb.poll.getNextID'c'qid );
        
        if( 
ipb.poll._choices.getqid ).size() >= ipb.poll.maxChoices ){
            
alertipb.lang['poll_no_more_choices'] );
            return;
        }
        
        var 
choice ipb.templates['poll_choice'].evaluate( { qidqidcidnewidchoice''votes} );
        $(
'choices_for_' qid ).insertchoice );
        
        
// Remove votes
        
if( !ipb.poll.isMod || ipb.poll.isPublicPoll )
        {
            $(
'poll_' qid '_' newid '_votes').remove();
        }
        
        
// Time to show
        
if( instant ){
            $(
'poll_' qid '_' newid '_wrap').show();
        } else {
            new 
Effect.BlindDown( $('poll_' qid '_' newid '_wrap'), { durationipb.poll.effectDuration } );
        }
        
        
// Add event
        
if( $('remove_' qid '_' newid) ){
            $(
'remove_' qid '_' newid ).observe('click'ipb.poll.removeChoice.bindAsEventListenerthisqidnewid ) );
        }
        
        
// Add to array
        
ipb.poll._choices.getqid ).setnewid$H({ value''votes}) );
        
        
ipb.poll.updateStatus();
    },
    
    
/* ------------------------------ */
    /**
     * Removes a choice
     * 
     * @param    {event}        e        The event
     * @param    {int}        qid        The question ID
     * @param    {int}        cid        The choice ID    
    */
    
removeChoice: function(eqidcid)
    {
        
Event.stop(e);
        
        if( !
qid || Object.isUndefinedcid ) || !$('poll_' qid '_' cid) ){ return; }
        
        
// If theres a value, check they want to delete
        
if( !$F('poll_' qid '_' cid).blank() )
        {
            if( !
confirmipb.lang['delete_confirm'] ) )
            {
                return;
            }
        }            
        
        
// Hide it
        
new Effect.BlindUp( $('poll_' qid '_' cid '_wrap' ), { durationipb.poll.effectDurationafterFinish: function(){ $('poll_' qid '_' cid '_wrap').remove(); } } );
        
        
// remove it from array
        
ipb.poll._choices.getqid ).unset( cid );
        
        
ipb.poll.updateStatus();
    },
    
    
/* ------------------------------ */
    /**
     * Add a new question
     * 
     * @param    {event}        e            The event
     * @param    {boolean}    instant        Show instantly?
    */
    
addQuestion: function(einstant)
    {
        
Debug.write("Here");
        
Event.stop(e);
        
        if( 
ipb.poll._choices.size() >= ipb.poll.maxQuestions ){
            
alertipb.lang['poll_no_more_q'] );
            return;
        }
        
        var 
newid ipb.poll.getNextID('q');

        var 
item ipb.templates['poll_question'].evaluate( { qidnewidvalue'' } );
        $( 
'poll_container' ).insertitem );
        
        if( $(
'remove_question_' newid) ){
            $(
'remove_question_' newid).observe('click'ipb.poll.removeQuestion.bindAsEventListenerthisnewid ) );
        }
        
        
// Show it
        
if( instant ){
            $(
'question_' newid '_wrap').show();
        } else {
            new 
Effect.BlindDown( $('question_' newid '_wrap'), { durationipb.poll.effectDuration } );
        }
        
        
// Add it to array
        
ipb.poll._choices.setnewid$H() );
        
        
// Lets add a choice to start them off
        
ipb.poll.addChoice(enewid1);
        
        
// Add events on the question wrap
        
if( $('add_choice_' newid ) )
        {
            $(
'add_choice_' newid).observe('click'ipb.poll.addChoice.bindAsEventListenerthisnewid ) );
        }
        
        
ipb.poll.updateStatus();
    },
    
    
/* ------------------------------ */
    /**
     * Removes a question
     * 
     * @param    {event}        e        The event
     * @param    {int}        qid        
    */
    
removeQuestion: function(eqidforce)
    {
        
Event.stop(e);
        
        if( !$(
'question_' qid '_wrap') ){ return; }
        
        if( !
force )
        {
            
// Confirm it
             
if( !confirmipb.lang['delete_confirm'] ) )
            {
                return;
            }
        
            
// Well, ok then...
            
new Effect.BlindUp( $('question_' qid '_wrap'), { durationipb.poll.effectDurationafterFinish: function(){
                $(
'question_' qid '_wrap').remove();
            } } );
        }
        else
        {
            $(
'question_' qid '_wrap').remove();
        }
        
        
// Remove from array
        
ipb.poll._choices.unset( qid );
        
        
ipb.poll.updateStatus();
    },
    
    
/* ------------------------------ */
    /**
     * Returns the next highest ID
     * 
     * @param    {string}    type        Type of ID, either q for question or c for choice
     * @param    {int}        qid            Question ID if type is c
    */
    
getNextID: function( typeqid )
    {
        
//Debug.dir( ipb.poll._choices );
        
        
if( type == 'q' )
        {
            if( 
Object.isUndefinedipb.poll._choices ) ){
                var 
max 1;
            }
            else
            {
                var 
max parseIntipb.poll._choices.max( function(q){
                        return 
parseIntq.key );
                    }) ) + 
1;
                
                if ( 
isNaNmax ) )
                {
                    var 
max 1;
                }
            }
        }
        else
        {
            if( 
Object.isUndefinedqid ) ){ return false; }
            
            if( 
Object.isUndefinedipb.poll._choices.getqid ) ) ){
                var 
max 0;
            }
            else
            {
                var 
max parseIntipb.poll._choices.getqid ).max( function(c){
                    return 
parseIntc.key );
                }) ) + 
1;
                
                if( 
isNaNmax ) ){
                    
max 1;
                }
            }
        }
        
        
Debug.writemax );
        return 
max;
    }
};

ipb.poll.init();
?>
Онлайн: 1
Реклама