Sha256: f8129509280dc1f68d7627a3b0d3780d42433b502dc9dfe4c9fc10563147efbb

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

"use strict";

var ItemQuantityPicker = function (wrapper){

    var _$wrapper;
    var _$less, _$great, _$input;
    var _cur_val;
    var __callbacks;

    var _on_change = function(func) {
        if (__callbacks === null) return;
        __callbacks.push(func);
    };

    var _call_on_change = function() {
        if (__callbacks === null) return;

        for (var i = 0; i < __callbacks.length; i++) {
            if (__callbacks[i] !== undefined) {
                __callbacks[i](_get_cur_val());
            }
        }
    };

    var _set_cur_val = function (new_val){
        if (new_val !== _cur_val) {
            if (new_val === 0) {
                _cur_val = 1;
            } else {
                _cur_val = new_val;
                _call_on_change();
            }
        }
    };

    var _get_cur_val = function() {
        return _cur_val;
    };

    var _onClickLess = function(e) {
        console.log('<_onClickLess>');
        _set_cur_val(_cur_val - 1);
        _afterClick();
        e.preventDefault();
    };

    var _onClickGreat = function(e) {
        console.log('<_onClickGreat>');
        _set_cur_val(Number(_cur_val) + 1);
        _afterClick();
        e.preventDefault();
    };

    var _afterClick = function() {
        _$input.val(_cur_val);
    };

    var _onChangeInput = function(e) {
        console.log('<_onChangeInput>');
    };

    var _fInitBehaviour = function (){
        _$less.on('click', _onClickLess);
        _$great.on('click', _onClickGreat);
        _$input.on('change', _onChangeInput);
    };

    var _fInit = function (wrapper){

        _$wrapper = $(wrapper);
        _$less = _$wrapper.find('a.l');
        _$great = _$wrapper.find('a.g');
        _$input = _$wrapper.find('input');
        _cur_val = _$input.val();
        __callbacks = [];

        _fInitBehaviour();

    };

    _fInit(wrapper);

    return {
        get_cur_val: _get_cur_val,
        on_change: _on_change
    }

};

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
c80_yax-0.1.0.20 app/assets/javascripts/c80_yax/frontend/item_quantity_picker/item_quantity_picker.js