📄 lightwindow.js
字号:
//
// Display the lightWindow.
//
_displayLightWindow : function(display, visibility) {
$('lightwindow_overlay').style.display = $('lightwindow').style.display = $('lightwindow_container').style.display = display;
$('lightwindow_overlay').style.visibility = $('lightwindow').style.visibility = $('lightwindow_container').style.visibility = visibility;
},
//
// Setup Dimensions of lightwindow.
//
_setupDimensions : function() {
var originalHeight, originalWidth;
switch (this.windowType) {
case 'page' :
originalHeight = this.options.dimensions.page.height;
originalWidth = this.options.dimensions.page.width;
break;
case 'image' :
originalHeight = this.options.dimensions.image.height;
originalWidth = this.options.dimensions.image.width;
break;
case 'media' :
originalHeight = this.options.dimensions.media.height;
originalWidth = this.options.dimensions.media.width;
break;
case 'external' :
originalHeight = this.options.dimensions.external.height;
originalWidth = this.options.dimensions.external.width;
break;
case 'inline' :
originalHeight = this.options.dimensions.inline.height;
originalWidth = this.options.dimensions.inline.width;
break;
default :
originalHeight = this.options.dimensions.page.height;
originalWidth = this.options.dimensions.page.width;
break;
}
var offsetHeight = this._getParameter('lightwindow_top') ? parseFloat(this._getParameter('lightwindow_top'))+this.pagePosition.y : this.dimensions.viewport.height/2+this.pagePosition.y;
var offsetWidth = this._getParameter('lightwindow_left') ? parseFloat(this._getParameter('lightwindow_left'))+this.pagePosition.x : this.dimensions.viewport.width/2+this.pagePosition.x;
// So if a theme has say shadowed edges, they should be consistant and take care of in the contentOffset
$('lightwindow').setStyle({
top: offsetHeight+'px',
left: offsetWidth+'px'
});
$('lightwindow_container').setStyle({
height: originalHeight+'px',
width: originalWidth+'px',
left: -(originalWidth/2)+'px',
top: -(originalHeight/2)+'px'
});
$('lightwindow_contents').setStyle({
height: originalHeight+'px',
width: originalWidth+'px'
});
},
//
// Get the type of file.
//
_fileType : function(url) {
var image = new RegExp("[^\.]\.("+this.options.fileTypes.image.join('|')+")\s*$", "i");
if (image.test(url)) return 'image';
if (url.indexOf('#') > -1 && (document.domain == this._getDomain(url))) return 'inline';
if (url.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));
var type = 'unknown';
var page = new RegExp("[^\.]\.("+this.options.fileTypes.page.join('|')+")\s*$", "i");
var media = new RegExp("[^\.]\.("+this.options.fileTypes.media.join('|')+")\s*$", "i");
if (document.domain != this._getDomain(url)) type = 'external';
if (media.test(url)) type = 'media';
if (type == 'external' || type == 'media') return type;
if (page.test(url) || url.substr((url.length-1), url.length) == '/') type = 'page';
return type;
},
//
// Get file Extension
//
_fileExtension : function(url) {
if (url.indexOf('?') > -1) {
url = url.substring(0, url.indexOf('?'));
}
var extenstion = '';
for (var x = (url.length-1); x > -1; x--) {
if (url.charAt(x) == '.') {
return extenstion;
}
extenstion = url.charAt(x)+extenstion;
}
},
//
// Monitor the keyboard while this lightwindow is up
//
_monitorKeyboard : function(status) {
if (status) document.onkeydown = this._eventKeypress.bind(this);
else document.onkeydown = '';
},
//
// Perform keyboard actions
//
_eventKeypress : function(e) {
if (e == null) {
var keycode = event.keyCode;
} else {
var keycode = e.which;
}
switch (keycode) {
case 27:
this.deactivate();
break;
case 13:
return;
default:
break;
}
// Gotta stop those quick fingers
if (this.animating) {
return false;
}
switch (String.fromCharCode(keycode).toLowerCase()) {
case 'p':
if (this.navigationObservers.previous) {
this.navigateWindow('previous');
}
break;
case 'n':
if (this.navigationObservers.next) {
this.navigateWindow('next');
}
break;
default:
break;
}
},
//
// Get Gallery Information
//
_getGalleryInfo : function(rel) {
if (!rel) return false;
if (rel.indexOf('[') > -1) {
return new Array(escape(rel.substring(0, rel.indexOf('['))), escape(rel.substring(rel.indexOf('[')+1, rel.indexOf(']'))));
} else {
return false;
}
},
//
// Get the domain from a string.
//
_getDomain : function(url) {
var leadSlashes = url.indexOf('//');
var domainStart = leadSlashes+2;
var withoutResource = url.substring(domainStart, url.length);
var nextSlash = withoutResource.indexOf('/');
var domain = withoutResource.substring(0, nextSlash);
if (domain.indexOf(':') > -1){
var portColon = domain.indexOf(':');
domain = domain.substring(0, portColon);
}
return domain;
},
//
// Get the value from the params attribute string.
//
_getParameter : function(parameter, parameters) {
if (!this.element) return false;
if (parameter == 'lightwindow_top' && this.element.top) {
return unescape(this.element.top);
} else if (parameter == 'lightwindow_left' && this.element.left) {
return unescape(this.element.left);
} else if (parameter == 'lightwindow_type' && this.element.type) {
return unescape(this.element.type);
} else if (parameter == 'lightwindow_show_images' && this.element.showImages) {
return unescape(this.element.showImages);
} else if (parameter == 'lightwindow_height' && this.element.height) {
return unescape(this.element.height);
} else if (parameter == 'lightwindow_width' && this.element.width) {
return unescape(this.element.width);
} else if (parameter == 'lightwindow_loading_animation' && this.element.loadingAnimation) {
return unescape(this.element.loadingAnimation);
} else if (parameter == 'lightwindow_iframe_embed' && this.element.iframeEmbed) {
return unescape(this.element.iframeEmbed);
} else if (parameter == 'lightwindow_form' && this.element.form) {
return unescape(this.element.form);
} else {
if (!parameters) {
if (this.element.params) parameters = this.element.params;
else return;
}
var value;
var parameterArray = parameters.split(',');
var compareString = parameter+'=';
var compareLength = compareString.length;
for (var i = 0; i < parameterArray.length; i++) {
if (parameterArray[i].substr(0, compareLength) == compareString) {
var currentParameter = parameterArray[i].split('=');
value = currentParameter[1];
break;
}
}
if (!value) return false;
else return unescape(value);
}
},
//
// Get the Browser Viewport Dimensions
//
_browserDimensions : function() {
if (Prototype.Browser.IE) {
this.dimensions.viewport.height = document.documentElement.clientHeight;
this.dimensions.viewport.width = document.documentElement.clientWidth;
} else {
this.dimensions.viewport.height = window.innerHeight;
this.dimensions.viewport.width = document.width || document.body.offsetWidth;
}
},
//
// Get the scrollbar offset, I don't like this method but there is really no other way I can find.
//
_getScrollerWidth : function() {
var scrollDiv = Element.extend(document.createElement('div'));
scrollDiv.setAttribute('id', 'lightwindow_scroll_div');
scrollDiv.setStyle({
position: 'absolute',
top: '-10000px',
left: '-10000px',
width: '100px',
height: '100px',
overflow: 'hidden'
});
var contentDiv = Element.extend(document.createElement('div'));
contentDiv.setAttribute('id', 'lightwindow_content_scroll_div');
contentDiv.setStyle({
width: '100%',
height: '200px'
});
scrollDiv.appendChild(contentDiv);
var body = document.getElementsByTagName('body')[0];
body.appendChild(scrollDiv);
var noScroll = $('lightwindow_content_scroll_div').offsetWidth;
scrollDiv.style.overflow = 'auto';
var withScroll = $('lightwindow_content_scroll_div').offsetWidth;
Element.remove($('lightwindow_scroll_div'));
this.scrollbarOffset = noScroll-withScroll;
},
//
// Add a param to an object dynamically created
//
_addParamToObject : function(name, value, object, id) {
var param = document.createElement('param');
param.setAttribute('value', value);
param.setAttribute('name', name);
if (id) {
param.setAttribute('id', id);
}
object.appendChild(param);
return object;
},
//
// Get the outer HTML of an object CROSS BROWSER
//
_outerHTML : function(object) {
if (Prototype.Browser.IE) {
return object.outerHTML;
} else {
var clone = object.cloneNode(true);
var cloneDiv = document.createElement('div');
cloneDiv.appendChild(clone);
return cloneDiv.innerHTML;
}
},
//
// Convert an object to markup
//
_convertToMarkup : function(object, closeTag) {
var markup = this._outerHTML(object).replace('</'+closeTag+'>', '');
if (Prototype.Browser.IE) {
for (var i = 0; i < object.childNodes.length; i++){
markup += this._outerHTML(object.childNodes[i]);
}
markup += '</'+closeTag+'>';
}
return markup;
},
//
// Depending what type of browser it is we have to append the object differently... DAMN YOU IE!!
//
_appendObject : function(object, closeTag, appendTo) {
if (Prototype.Browser.IE) {
appendTo.innerHTML += this._convertToMarkup(object, closeTag);
// Fix the Eolas activate thing but only for specified media, for example doing this to a quicktime film breaks it.
if (this.options.EOLASFix.indexOf(this._fileType(this.element.href)) > -1) {
var objectElements = document.getElementsByTagName('object');
for (var i = 0; i < objectElements.length; i++) {
if (objectElements[i].getAttribute("data")) objectElements[i].removeAttribute('data');
objectElements[i].outerHTML = objectElements[i].outerHTML;
objectElements[i].style.visibility = "visible";
}
}
} else {
appendTo.appendChild(object);
}
},
//
// Add in iframe
//
_appendIframe : function(scroll) {
var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'lightwindow_iframe');
iframe.setAttribute('name', 'lightwindow_iframe');
iframe.setAttribute('src', 'about:blank');
iframe.setAttribute('height', '100%');
iframe.setAttribute('width', '100%');
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('marginwidth', '0');
iframe.setAttribute('marginheight', '0');
iframe.setAttribute('scrolling', scroll);
this._appendObject(iframe, 'iframe', $('lightwindow_contents'));
},
//
// Write Content to the iframe using the skin
//
_writeToIframe : function(content) {
var template = this.options.skin.iframe;
template = template.replace('{body_replace}', content);
if ($('lightwindow_iframe').contentWindow){
$('lightwindow_iframe').contentWindow.document.open();
$('lightwindow_iframe').contentWindow.document.write(template);
$('lightwindow_iframe').contentWindow.document.close();
} else {
$('lightwindow_iframe').contentDocument.open();
$('lightwindow_iframe').contentDocument.write(template);
$('lightwindow_iframe').contentDocument.close();
}
},
_checkImage : function(img) {
// During the onload event, IE correctly identifies any images
// that weren't downloaded as not complete. Others should too.
// Gecko-based browsers act like NS4 in that they report this
// incorrectly: they always return true.
// if (!img.complete) {
// return false;
// }
// However, they do have two very useful properties: naturalWidth
// and naturalHeight. These give the true size of the image. If
// it failed to load, either of these should be zero.
if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
return false;
}
console.log(img);
// No other way of checking: assume it's ok.
return true;
},
//
// Load the window Information
//
_loadWindow : function() {
switch (this.windowType) {
case 'image' :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -