Connect with:
About Me
Alessandro Annini
I'm a freelance developer currently working at Leaff.it/ I really love famo.us framework and its amazing potential and if you're like me, and you want the web to win, please share some snippet of code for the community and grow faster with us
Categories
Meta
Famo.us Code | Famo.us ClearInput Widget
158
post-template-default,single,single-post,postid-158,single-format-standard,ajax_updown_fade,page_not_loaded,,qode-theme-ver-6.2.1

Famo.us ClearInput Widget

19 Jun Famo.us ClearInput Widget

This widget gives you a text input with the clear button inside it.
source from my github

Example:

 

 

This is a Famo.us Input with the possibility to add a clear button (ImageSurface) inside, on near the right border.
Usage

// Your basic Famo.us setup
 var mainContext = Engine.createContext();

// Create the new field and set the 'x' image with the clearImage property
 var ci = new ClearInput({
 size:[250,35],
 placeholder: 'search',
 clearImage: 'content/images/delete-icon.png'
 });

// Add it to Famo.us main context
 

// Use 'inputCleared' event to know when the field has been cleared
 ci.ev('inputCleared', function() { /*do something*/ });

// Use getValue() and setValue('') as normal Famo.us input
 var ciValue = ci.getValue();
 ci.setValue(ciValue);

// Get Focus
 

// Set Properties
 ci.setProperties({visibility:'hidden'});

 

/*
 *
 *
 *   The MIT License (MIT)
 *
 *   Copyright (c) 2014 Alessandro Filippo Annini
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 * Owner: alessandro.annini@gmail.com
 * @license MIT
 * @copyright Alessandro Filippo Annini, 2014
 *
* */

define(function(require, exports, module) {
    var View = require('famous/core/View');
    var Surface = require('famous/core/Surface');
    var ImageSurface = require('famous/surfaces/ImageSurface');
    var InputSurface = require('famous/surfaces/InputSurface');
    var StateModifier = require('famous/modifiers/StateModifier');
    var Transform = require('famous/core/Transform');
    var EventHandler = require('famous/core/EventHandler');

    function ClearInput(options) {
        View.apply(this, arguments);

        this.eventOutput = new EventHandler();
        EventHandler.setOutputHandler(this, this.eventOutput);

        this.rootModifier = new StateModifier({
            size: this.options.size
        });

        this._mainSurface = new Surface({
            size: options.size || [undefined, true],
            classes: options.classes || ['clear-input']
        });

        this._inputSurface = new InputSurface({
            size: this._mainSurface.getSize(),
            placeholder: options.placeholder,
            type: 'text'
        });

        this._imageSurface = new ImageSurface({
            size: [this._inputSurface.getSize()[1], this._inputSurface.getSize()[1]],
            content: options['clearImage'] || '<img src="' + defaultClearImg + '"/>',
            properties: {
                paddingRight: '4px'
            }
        });

        this._imageModifier = new StateModifier({
            origin: [true, 0.5],
            transform: Transform.translate(-4, 0, 0)
        });

        this._imageSurface.on('click', function () {
            this._inputSurface.setValue('');
            this.eventOutput.emit('inputCleared');
        }.bind(this));

        this.mainNode = this.add(this.rootModifier);
        this.mainNode.add(this._mainSurface);
        this.mainNode.add(this._inputSurface);
        this.mainNode.add(this._imageModifier).add(this._imageSurface);
    }

    ClearInput.prototype = Object.create(View.prototype);
    ClearInput.prototype.constructor = ClearInput;

    ClearInput.prototype.getValue = function () {
        return this._inputSurface.getValue();
    };

    ClearInput.prototype.setValue = function (data) {
        return this._inputSurface.setValue(data);
    };

    ClearInput.prototype.ev = function (ev, action) {
        this._inputSurface.on(ev, action);
    };

    ClearInput.prototype.focus = function() {
        this._inputSurface.focus();
    };

    ClearInput.prototype.setProperties = function(obj) {
        this._inputSurface.setProperties(obj);
    };

    var defaultClearImg = '';

    module.exports = ClearInput;
});