JSPM

  • Created
  • Published
  • Downloads 1160
  • Score
    100M100P100Q106104F
  • License MIT

collection of mixins in vue

Package Exports

  • vue-mixins
  • vue-mixins/class
  • vue-mixins/fragToString
  • vue-mixins/getDocumentHeight
  • vue-mixins/getViewportSize
  • vue-mixins/getVue
  • vue-mixins/isOpened
  • vue-mixins/onClick
  • vue-mixins/onClickStack
  • vue-mixins/onDocument
  • vue-mixins/onElementResize
  • vue-mixins/onResize
  • vue-mixins/onWindowResize
  • vue-mixins/onWindowScroll
  • vue-mixins/onceDocument
  • vue-mixins/parentListener
  • vue-mixins/setCss
  • vue-mixins/style
  • vue-mixins/vue

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-mixins) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

vue-mixins

A collection of mixins in vue. Heavily used in vue-comps.

Policy

all sorts of mixins can be submitted. There will be no removes because of deprecation. If the API of a mixin changes the name has to change, for example onResize -> onResize2

Install

npm install --save-dev vue-mixins

or include bundle.js

Usage

## whithin your module
components:
  mixins:[
    require("vue-mixins/onClick")
  ]
# if you have used the bundle.js
components:
  mixins:[
    window.vueMixins.onClick
  ]

List of mixins

Name src description
getViewportSize link adds a method getViewportSize which returns an object containing the width and height of the viewport
getDocumentHeight link adds a method getDocumentHeight which returns the height of the document
onceDocument link adds a eventListener to the document which removes itself after first successful call
onClick link adds a method click which will call the function onClick if set
onClickStack link adds two methods: click and addToClickStack
onClickStore link adds two methods: click and onClick (see below)
onDocument link like onceDocument but doesn't removes itself
onResize link deprecated
onWindowResize link fires on resize of window
onElementResize link fires on resize of window or element, but only if the dimensions of the element changed
onWindowScroll link fires on scroll on window
setCss link set Css of another element
dynamicCss link dynamically manipulate css stylesheet
getVue link deprecated, use vue instead
vue link adds a computed property Vue with the current instance of Vue
isOpened link adds everything for opened state management
parentListener link hooks a function upon parent click
fragToString link converts a documentFragment to String
class link used to create a properly merged vue class object/array from a given prop and another vue class object/array
style link used to create a properly merged vue style object/array from a given prop and another vue style object/array

Detailed usage

getViewportSize

// adds a method:
// getViewportSize()
//
// usage:
vs = this.getViewportSize()
vs.width
vs.height

getDocumentHeight

// adds a method:
// getDocumentHeight()
//
// usage:
height = this.getDocumentHeight()

onceDocument

// adds a method:
// onceDocument(event, cb, useCapture)
//
// usage:
dispose = this.onceDocument('click',function(e){
  doSomething()
  // return true will remove the listener
  // return false will not remove the listener
  },false)
dispose() // will remove the listener

onClick

// adds a method:
// click(event) which will call this.onClick(e) if available
//
// usage:
this.onClick = function(e) {doSomething()}
<!-- in template -->
<div @click="click"></div>

onClickStack

// adds two methods:
// - click(event) will call the last function in this.onClickStack if available
// - addToClickStack(fn) will add a function to this.onClickStack and return a function to dispose it
//
// usage:
var dispose = null
var cb = function(e) {
  doSomething()
  dispose() // to remove from stack
}
dispose = this.addToClickStack(cb)
<!-- in template -->
<div @click="click"></div>

onClickStore

// adds two methods:
// - click(event) will call all functions in this.onClickStore
// - onClick(fn) will add a function to this.onClickStore and return a function to dispose it
//
// usage:
var dispose = null
var cb = function(e) {
  doSomething()
  dispose() // to remove from store
}
dispose = this.onClickStore(cb)
<!-- in template -->
<div @click="click"></div>

onDocument

like onceDocument, but doesn't remove itself on first successful invokation.

onWindowResize

