Giter Club home page Giter Club logo

knockout-popuptemplate's Introduction

knockout.popupTemplate

NPM version Build Status Dependency Status

This binding ties an element (called the anchor) to a template (called the popup), which will pop up on screen next to it. By default it works similarly to a dropdown menu, the popup being rendered on initialization, appearing below and aligned to the left edge of the anchor, shown when the anchor is clicked, and hidden if the user clicks outside the popup.

Usage

The basic use of popupTemplate is thus:

<div data-bind="popupTemplate: 'templateName'">Anchor text</div>

This will give the default behavior, outlined above. Alternatively, the binding can take a configuration object, with the following fields:

  • template: Required. A string containing the name of the template to render.
  • data: Data model to use rendering the template.
  • renderOnInit: If truthy, the popup is rendered at init and merely shown or hidden, instead of being rendered on each open.
  • closeOnClickInPopup: If truthy, clicking inside a popup will close it. Useful for menus and similar.
  • openState: An observable that is updated to follow the state of the popup. The popup can also be shown or hidden by setting the observable to true or false.
  • beforeOpen: An event handler to be called before opening the popup.
  • afterOpen: An event handler to be called immediately after opening the popup.
  • beforeClose: An event handler to be called before closing the popup.
  • afterClose: An event handler to be called immediately after closing the popup.
  • anchorHandler: If false, clicking the anchor does not open or close the popup.
  • outsideHandler: If false, clicking outside the popup will not close it.
  • positioning: An object containing positioning information (see below).
  • className: A string containing a class name to put in the class attribute of the popupTemplate container.
  • disposalCallback: A function, that will take care of removing the popupTemplate container from the DOM. This is usefull if you wish to use animations with renderOnOpen mode. The function is passed one argument which is a reference to the element containing the popupTemplate.
  • disable: If this is truthy, or an obseravble containing a truthy value, opening the popup is disabled.
  • openOnMouseOver: If truthy, hovering over the anchor will display the popup.
<div data-bind="popupTemplate: { template: 'templateName' }">Anchor text</div>

Positioning the popup

You can select where the popup will appear in relation to the anchor by passing an object in the positioning field of the configuration object, which should have one or both of vertical and horizontal fields.

These fields can contain either a string, or an observable containing a string. These strings should be selected from the following lists:

Horizontal

  • 'outside-left': Align the right edge of the popup with the left edge of the anchor.
  • 'inside-left': Align the left edge of the popup with the left edge of the anchor.
  • 'middle': Align the horizontal midpoint of the popup with the horizontal middle of the anchor.
  • 'inside-right': Align the right edge of the popup with the right edge of the anchor.
  • 'outside-right': Align the left edge of the popup with the right edge of the anchor.

Vertical

  • 'outside-top': Align the bottom edge of the popup with the top edge of the anchor.
  • 'inside-top': Align the top edge of the popup with the top edge of the anchor.
  • 'middle': Align the vertical midpoint of the popup with the vertical middle of the anchor.
  • 'inside-bottom': Align the bottom edge of the popup with the bottom edge of the anchor.
  • 'outside-bottom': Align the top edge of the popup with the bottom edge of the anchor.

It is also possible to supply a list of positionings where the best fit would be chosen.

For convinience you can also supply strings containing "<horizontal> <vertical>" instead of a position object:

<div data-bind="popupTemplate: {
                  template: 'templateName',
                  'positioning': [
                    { horizontal: 'middle', vertical: 'middle' },
                    'outside-left inside-top',
                    'outside-right middle'
                  ]
                }">
  Anchor text
</div>

Examples

http://one-com.github.io/knockout-popupTemplate/examples/index.html

License

Copyright © 2014, One.com

knockout.popupTemplate is licensed under the BSD 3-clause license, as given at http://opensource.org/licenses/BSD-3-Clause

knockout-popuptemplate's People

Contributors

birkestroem avatar gertsonderby avatar gustav-olsen-groupone avatar gustavnikolaj avatar hanstdam avatar lusia avatar papandreou avatar sunesimonsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

knockout-popuptemplate's Issues

add config.closeOnEscape option

// If closeOnEscape is set, use that value but cast it to boolean - else assume that it is true
config.closeOnEscape = typeof config.closeOnEscape !== 'undefined' ? !!config.closeOnEscape : true;

if (config.closeOnEscape) {
    var escapeEventHandler = function escapeEventHandler(e) {
        if (e.which === 27) {
            config.openState(false);
            e.stopPropagation();
            e.preventDefault();
            return false;
        }
    };

    subscriptions.push(config.openState.subscribe(function (open) {
        if (open) {
            document.addEventListener('keydown', escapeEventHandler, true);
        } else {
            document.removeEventListener('keydown', escapeEventHandler, true);
        }
    }));
}

This is general desireable behaviour, in my opinion.

Could be added by augmenting close popuphandler method to be able to handle both key and mouse events and hooking it into the existing methods for setting up handlers.

callback hooks order

The disposal is done before the afterClose hook has been fired.

In renderOnOpen mode this is the order of the hooks, when opening and closing a popupTemplate:

beforeOpen
afterOpen
beforeClose
disposalCallback
afterClose

I'd say that afterClose should be called when the close class is added before the element is thrown out.

pass positioning parameters as a string

To allow for a simpler configuration in bindings:

<div data-bind="popupTemplate: { template: '...', positioning: 'outer-bottom inner-right' }"></div>

Versus:

<div data-bind="popupTemplate: { template: '...', positioning: { vertical: 'outer-bottom', horizontal: 'inner-right' } }"></div>

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.