mirror of
https://github.com/KevinMidboe/vue-js-modal.git
synced 2025-10-29 18:00:20 +00:00
Added comments
This commit is contained in:
@@ -40,6 +40,10 @@
|
|||||||
buttons () {
|
buttons () {
|
||||||
return this.params.buttons || this.defaultButtons
|
return this.params.buttons || this.defaultButtons
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Returns FLEX style with correct width for arbitrary number of
|
||||||
|
* buttons.
|
||||||
|
*/
|
||||||
buttonStyle () {
|
buttonStyle () {
|
||||||
return {
|
return {
|
||||||
flex: `1 1 ${100 / this.buttons.length}%`
|
flex: `1 1 ${100 / this.buttons.length}%`
|
||||||
|
|||||||
@@ -189,6 +189,9 @@
|
|||||||
created () {
|
created () {
|
||||||
this.setInitialSize()
|
this.setInitialSize()
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Sets global listeners
|
||||||
|
*/
|
||||||
beforeMount () {
|
beforeMount () {
|
||||||
Modal.event.$on('toggle', (name, state, params) => {
|
Modal.event.$on('toggle', (name, state, params) => {
|
||||||
if (name === this.name) {
|
if (name === this.name) {
|
||||||
@@ -208,14 +211,23 @@
|
|||||||
`but height is not "auto" (${this.height})`)
|
`but height is not "auto" (${this.height})`)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Removes "resize" window listener
|
||||||
|
*/
|
||||||
beforeDestroy () {
|
beforeDestroy () {
|
||||||
window.removeEventListener('resize', this.onWindowResize)
|
window.removeEventListener('resize', this.onWindowResize)
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
/**
|
||||||
|
* Returns true if height is set to "auto"
|
||||||
|
*/
|
||||||
isAutoHeight () {
|
isAutoHeight () {
|
||||||
return this.modal.heightType === 'auto'
|
return this.modal.heightType === 'auto'
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Calculates and returns modal position based on the
|
||||||
|
* pivots, window size and modal size
|
||||||
|
*/
|
||||||
position () {
|
position () {
|
||||||
const { window, modal, shift, pivotX, pivotY,
|
const { window, modal, shift, pivotX, pivotY,
|
||||||
trueModalWidth, trueModalHeight, isAutoHeight } = this
|
trueModalWidth, trueModalHeight, isAutoHeight } = this
|
||||||
@@ -235,7 +247,10 @@
|
|||||||
top: inRange(minTop, maxTop, top)
|
top: inRange(minTop, maxTop, top)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Returns pixel width (if set with %) and makes sure that modal size
|
||||||
|
* fits the window
|
||||||
|
*/
|
||||||
trueModalWidth () {
|
trueModalWidth () {
|
||||||
const { window, modal, adaptive, minWidth } = this
|
const { window, modal, adaptive, minWidth } = this
|
||||||
|
|
||||||
@@ -247,7 +262,12 @@
|
|||||||
? inRange(minWidth, window.width, value)
|
? inRange(minWidth, window.width, value)
|
||||||
: value
|
: value
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Returns pixel height (if set with %) and makes sure that modal size
|
||||||
|
* fits the window.
|
||||||
|
*
|
||||||
|
* Returns 0 if height set as "auto"
|
||||||
|
*/
|
||||||
trueModalHeight () {
|
trueModalHeight () {
|
||||||
const { window, modal, isAutoHeight, adaptive } = this
|
const { window, modal, isAutoHeight, adaptive } = this
|
||||||
|
|
||||||
@@ -263,18 +283,24 @@
|
|||||||
? inRange(this.minHeight, this.window.height, value)
|
? inRange(this.minHeight, this.window.height, value)
|
||||||
: value
|
: value
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Returns class list for screen overlay (modal background)
|
||||||
|
*/
|
||||||
overlayClass () {
|
overlayClass () {
|
||||||
return {
|
return {
|
||||||
'v--modal-overlay': true,
|
'v--modal-overlay': true,
|
||||||
'scrollable': this.scrollable && this.isAutoHeight
|
'scrollable': this.scrollable && this.isAutoHeight
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Returns class list for modal itself
|
||||||
|
*/
|
||||||
modalClass () {
|
modalClass () {
|
||||||
return ['v--modal-box', this.classes]
|
return ['v--modal-box', this.classes]
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* CSS styles for position and size of the modal
|
||||||
|
*/
|
||||||
modalStyle () {
|
modalStyle () {
|
||||||
return {
|
return {
|
||||||
top: this.position.top + 'px',
|
top: this.position.top + 'px',
|
||||||
@@ -287,6 +313,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
/**
|
||||||
|
* Initializes modal's size & position,
|
||||||
|
* if "reset" flag is set to true - this function will be called
|
||||||
|
* every time "beforeOpen" is triggered
|
||||||
|
*/
|
||||||
setInitialSize () {
|
setInitialSize () {
|
||||||
let { modal } = this
|
let { modal } = this
|
||||||
let width = parseNumber(this.width)
|
let width = parseNumber(this.width)
|
||||||
@@ -303,6 +334,9 @@
|
|||||||
this.window.height = window.innerHeight
|
this.window.height = window.innerHeight
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates event object
|
||||||
|
*/
|
||||||
genEventObject (params) {
|
genEventObject (params) {
|
||||||
//todo: clean this up (change to ...)
|
//todo: clean this up (change to ...)
|
||||||
var data = {
|
var data = {
|
||||||
@@ -317,7 +351,9 @@
|
|||||||
|
|
||||||
return Vue.util.extend(data, params || {});
|
return Vue.util.extend(data, params || {});
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Event handler which is triggered on modal resize
|
||||||
|
*/
|
||||||
onModalResize (event) {
|
onModalResize (event) {
|
||||||
this.modal.widthType = 'px'
|
this.modal.widthType = 'px'
|
||||||
this.modal.width = event.size.width
|
this.modal.width = event.size.width
|
||||||
@@ -330,7 +366,9 @@
|
|||||||
|
|
||||||
this.$emit('resize', resizeEvent)
|
this.$emit('resize', resizeEvent)
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Event handler which is triggered on $modal.show and $modal.hight
|
||||||
|
*/
|
||||||
toggle (state, params) {
|
toggle (state, params) {
|
||||||
const { reset, visible } = this
|
const { reset, visible } = this
|
||||||
|
|
||||||
@@ -383,7 +421,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Event handler that is triggered when background overlay is clicked
|
||||||
|
*/
|
||||||
onBackgroundClick () {
|
onBackgroundClick () {
|
||||||
if (this.clickToClose) {
|
if (this.clickToClose) {
|
||||||
this.toggle(false)
|
this.toggle(false)
|
||||||
|
|||||||
15
src/index.js
15
src/index.js
@@ -5,13 +5,18 @@ const defaultComponentName = 'modal'
|
|||||||
|
|
||||||
const Plugin = {
|
const Plugin = {
|
||||||
install (Vue, options = {}) {
|
install (Vue, options = {}) {
|
||||||
|
/**
|
||||||
|
* Makes sure that plugin can be insstalled only once
|
||||||
|
*/
|
||||||
if (this.installed) {
|
if (this.installed) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.installed = true
|
this.installed = true
|
||||||
this.event = new Vue()
|
this.event = new Vue()
|
||||||
|
/**
|
||||||
|
* Plugin API
|
||||||
|
*/
|
||||||
Vue.prototype.$modal = {
|
Vue.prototype.$modal = {
|
||||||
show (name, params) {
|
show (name, params) {
|
||||||
Plugin.event.$emit('toggle', name, true, params)
|
Plugin.event.$emit('toggle', name, true, params)
|
||||||
@@ -25,10 +30,14 @@ const Plugin = {
|
|||||||
Plugin.event.$emit('toggle', name, undefined, params)
|
Plugin.event.$emit('toggle', name, undefined, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Sets custom component name (if provided)
|
||||||
|
*/
|
||||||
const componentName = options.componentName || defaultComponentName
|
const componentName = options.componentName || defaultComponentName
|
||||||
Vue.component(componentName, Modal)
|
Vue.component(componentName, Modal)
|
||||||
|
/**
|
||||||
|
* Registration of <Dialog/> component
|
||||||
|
*/
|
||||||
if (options.dialog) {
|
if (options.dialog) {
|
||||||
Vue.component('v-dialog', Dialog)
|
Vue.component('v-dialog', Dialog)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user