Giter VIP home page Giter VIP logo

jquery-ui-bem-block's Introduction

Code Climate

jquery-ui-bem-block

Base jquery-ui widget to manipulate classes like in bem methodology

Disclosure

This widget do not use standard BEM naming convention(b-block__element_mod). Instead, I am using such naming b-block--element_mod to make selection in editor more convenient. But you can change this behavior by setting options to widget:

  • mod_delim
  • elem_delim

Usage

To make your own widget with BEM support simple inherit from $.opesho.block:

$.widget('ns.myWidget', $.opesho.block, {
    _create: function() {
        this._super();
    }
});

Public API

Get elements by name

/**
 * @param {String} name
 */
elems(name)

Example:

/**
 * return all nodes with classname b-block--item
 */
var blockElements = $('#mywidget').block('elems', 'item');

Check element exists in block

/**
 * @param {String} name
 */
hasElems(name)

Get modifiers array for given element.

/**
 * @param {String} [elem="widget block element"] The elem is optional.
 */
mods(elem)

Example:

/*
 * Returns array, for example, ["state", "theme"]
 */
var blockElements = $('#mywidget').block('mods');

Apply modifier to element.

/**
 * @param {String} key
 * @param {String|Boolean} [value]
 * @param {jQuery} [elem]
 */
setMod(mod, value, elem)

Delete modifier from element

/**
 * Delete mod from element or block
 *
 * @param {String} key
 * @param {String|Boolean} [value]
 * @param {jQuery} [elem]
 */
delMod(mod, value, elem)

Toggle modifier

/**
 * @param {String} key
 * @param {String|Boolean} [value]
 * @param {jQuery} [elem]
 */
toggleMod(mod, value, elem)

Get modifier value

/**
 * @param {String} key
 * @param {jQuery} elem
 */
modVal(mod, elem)

Extend block widget example

    var $document = $(document);

    $document.on('click', function (event) {
        $document.trigger('custom:click', event.target);
    });

    // provide simple widget communication
    $.widget('custom.clickable', {
        _create: function() {
            var self = this,
                element = this.element;
            this.document.on('custom:click', function (event, origTarget) {
                var sendEvent = !element.is(origTarget) && !element.has(origTarget).length;
                if (sendEvent) {
                    self._trigger('custom:click');
                }
            });
        }
    });
    // extend base block
    $.widget('opesho.block', $.opesho.block, {
        _create: function () {
            this._super();
            this.element.clickable({
               "custom:click": $.proxy(this._onCustomClick, this)
            });
        }
    });
    //make your base panel widget that can be closed on document click or another widget click
    $.widget('custom.panel', $.opesho.block, {
        _create: function () {
            this._isOpen = false;
        },
        _onCustomClick: function () {
            if (this._isOpen) {
                this.close();
            }
        },
        close: function () {
            this.element.hide();
            this._isOpen = false;
        },
        open: function () {
            this.element.show();
            this._isOpen = true;
        },
        toggle: function() {
            var self = this;
            this._isOpen = false;
            setTimeout(function() {
                self.element.toggle();
                if (self.element.is(':hidden') === false) {
                    self._isOpen = true;
                }
            }, 0);
        }
    });

Using block without inherit

//TODO

TODO

  • trigger events
  • make unit tests
  • cache
  • documentation

License

Apache 2.0

jquery-ui-bem-block's People

Contributors

zemd avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.