All files / components/textfieldControl/js TextfieldStepper.js

100% Statements 13/13
83.33% Branches 5/6
100% Functions 4/4
100% Lines 13/13
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266                        24x   24x             24x                                                                                                                                                                                                                                                                                                                                                                     24x 12x 12x 12x     24x 418x 12x 6x 6x         24x                                                                                                    
/*
Copyright 2016 Stanislav Shterev
Copyright 2017 OCAD University
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    /*********************
     * Textfield Stepper *
     *********************/
 
    fluid.defaults("fluid.textfieldStepper", {
        gradeNames: ["fluid.viewComponent"],
        strings: {
            // Specified by implementor
            // text of label to apply to both textfield and control
            // via aria-label attribute
            // "aria-label": "",
            increaseLabel: "increment",
            decreaseLabel: "decrement"
        },
        selectors: {
            textfield: ".flc-textfieldStepper-field",
            focusContainer: ".flc-textfieldStepper-focusContainer",
            increaseButton: ".flc-textfieldStepper-increase",
            decreaseButton: ".flc-textfieldStepper-decrease"
        },
        styles: {
            container: "fl-textfieldStepper",
            focus: "fl-textfieldStepper-focus"
        },
        components: {
            textfield: {
                type: "fluid.textfield.rangeController",
                container: "{that}.dom.textfield",
                options: {
                    components: {
                        controller: {
                            options: {
                                model: "{textfieldStepper}.model",
                                modelListeners: {
                                    "range.min": {
                                        "this": "{textfield}.container",
                                        method: "attr",
                                        args: ["aria-valuemin", "{change}.value"]
                                    },
                                    "range.max": {
                                        "this": "{textfield}.container",
                                        method: "attr",
                                        args: ["aria-valuemax", "{change}.value"]
                                    }
                                }
                            }
                        }
                    },
                    attrs: "{textfieldStepper}.options.attrs",
                    strings: "{textfieldStepper}.options.strings",
                    listeners: {
                        "onCreate.bindUpArrow": {
                            listener: "fluid.textfieldStepper.bindKeyEvent",
                            // up arrow === 38
                            args: ["{that}.container", "keydown", 38, "{textfieldStepper}.increase"]
                        },
                        "onCreate.bindDownArrow": {
                            listener: "fluid.textfieldStepper.bindKeyEvent",
                            // down arrow === 40
                            args: ["{that}.container", "keydown", 40, "{textfieldStepper}.decrease"]
                        },
                        "onCreate.addRole": {
                            "this": "{that}.container",
                            method: "attr",
                            args: ["role", "spinbutton"]
                        }
                    },
                    modelListeners: {
                        "value": {
                            "this": "{that}.container",
                            method: "attr",
                            args: ["aria-valuenow", "{change}.value"]
                        }
                    }
                }
            },
            increaseButton: {
                type: "fluid.textfieldStepper.button",
                container: "{textfieldStepper}.dom.increaseButton",
                options: {
                    strings: {
                        label: "{textfieldStepper}.options.strings.increaseLabel"
                    },
                    listeners: {
                        "onClick.increase": "{textfieldStepper}.increase"
                    },
                    modelRelay: {
                        target: "disabled",
                        singleTransform: {
                            type: "fluid.transforms.binaryOp",
                            left: "{textfieldStepper}.model.value",
                            right: "{textfieldStepper}.model.range.max",
                            operator: ">="
                        }
                    }
                }
            },
            decreaseButton: {
                type: "fluid.textfieldStepper.button",
                container: "{textfieldStepper}.dom.decreaseButton",
                options: {
                    strings: {
                        label: "{textfieldStepper}.options.strings.decreaseLabel"
                    },
                    listeners: {
                        "onClick.decrease": "{textfieldStepper}.decrease"
                    },
                    modelRelay: {
                        target: "disabled",
                        singleTransform: {
                            type: "fluid.transforms.binaryOp",
                            left: "{textfieldStepper}.model.value",
                            right: "{textfieldStepper}.model.range.min",
                            operator: "<="
                        }
                    }
                }
            }
        },
        invokers: {
            increase: {
                funcName: "fluid.textfieldStepper.step",
                args: ["{that}"]
            },
            decrease: {
                funcName: "fluid.textfieldStepper.step",
                args: ["{that}", -1]
            },
            addFocus: {
                "this": "{that}.dom.focusContainer",
                method: "addClass",
                args: ["{that}.options.styles.focus"]
            },
            removeFocus: {
                "this": "{that}.dom.focusContainer",
                method: "removeClass",
                args: ["{that}.options.styles.focus"]
            }
        },
        listeners: {
            "onCreate.addContainerStyle": {
                "this": "{that}.container",
                method: "addClass",
                args: ["{that}.options.styles.container"]
            },
            "onCreate.bindFocusin": {
                "this": "{that}.container",
                method: "on",
                args: ["focusin", "{that}.addFocus"]
            },
            "onCreate.bindFocusout": {
                "this": "{that}.container",
                method: "on",
                args: ["focusout", "{that}.removeFocus"]
            }
        },
        model: {
            value: null,
            step: 1,
            range: {
                min: 0,
                max: 100
            }
        },
        attrs: {
            // Specified by implementor
            // ID of an element to use as a label for the stepper
            // attribute
            // "aria-labelledby": ""
            // Should specify either "aria-label" or "aria-labelledby"
            // aria-label: "{that}.options.strings.label",
            // ID of an element that is controlled by the textfield.
            // "aria-controls": ""
        },
        distributeOptions: [{
            // The scale option sets the number of decimal places to round
            // the number to. If no scale is specified, the number will not be rounded.
            // Scaling is useful to avoid long decimal places due to floating point imprecision.
            source: "{that}.options.scale",
            target: "{that > fluid.textfield > controller}.options.scale"
        }]
    });
 
    fluid.textfieldStepper.step = function (that, coefficient) {
        coefficient = coefficient || 1;
        var newValue = that.model.value + (coefficient * that.model.step);
        that.applier.change("value", newValue);
    };
 
    fluid.textfieldStepper.bindKeyEvent = function (elm, keyEvent, keyCode, fn) {
        $(elm).on(keyEvent, function (event) {
            if (event.which === keyCode) {
                fn();
                event.preventDefault();
            }
        });
    };
 
    fluid.defaults("fluid.textfieldStepper.button", {
        gradeNames: ["fluid.viewComponent"],
        strings: {
            // to be specified by an implementor.
            // to provide a label for the button.
            // label: ""
        },
        styles: {
            container: "fl-textfieldStepper-button"
        },
        model: {
            disabled: false
        },
        events: {
            onClick: null
        },
        listeners: {
            "onCreate.bindClick": {
                "this": "{that}.container",
                "method": "click",
                "args": "{that}.events.onClick.fire"
            },
            "onCreate.addLabel": {
                "this": "{that}.container",
                method: "attr",
                args: ["aria-label", "{that}.options.strings.label"]
            },
            "onCreate.addContainerStyle": {
                "this": "{that}.container",
                method: "addClass",
                args: ["{that}.options.styles.container"]
            },
            // removing from tab order as keyboard users will
            // increment and decrement the stepper using the up/down arrow keys.
            "onCreate.removeFromTabOrder": {
                "this": "{that}.container",
                method: "attr",
                args: ["tabindex", "-1"]
            }
        },
        modelListeners: {
            disabled: {
                "this": "{that}.container",
                method: "prop",
                args: ["disabled", "{change}.value"]
            }
        }
    });
 
})(jQuery, fluid_3_0_0);