Greg Ostry
Greg Ostry

Reputation: 1231

How to give parameters to a function in object literal patern

I'm trying to solve my problem with object literal pattern.

Here is my jsFiddle: https://jsfiddle.net/1w8Lcdwk/

I have an accordion and when i select on the first panel the 4 and 5 option in select element it disables me the panel 2 and panel 3. This is working. Now i have to change the button functionality. When a panel is disabled it should go to the next active(not disabled) panel.

How can i this.controllTab(event, $next=3, $previous=3); give the function these parameters without to call the accordion function. The function should be called only when i hit the next button. I'm changing the $next: $previous steps from 1 to 3. But this is not working.

var bestellvorgang = {
    $accordion:null,
init: function() {
        this.cacheDom();
        this.bindEvent();
        $accordion.accordion();
    },
cacheDom: function() {
        $accordion = $('#accordion');
        $btnTabControl = $accordion.find('button[name=tab-control]');
        $productSelect = $accordion.find('#productSelect');
    },
bindEvent: function() {
        $btnTabControl.on('click', this.controllTab.bind(this));
        $productSelect.on('change', this.productSelect.bind(this));
    },
controllTab: function(event, $next=1, $previous=1) {
        event.preventDefault();
        var delta = $(event.currentTarget).is('.next') ? $next:-$previous;
        $accordion.accordion('option', 'active', ($accordion.accordion('option', 'active')) + delta);
    },
productSelect: function(event) {
        $selected = event.currentTarget.value;
        switch($selected) {
          case 'p4':
            $('#ui-id-3, #ui-id-5').addClass('ui-state-disabled');
            this.controllTab(event, $next=3, $previous=3);
          break;
          case 'p5':
            $('#ui-id-3, #ui-id-5').addClass('ui-state-disabled');
          break;
          default:
            $('#ui-id-3, #ui-id-5').removeClass('ui-state-disabled');
        }
   }
};


$(document).ready(function(){
    bestellvorgang.init();
});

Upvotes: 0

Views: 54

Answers (2)

gaetanoM
gaetanoM

Reputation: 42054

When a panel is disabled it should go to the next active(not disabled) panel.

Because you change the active panel in:

