I'm playing around with prototypes, constructor functions and javascript - I'm not really a Javascript developer but am interested in this.
Am I on the right track with this code? I'm looking for a code review from a JS developer. I know the codes a bit funky.
I created an example of it working here.
function modal( width, height, backgroundColor, overlayColor, showOverlay, url )
{
this.width = width; // a percentage based number %
this.height = height; // optional, usually needs not to be set
this.backgroundColor = backgroundColor; // set the background colour to the modal
this.overlayColor = overlayColor; // as hex, to be converted to rgba
this.url = url;
this.showOverlay = showOverlay;
this.createModal = function()
{
// create elements to add classes to and insert in to dom
var modalOverlay = document.createElement('div')
, modalContent = document.createElement('div')
, modalInner = document.createElement('div');
// build html structure
modalOverlay.className = "modalOverlay";
modalContent.className = "modalContent";
modalOverlay.appendChild(modalContent);
modalInner.className = "modalInner";
modalContent.appendChild(modalInner);
// add new html structure to body
document.body.appendChild(modalOverlay);
// Theme model
this.themeModel();
}
this.themeModel = function() {
// is there a background color?
var overlayBgColor = this.showOverlay ? this.convertHext(this.overlayColor,50) : "transparent";
$('.modalOverlay').css({
'background-color': overlayBgColor
});
$('.modalContent').css({
'background-color': this.backgroundColor,
'width': this.width,
'height': this.height
});
// load content
this.loadContent();
}
this.loadContent = function(){
if( url )
{
$('.modalInner').load(url);
}
else
{
$('.modalInner').html("please provide a url to load! <a class='close-modal' href=''>close</a>");
}
}
this.showModal = function(){
this.createModal();
$('.modalOverlay').fadeIn(50);
$('body').addClass('modalShow');
}
this.hideModal = function(){
$('.modalOverlay').fadeOut(300, function(){
$('.modalOverlay').remove();
})
$('body').removeClass('modalShow');
}
this.convertHext = function(hex,opacity)
{
hex = hex.replace('#','');
r = parseInt(hex.substring(0,2), 16);
g = parseInt(hex.substring(2,4), 16);
b = parseInt(hex.substring(4,6), 16);
result = 'rgba('+r+','+g+','+b+','+opacity/100+')';
return result;
}
}
body
{
position: relative;
margin: 0; /* reset margin */
padding: 0; /* reset padding */
}
.modalShow .modalContent
{
opacity: 1;
overflow':'hidden';
-webkit-transition: all .3s ease-out;
transition: all .3s ease-out;
margin-top: 30px;
}
.modalOverlay
{
display: none;
overflow: auto;
overflow-y: scroll;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 9999;
-webkit-overflow-scrolling: touch;
outline: 0;
background-color: #2d2d2d;
background-color: rgba(45,45,45,0.2);
}
.modalContent
{
width: 50%;
height: auto;
background-color: #FFFFFF;
margin-top: 0;
margin-bottom: 30px;
margin-left: auto;
margin-right: auto;
opacity: 0;
-webkit-transition: all .3s ease-out;
transition: all .3s ease-out;
}
.modalInner
{
padding: 20px;
}
<body>
<a class="showModal" href="">show modal</a>
</body>