window.js
来自「ruby on rails web敏捷开发之路第二版 源代码」· JavaScript 代码 · 共 861 行 · 第 1/2 页
JS
861 行
width = this.maxWidth; this.width = width; this.height = height; Element.setStyle(this.element,{width: width + 'px'}); Element.setStyle(this.element,{height: height + 'px'}); // Update content height var content = $(this.element.id + '_content') Element.setStyle(content,{height: height + 'px'}); Element.setStyle(content,{width: width + 'px'}); }, // Brings window to front toFront: function() { windows = document.getElementsByClassName("dialog"); var maxIndex= 0; for (var i = 0; i<windows.length; i++){ if (maxIndex < parseFloat(windows[i].style.zIndex)) maxIndex = windows[i].style.zIndex; } this.element.style.zIndex = parseFloat(maxIndex) +1; }, // Displays window modal state or not show: function(modal) { if (modal) { WindowUtilities.disableScreen(this.className); this.modal = true; this.setZIndex(Windows.maxZIndex + 20); Windows.unsetOverflow(this); Event.observe(document, "keypress", this.eventKeyPress); } this.setSize(this.width, this.height); if (this.showEffectOptions ) this.showEffect(this.element, this.showEffectOptions); else this.showEffect(this.element); this._checkIEOverlapping(); }, // Displays window modal state or not at the center of the page showCenter: function(modal) { this.setSize(this.width, this.height); this.center(); this.show(modal); }, center: function() { var arrayPageSize = WindowUtilities.getPageSize(); var arrayPageScroll = WindowUtilities.getPageScroll(); this.element.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - this.height) / 2) + 'px'); this.element.style.left = (((arrayPageSize[0] - this.width) / 2) + 'px'); }, // Hides window hide: function() { if (this.modal) { WindowUtilities.enableScreen(); Windows.resetOverflow(); Event.stopObserving(document, "keypress", this.eventKeyPress); } // To avoid bug on scrolling bar Element.setStyle(this.getContent(), {overflow: "hidden"}); if (this.hideEffectOptions) this.hideEffect(this.element, this.hideEffectOptions); else this.hideEffect(this.element); if(this.iefix) Element.hide(this.iefix); }, _checkIEOverlapping: function() { if(!this.iefix && (navigator.appVersion.indexOf('MSIE')>0) && (navigator.userAgent.indexOf('Opera')<0) && (Element.getStyle(this.element, 'position')=='absolute')) { new Insertion.After(this.element.id, '<iframe id="' + this.element.id + '_iefix" '+ 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>'); this.iefix = $(this.element.id+'_iefix'); } if(this.iefix) setTimeout(this._fixIEOverlapping.bind(this), 50); }, _fixIEOverlapping: function() { Position.clone(this.element, this.iefix); this.iefix.style.zIndex = this.element.style.zIndex - 1; Element.show(this.iefix); }, setOpacity: function(opacity) { if (Element.setOpacity) Element.setOpacity(this.element, opacity); }, setZIndex: function(zindex) { Element.setStyle(this.element,{zIndex: zindex}); Windows.updateZindex(zindex, this); }};// Windows containers, register all page windowsvar Windows = { windows: [], observers: [], maxZIndex: 0, addObserver: function(observer) { this.observers.push(observer); }, removeObserver: function(observer) { this.observers = this.observers.reject( function(o) { return o==observer }); }, notify: function(eventName, win) { // onStartResize(), onEndResize(), onStartMove(), onEndMove(), onClose(), onDestroy() this.observers.each( function(o) {if(o[eventName]) o[eventName](eventName, win);}); }, // Gets window from its id getWindow: function(id) { return this.windows.detect(function(d) { return d.getId() ==id }); }, // Registers a new window (called by Windows constructor) register: function(win) { this.windows.push(win); }, // Unregisters a window (called by Windows destructor) unregister: function(win) { this.windows = this.windows.reject(function(d) { return d==win }); }, // Closes a window with its id close: function(id) { win = this.getWindow(id); // Asks delegate if exists if (win.getDelegate() && ! win.getDelegate().canClose(win)) return; if (win) { this.notify("onClose", win); win.hide(); if (win.destroyOnClose) win.destroy(); } }, unsetOverflow: function(except) { this.windows.each(function(d) { d.oldOverflow = Element.getStyle(d.getContent(), "overflow") || "auto" ; Element.setStyle(d.getContent(), {overflow: "hidden"}) }); if (except && except.oldOverflow) Element.setStyle(except.getContent(), {overflow: except.oldOverflow}); }, resetOverflow: function() { this.windows.each(function(d) { if (d.oldOverflow) Element.setStyle(d.getContent(), {overflow: d.oldOverflow}) }); }, updateZindex: function(zindex, win) { if (zindex > this.maxZIndex) this.maxZIndex = zindex; }};var Dialog = { win: null, confirm: function(message, parameters) { var okLabel = parameters && parameters.okLabel ? parameters.okLabel : "Ok"; var cancelLabel = parameters && parameters.cancelLabel ? parameters.cancelLabel : "Cancel"; var windowParam = parameters ? parameters.windowParameters : {}; windowParam.className = windowParam.className || "alert"; var content = "\ <div class='" + windowParam.className + "_message'>" + message + "</div>\ <div class='" + windowParam.className + "_buttons'>\ <input type='button' value='" + okLabel + "' onclick='Dialog.okCallback()'/>\ <input type='button' value='" + cancelLabel + "' onclick='Dialog.cancelCallback()'/>\ </div>\ "; return this.openDialog(content, parameters) }, alert: function(message, parameters) { var okLabel = parameters && parameters.okLabel ? parameters.okLabel : "Ok"; var windowParam = parameters ? parameters.windowParameters : {}; windowParam.className = windowParam.className || "alert"; var content = "\ <div class='" + windowParam.className + "_message'>" + message + "</div>\ <div class='" + windowParam.className + "_buttons'>\ <input type='button' value='" + okLabel + "' onclick='Dialog.okCallback()'/>\ </div>\ "; return this.openDialog(content, parameters) }, openDialog: function(content, parameters) { // remove old dialog if (this.win) this.win.destroy(); var windowParam = parameters ? parameters.windowParameters : {}; windowParam.resizable = windowParam.resizable || false; windowParam.effectOptions = {duration: 1}; this.win = new Window('modal_dialog', windowParam); this.win.getContent().innerHTML = content; this.win.showCenter(true); this.win.cancelCallback = parameters.cancel; this.win.okCallback = parameters.ok; this.eventResize = this.recenter.bindAsEventListener(this); Event.observe(window, "resize", this.eventResize); Event.observe(window, "scroll", this.eventResize); return this.win; }, okCallback: function() { this.win.hide(); Event.stopObserving(window, "resize", this.eventResize); Event.stopObserving(window, "scroll", this.eventResize); if (this.win.okCallback) this.win.okCallback(this.win); }, cancelCallback: function() { this.win.hide(); Event.stopObserving(window, "resize", this.eventResize); Event.stopObserving(window, "scroll", this.eventResize); if (this.win.cancelCallback) this.win.cancelCallback(win); }, recenter: function(event) { var arrayPageSize = WindowUtilities.getPageSize(); // set height of Overlay to take up whole page and show $('overlay_modal').style.height = (arrayPageSize[1] + 'px'); this.win.center(); }}/* Based on Lightbox JS: Fullsize Image Overlays by Lokesh Dhakar - http://www.huddletogether.com For more information on this script, visit: http://huddletogether.com/projects/lightbox/ Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/ (basically, do anything you want, just leave my name and link)*/var isIE = navigator.appVersion.match(/MSIE/) == "MSIE";//// getPageScroll()// Returns array with x,y page scroll values.// Core code from - quirksmode.org//var WindowUtilities = { getPageScroll :function() { var yScroll; if (self.pageYOffset) { yScroll = self.pageYOffset; } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict yScroll = document.documentElement.scrollTop; } else if (document.body) {// all other Explorers yScroll = document.body.scrollTop; } arrayPageScroll = new Array('',yScroll) return arrayPageScroll; }, // getPageSize() // Returns array with page width, height and window width, height // Core code from - quirksmode.org // Edit for Firefox by pHaez getPageSize: function(){ var xScroll, yScroll; if (window.innerHeight && window.scrollMaxY) { xScroll = document.body.scrollWidth; yScroll = window.innerHeight + window.scrollMaxY; } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight; } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight; } var windowWidth, windowHeight; if (self.innerHeight) { // all except Explorer windowWidth = self.innerWidth; windowHeight = self.innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight; } else if (document.body) { // other Explorers windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight; } // for small pages with total height less then height of the viewport if(yScroll < windowHeight){ pageHeight = windowHeight; } else { pageHeight = yScroll; } // for small pages with total width less then width of the viewport if(xScroll < windowWidth){ pageWidth = windowWidth; } else { pageWidth = xScroll; } arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) return arrayPageSize; }, disableScreen: function(className) { WindowUtilities.initLightbox(className); var objBody = document.getElementsByTagName("body").item(0); // prep objects var objOverlay = $('overlay_modal'); var arrayPageSize = WindowUtilities.getPageSize(); // Hide select boxes as they will 'peek' through the image in IE if (isIE) { selects = document.getElementsByTagName("select"); for (var i = 0; i != selects.length; i++) { selects[i].style.visibility = "hidden"; } } // set height of Overlay to take up whole page and show objOverlay.style.height = (arrayPageSize[1] + 'px'); objOverlay.style.display = 'block'; }, enableScreen: function() { var objOverlay = $('overlay_modal'); if (objOverlay) { // hide lightbox and overlay objOverlay.style.display = 'none'; // make select boxes visible if (isIE) { selects = document.getElementsByTagName("select"); for (var i = 0; i != selects.length; i++) { selects[i].style.visibility = "visible"; } } objOverlay.parentNode.removeChild(objOverlay); } }, // initLightbox() // Function runs on window load, going through link tags looking for rel="lightbox". // These links receive onclick events that enable the lightbox display for their targets. // The function also inserts html markup at the top of the page which will be used as a // container for the overlay pattern and the inline image. initLightbox: function(className) { // Already done, just update zIndex if ($('overlay_modal')) { Element.setStyle('overlay_modal', {zIndex: Windows.maxZIndex + 10}); } // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file) else { var objBody = document.getElementsByTagName("body").item(0); var objOverlay = document.createElement("div"); objOverlay.setAttribute('id', 'overlay_modal'); objOverlay.className = "overlay_" + className objOverlay.style.display = 'none'; objOverlay.style.position = 'absolute'; objOverlay.style.top = '0'; objOverlay.style.left = '0'; objOverlay.style.zIndex = Windows.maxZIndex + 10; objOverlay.style.width = '100%'; objBody.insertBefore(objOverlay, objBody.firstChild); } }, setCookie: function(value, parameters) { document.cookie= parameters[0] + "=" + escape(value) + ((parameters[1]) ? "; expires=" + parameters[1].toGMTString() : "") + ((parameters[2]) ? "; path=" + parameters[2] : "") + ((parameters[3]) ? "; domain=" + parameters[3] : "") + ((parameters[4]) ? "; secure" : ""); }, getCookie: function(name) { var dc = document.cookie; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else { begin += 2; } var end = document.cookie.indexOf(";", begin); if (end == -1) { end = dc.length; } return unescape(dc.substring(begin + prefix.length, end)); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?