Last active
August 29, 2015 14:16
-
-
Save navio/4987211dbb30d9113167 to your computer and use it in GitHub Desktop.
Modal Singleton Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Information</title> | |
</head> | |
<body> | |
<p>Open Modal</p> | |
</body> | |
<style media="screen"> | |
.ui-modal{ | |
display:block; | |
color: red; | |
} | |
.hide { | |
display: none; | |
} | |
</style> | |
<script charset="utf-8"> | |
function ModalClass(content){ | |
this.instance = init(); | |
this.data = content; | |
function init(){ | |
var modalDiv = document.createElement('div'); | |
modalDiv.setAttribute('class','ui-modal hide'); | |
modalDiv.innerHTML = content; | |
return document.querySelector('body').appendChild(modalDiv); | |
} | |
} | |
ModalClass.prototype._refreshContent = function(){ this.instance.innerHTML = this.data; }; | |
ModalClass.prototype.updateContent = function(content){ | |
this.data = content; | |
this._refreshContent(); | |
}; | |
ModalClass.prototype.me = function() { return this.instance }; | |
ModalClass.prototype.hide = function() { this.instance.setAttribute('class','hide'); return 0; } | |
ModalClass.prototype.show = function() { this.instance.setAttribute('class','ui-modal'); return 1; }; | |
function ModalFactory(){ | |
var instance; | |
return function interface(){ | |
if(this.instance !== undefined ){ | |
this.instance.updateContent(arguments[0]); | |
return this.instance; | |
} | |
this.instance = Object.create(ModalClass.prototype); | |
ModalClass.apply(this.instance,arguments); | |
return this.instance; | |
}; | |
} | |
var Modal = new ModalFactory(); | |
var modal = Modal('This is my Content'); | |
var modal1 = Modal('This is my Content1'); | |
document.querySelector('p').addEventListener('click',function(){ modal.show(); }); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment