dialog-promise
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -14,3 +14,3 @@ "use strict"; | ||
DialogPromise.castellano={ | ||
DialogPromise.es={ | ||
Ok: 'Aceptar', | ||
@@ -22,2 +22,5 @@ Cancel: 'Cancelar', | ||
DialogPromise.defaultOpts={ | ||
}; | ||
function dialogPromise(dialogConstructor, opts){ | ||
@@ -47,8 +50,46 @@ opts = opts||{}; | ||
body.appendChild(dialogWindow); | ||
dialogWindow.style.visibility='hidden'; | ||
dialogWindow.style.display='block'; | ||
var ubicateDialog=function(){ | ||
if(opts.underElement){ | ||
dialogWindow.style.position='absolute'; | ||
var rect = opts.underElement.getBoundingClientRect(); | ||
dialogWindow.style.top=rect.bottom+'px'; | ||
dialogWindow.style.left=rect.left+'px'; | ||
dialogWindow.style.visibility='visible'; | ||
}else{ | ||
dialogWindow.style.left=Math.floor(Math.max(50-dialogWindow.offsetWidth/2*100/window.innerWidth))+'%'; | ||
dialogWindow.style.top=Math.floor(Math.max(50-dialogWindow.offsetHeight/2*100/window.innerHeight))+'%'; | ||
dialogWindow.style.visibility='visible'; | ||
dialogWindow.style.display='block'; | ||
var changes=3; | ||
var control=setInterval(function(){ | ||
if(changes){ | ||
if( | ||
dialogWindow.offsetHeight+dialogWindow.offsetTop>=window.innerHeight || | ||
dialogWindow.offsetWidth+dialogWindow.offsetLeft>=window.innerWidth | ||
){ | ||
changes--; | ||
dialogWindow.style.top=Math.floor(Math.max(50-dialogWindow.offsetHeight/2*100/window.innerHeight))+'%'; | ||
dialogWindow.style.left=Math.floor(Math.max(50-dialogWindow.offsetWidth/2*100/window.innerWidth))+'%'; | ||
} | ||
} | ||
},250); | ||
} | ||
}; | ||
if(dialogWindow.offsetWidth){ | ||
ubicateDialog(); | ||
}else{ | ||
setTimeout(ubicateDialog,25); | ||
} | ||
modalBackground.style.display='block'; | ||
modalBackground.addEventListener('click', closeWindowWithEmptyAnswer); | ||
var interceptKey = function interceptKey(event){ | ||
if(event.target!==dialogWindow && !dialogWindow.contains(event.target)){ | ||
var code=event.keyCode || event.which; | ||
if(code==27){ | ||
closeWindowWithEmptyAnswer(); | ||
event.preventDefault(); | ||
}else if(code>=112 && code<=123){ | ||
}else if(event.target!==dialogWindow && !dialogWindow.contains(event.target)){ | ||
event.preventDefault(); | ||
} | ||
@@ -71,4 +112,6 @@ } | ||
function simpleFormPromise(elementsList){ | ||
return dialogPromise(function(mainElement, done){ | ||
function simpleFormPromise(opts){ | ||
var elementsList=opts.elementsList; | ||
delete opts.elementsList; | ||
return dialogPromise(function(mainElement, done, opts){ | ||
elementsList.forEach(function(elementDefinition){ | ||
@@ -80,2 +123,3 @@ if(typeof elementDefinition=='string'){ | ||
}else if(elementDefinition instanceof HTMLElement){ | ||
elementDefinition.dialogPromiseDone=done; | ||
mainElement.appendChild(elementDefinition); | ||
@@ -94,21 +138,41 @@ }else{ | ||
function alertPromise(message, buttonDef){ | ||
return simpleFormPromise([message, buttonDef||{label:DialogPromise.messages.Ok, value:true}]); | ||
function alertPromise(message, opts){ | ||
opts=opts||DialogPromise.defaultOpts; | ||
opts.elementsList=[message, opts.buttonDef||{label:DialogPromise.messages.Ok, value:true}]; | ||
if(opts.buttonDef){ | ||
delete opts.buttonDef; | ||
} | ||
return simpleFormPromise(opts); | ||
} | ||
function confirmPromise(message, buttonsDef){ | ||
return simpleFormPromise([message].concat( | ||
buttonsDef||[ | ||
function confirmPromise(message, opts){ | ||
opts=opts||DialogPromise.defaultOpts; | ||
opts.elementsList=[message].concat( | ||
opts.buttonsDef||[ | ||
{label:DialogPromise.messages.Yes, value:true}, | ||
{label:DialogPromise.messages.No, value:false} | ||
] | ||
)); | ||
) | ||
if(!opts.buttonsDef){ | ||
delete opts.buttonsDef; | ||
} | ||
return simpleFormPromise(opts); | ||
} | ||
function promptPromise(message, buttonsDef){ | ||
buttonsDef = buttonsDef||[ | ||
function promptPromise(message, opts){ | ||
opts=opts||DialogPromise.defaultOpts; | ||
var buttonsDef = opts.buttonsDef||[ | ||
{label:DialogPromise.messages.Ok, value:true}, | ||
{label:DialogPromise.messages.Cancel, value:false} | ||
]; | ||
delete opts.buttonsDef; | ||
var input = document.createElement('input'); | ||
var firstKey=true; | ||
input.onkeydown=function(event){ | ||
if(firstKey){ | ||
firstKey=false; | ||
}else if((event.keyCode || event.which)==13){ | ||
input.dialogPromiseDone(input.value); | ||
} | ||
}; | ||
buttonsDef.forEach(function(buttonDef){ | ||
@@ -119,10 +183,13 @@ if(buttonDef.value===true){ | ||
}); | ||
return simpleFormPromise([ | ||
setTimeout(function(){ input.focus(); },50); | ||
opts.elementsList=[ | ||
message, | ||
input, | ||
document.createElement('br') | ||
].concat(buttonsDef)); | ||
].concat(buttonsDef); | ||
return simpleFormPromise(opts); | ||
} | ||
function miniMenuPromise(menu, opts){ | ||
opts=opts||DialogPromise.defaultOpts; | ||
return dialogPromise(function(mainElement,done){ | ||
@@ -150,3 +217,3 @@ var table=document.createElement('table'); | ||
mainElement.classList.add('dialog-0'); | ||
}) | ||
},opts) | ||
} |
{ | ||
"name": "dialog-promise", | ||
"description": "Dialog that returns promises", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "Codenautas <codenautas@googlegroups.com>", | ||
@@ -17,5 +17,5 @@ "repository": "codenautas/dialog-promise", | ||
"es6-promise": "~3.2.1", | ||
"phantomjs-prebuilt": "~2.1.7", | ||
"phantomjs-prebuilt": "~2.1.12", | ||
"best-globals": "~0.4.8" | ||
"best-globals": "~0.4.9" | ||
}, | ||
@@ -22,0 +22,0 @@ "engines": { |
@@ -18,2 +18,10 @@ # dialog-promise | ||
# API | ||
## alertPromise, confirmPromise & promptPromise | ||
Implements `alert`, `confirm` & `prompt` functions with Promises | ||
## Use | ||
@@ -26,4 +34,6 @@ | ||
function example2(button){ | ||
alertPromise('the button becames red').then(function(){ | ||
button.style.backgroundColor='red'; | ||
promptPromise("What's your name?").then(function(name){ | ||
return alertPromise("Hi "+name); | ||
}).then(function(){ | ||
button.textContent='try again!'; | ||
}); | ||
@@ -34,2 +44,42 @@ } | ||
## alertPromise, confirmPromise, promptPromise | ||
Recives a mensage (and an object with options). | ||
These are versions of the native funcions `alert`, `confirm` y `prompt` | ||
function |returned value | ||
----------|--------------------- | ||
`alert` |`true` | ||
`confirm` |`true` or `false` | ||
`prompt` |a string with the entered text | ||
If `Escape` is pressed or the mouse clicked outside the dialog the function returns `undefined`. | ||
## miniMenuPromise(elementsList[, opts]) | ||
Display a menu | ||
```html | ||
<script src="dialog-promise.js"></script> | ||
<button id=theButton onclick='example3(this)'>example</button> | ||
<script> | ||
function example3(button){ | ||
miniMenuPromise([ | ||
{value:'ar', img:'ar.png', label:'Argentina'}, | ||
{value:'cl', img:'cl.png', label:'Chile'}, | ||
{value:'uy', img:'uy.png', label:'Uruguay'}, | ||
], {underElement: theButton}).then(function(option){ | ||
return alertPromise("val = "+option); | ||
}); | ||
} | ||
</script> | ||
``` | ||
## Options | ||
The options could be passed to all of these functions | ||
option |use | ||
---------------|------------------------------------- | ||
`underElement` | the dialog apears below this element. If no element is passed the dialog apears in the center of the window | ||
## License | ||
@@ -36,0 +86,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15049
289
86