// adds a method: onWindowResize(cb) which will return a function to dispose it
//
// usage:
dispose = this.onWindowResize(function(){/*doSomethingOnWindowResize*/})
// remove your cb
dispose()
// all events will be automatically disposed on `beforeDestroy`

onElementResize

// adds a method: onElementResize(el, cb) which will return a function to dispose it
//
// usage:
dispose = this.onElementResize(el, function(){/*doSomethingOnElementResize*/})
// remove your cb
dispose()
// all events will be automatically disposed on `beforeDestroy`

onWindowScroll

// adds a method: onWindowScroll(cb) which will return a function to dispose it
//
// usage:
dispose = this.onWindowScroll(function(){/*doSomethingOnWindowScroll*/})
// remove your cb
dispose()
// all events will be automatically disposed on `beforeDestroy`

setCss

// adds a method:
// setCss(element,cssProperty, cssValue)
//
// usage:
this.setCss(document.body,"overflow","hidden")

// remove overflow from style attribute
this.setCss(document.body,"overflow")
// or
this.setCss(document.body,"overflow", "")

dynamicCss

// used to create a stylesheet and set rules in it.
// adds a method:
// setCssRules(newRules)
//
// usage:
this.setCssRules({body: {overflow: "hidden"}})
// to remove a rule:
this.setCssRules({body: {overflow: null}})
// nesting:
this.setCssRules({body: {"& div": {width: "10px"},overflow:"hidden"}})
// is short for: (& will be replaced by the parent selector)
// deeper nesting is allowed
this.setCssRules({body: {overflow:"hidden"},"body div": {width: "10px"}})

vue

// adds a computed property:
// Vue
//
// usage:
Vue = this.Vue

isOpened

// adda a boolean prop "isOpened" which will call "this.toggle()" on change
//
// adds two methods:
// setOpened(), setClosed() which will set "this.isOpened" without triggering
// the toggle
//
// usage:
methods:
  toggle: function(){
    if (this.opened) {
      this.close()
    } else {
      this.open()
    }
  }
  open: function() {
    this.setOpened()
  }
  close: function() {
    this.setClosed()
  }  

parentListener

// adds two props: "ignoreParent" and "parent", which
// defaults to "this.$el.parentElement"
//
// usage:
methods:
  onParentClick: function() {
    // will be called when "ignoreParent" is false on click on parent
  }

fragToString

// adds a method: "fragToString"
// usage:
str = this.fragToString(someFrag)
// str contains outerHTML of someFrag

class

// adds a computed property: "computedClass"
// which merges a "mergeClass" data object/array and a "class" prop.
// usage:
template: "<div :class=computedClass></div>",
props: {
  class: {
    default: function() {
      return ["someClass"]
    }
  }
},
data: function() {
  return {
    mergeClass: ["anotherClass"]
  }
}
// computedClass will be ["anotherClass","someClass"] when no prop is given
// if the component is used like this <comp :class="['yetAnotherClass']"></comp>
// computedClass will be ["anotherClass","yetAnotherClass"]
// works also with object notation and a mixture of both

style

// adds a computed property: "computedStyle"
// which merges a "mergeStyle" data object and a "style" prop.
// usage:
template: "<div :style=computedStyle></div>",
props: {
  style: {
    default: function() {
      return {color:"red"}
    }
  }
},
data: function() {
  return {
    mergeStyle: {color:"blue"}
  }
}
// computedStyle will be [{color:"blue"},{color:"red"}] when no prop is given
// if the component is used like this <comp :style="{color:white}"></comp>
// computedStyle will be [{color:"blue"},{color:"white"}]
// works also with array notation and a mixture of both

Develop

Clone rep

npm install

Available scripts:

npm run build # compiles coffee-script in src/
npm run test # runs a single-run karma in chrome and firefox
npm run watch # runs karma in chrome (uses src/*.coffee files direclty, no need for build)

# to run only single tests:
karma start --browsers Chrome --auto-watch --reporters spec --files ['test/onClick.coffee']

License

Copyright (c) 2015 Paul Pflugradt Licensed under the MIT license.