controllTab: function(event, $next=1, $previous=1) {

it's enough to add at the end a test in order to move to the next active panel if the active one is disabled:

controllTab: function(event, $next=1, $previous=1) {
    event.preventDefault();
    var delta = $(event.currentTarget).is('.next') ? $next : -$previous;
    $accordion.accordion('option', 'active', 
             ($accordion.accordion('option', 'active')) + delta);

    var next = 'gt';
    var prev = 'lt';
    if (event.target.textContent.trim().toLowerCase() == 'previous') {
        next = 'lt';
        prev = 'gt';
    }
    var activePanel = $accordion.accordion('option', 'active');
    if ($accordion.children('h3.ui-accordion-header')
                 .eq(activePanel).is('.ui-state-disabled')) {
        var x = $accordion.children('h3.ui-accordion-header:not(ui-state-disabled):'
                   + next + '(' + activePanel + '):first');
        if (x.length == 0) {
            x = $accordion.children('h3.ui-accordion-header:not(ui-state-disabled):' 
                 + prev + '(' + activePanel + '):last');
        }
        $accordion.accordion('option', 'active', (x.index() > 0) ?  x.index() - 1 : x.index());
    }
},

var bestellvorgang = {
    $accordion: null,
    init: function () {
        this.cacheDom();
        this.bindEvent();
        $accordion.accordion();
    },
    cacheDom: function () {
        $accordion = $('#accordion');
        $btnTabControl = $accordion.find('button[name=tab-control]');
        $productSelect = $accordion.find('#productSelect');
    },
    bindEvent: function () {
        $btnTabControl.on('click', this.controllTab.bind(this));
        $productSelect.on('change', this.productSelect.bind(this));
    },
    controllTab: function(event, $next=1, $previous=1) {
        event.preventDefault();
        var delta = $(event.currentTarget).is('.next') ? $next : -$previous;
        $accordion.accordion('option', 'active', ($accordion.accordion('option', 'active')) + delta);

        var next = 'gt';
        var prev = 'lt';
        if (event.target.textContent.trim().toLowerCase() == 'previous') {
            next = 'lt';
            prev = 'gt';
        }
        var activePanel = $accordion.accordion('option', 'active');
        if ($accordion.children('h3.ui-accordion-header').eq(activePanel).is('.ui-state-disabled')) {
            var x = $accordion.children('h3.ui-accordion-header:not(ui-state-disabled):' + next + '(' + activePanel + '):first');
            if (x.length == 0) {
                x = $accordion.children('h3.ui-accordion-header:not(ui-state-disabled):' + prev + '(' + activePanel + '):last');
            }
            $accordion.accordion('option', 'active', (x.index() > 0) ?  x.index() - 1 : x.index());
        }
    },
    productSelect: function (event) {
        $selected = event.currentTarget.value;
        switch ($selected) {
            case 'p4':
                $('#ui-id-3, #ui-id-5').addClass('ui-state-disabled');
                this.controllTab(event, $next=3, $previous=3);
                break;
            case 'p5':
                $('#ui-id-3, #ui-id-5').addClass('ui-state-disabled');
                break;
            default:
                $('#ui-id-3, #ui-id-5').removeClass('ui-state-disabled');
        }
    }
};
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>


<div id="accordion">
    <h3>Section 1</h3>

    <div>
        <p>
            Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
            ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
            amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
            odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
        </p>
        <label for="productSelect">Choose a Product:</label>
        <select id="productSelect">
            <option value="p1">Product 1</option>
            <option value="p2">Product 2</option>
            <option value="p3">Product 3</option>
            <option value="p4">Product 4</option>
            <option value="p5">Product 5</option>
        </select>
        <br><br>
        <button type="button" name="tab-control" class="btn btn-success pull-right next">Next <span
                class="glyphicon glyphicon-circle-arrow-down" aria-hidden="true"></span></button>
    </div>
    <h3>Section 2</h3>

    <div>
        <button type="button" name="tab-control" class="btn btn-info pull-right previous">Previous <span
                class="glyphicon glyphicon-circle-arrow-up" aria-hidden="true"></span></button>
        <p>
            Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
            purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
            velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
            suscipit faucibus urna.
        </p>
        <button type="button" name="tab-control" class="btn btn-success pull-right next">Next <span
                class="glyphicon glyphicon-circle-arrow-down" aria-hidden="true"></span></button>
    </div>
    <h3>Section 3</h3>

    <div>
        <button type="button" name="tab-control" class="btn btn-info pull-right previous">Previous <span
                class="glyphicon glyphicon-circle-arrow-up" aria-hidden="true"></span></button>
        <p>
            Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
            Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
            ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
            lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
        </p>
        <button type="button" name="tab-control" class="btn btn-success pull-right next">Next <span
                class="glyphicon glyphicon-circle-arrow-down" aria-hidden="true"></span></button>
    </div>
    <h3>Section 4</h3>

    <div>
        <button type="button" name="tab-control" class="btn btn-info pull-right previous">Previous <span
                class="glyphicon glyphicon-circle-arrow-up" aria-hidden="true"></span></button>
        <p>
            Cras dictum. Pellentesque habitant morbi tristique senectus et netus
            et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
            faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
            mauris vel est.
        </p>
    </div>
</div>

Upvotes: 0

vatz88
vatz88

Reputation: 2452

Changed

this.controllTab(event, $next=3, $previous=3);

to

this.controllTab(event, 4, 1);

Seems to be working. See here - https://jsfiddle.net/VaTz88/1w8Lcdwk/1/

Upvotes: 0

Related Questions