Skip to content

Instantly share code, notes, and snippets.

@saksmt
Created December 9, 2014 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saksmt/5b56a05af6313c222590 to your computer and use it in GitHub Desktop.
Save saksmt/5b56a05af6313c222590 to your computer and use it in GitHub Desktop.
Clock for Browser(Usefull with 2 monitors)
(function (window) {
'use stict';
/**
* Replaces "{slug}" with specified values, provided by argument
* @brief Set's slugs in string
* @param {Object} slugValues Map "slugName:slugValue"
* @returns {String}
*/
String.prototype.setSlugs = function (slugValues) {
var
slugName,
that = this;
for (slugName in slugValues) {
if (slugValues.hasOwnProperty(slugName)) {
var slug = slugValues[slugName];
that = that.split('{' + slugName + '}').join(slug);
}
}
return that;
};
var
clockId = 'cl' + Math.round(Math.random()*1000000)%100 + 'ock',
addStyle = function (stylesheet) {
var style = window.document.createElement('style');
style.innerHTML = stylesheet.setSlugs({
id: '#' + clockId
});
window.document.head.appendChild(style);
},
tick = false,
clock = document.createElement('div');
addStyle('\
{id} {\
top: 0px;\
left: 0px;\
cursor: move;\
padding: 10px;\
color: #3c3c3c;\
font-size: 24px;\
position: fixed;\
z-index: 9999999;\
user-select: none;\
border-radius: 3px;\
border: 3px solid #ccc;\
-moz-user-select: none;\
font-family: "Play", sans;\
background-color: #f7f7f7;\
}\
{id}::selection {\
color: inherit;\
background-color: inherit;\
}\
');
clock.id = clockId;
clock.onmousedown = function(e) {
var
self = this,
shiftX = e.clientX - self.getBoundingClientRect().left,
shiftY = e.clientY - self.getBoundingClientRect().top,
moveAt = function (e) {
var windowBorders = {
x: window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0,
y: window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0
};
if (
e.clientX - shiftX < 0 ||
e.clientX - shiftX + self.clientWidth > windowBorders.x ||
e.clientY - shiftY < 0 ||
e.clientY - shiftY + self.clientHeight > windowBorders.y
) {
return;
}
self.style.left = e.clientX - shiftX + 'px';
self.style.top = e.clientY - shiftY + 'px';
};
document.body.appendChild(this);
moveAt(e);
document.onmousemove = function(e) {
moveAt(e);
};
this.onmouseup = function() {
document.onmousemove = self.onmouseup = null;
};
};
clock.ondragstart = function() {
return false;
};
clock.updateTime = function () {
var date = new Date();
tick = !tick;
clock.innerHTML =
('0' + date.getHours()).substr(-2) +
(tick ? ' ': ':') +
('0' + date.getMinutes()).substr(-2);
};
clock.ondblclick = function () {
if (confirm('Close "Clock"?')) {
clock.parentElement.removeChild(clock);
}
};
var lifeCycle = setInterval(clock.updateTime, 1000);
document.body.appendChild(clock);
clock.updateTime();
}(window));
!function(e){"use stict";String.prototype.setSlugs=function(e){var n,t=this;for(n in e)if(e.hasOwnProperty(n)){var o=e[n];t=t.split("{"+n+"}").join(o)}return t};var n="cl"+Math.round(1e6*Math.random())%100+"ock",t=function(t){var o=e.document.createElement("style");o.innerHTML=t.setSlugs({id:"#"+n}),e.document.head.appendChild(o)},o=!1,i=document.createElement("div");t('{id} {top:0px;left:0px;cursor:move;padding:10px;color:#3c3c3c;font-size:24px;position:fixed;z-index:9999999;user-select:none;border-radius:3px;border:3px solid #ccc;-moz-user-select:none;font-family:"Play", sans;background-color:#f7f7f7;}{id}::selection {color:inherit;background-color:inherit;}'),i.id=n,i.onmousedown=function(n){var t=this,o=n.clientX-t.getBoundingClientRect().left,i=n.clientY-t.getBoundingClientRect().top,c=function(n){var c={x:e.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0,y:e.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0};n.clientX-o<0||n.clientX-o+t.clientWidth>c.x||n.clientY-i<0||n.clientY-i+t.clientHeight>c.y||(t.style.left=n.clientX-o+"px",t.style.top=n.clientY-i+"px")};document.body.appendChild(this),c(n),document.onmousemove=function(e){c(e)},this.onmouseup=function(){document.onmousemove=t.onmouseup=null}},i.ondragstart=function(){return!1},i.updateTime=function(){var e=new Date;o=!o,i.innerHTML=("0"+e.getHours()).substr(-2)+(o?" ":":")+("0"+e.getMinutes()).substr(-2)},i.ondblclick=function(){confirm('Close "Clock"?')&&i.parentElement.removeChild(i)};setInterval(i.updateTime,1e3);document.body.appendChild(i),i.updateTime()}(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment