123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- ///import core
- ///import uicore
- ///import ui/mask.js
- ///import ui/button.js
- (function (){
- var utils = baidu.editor.utils,
- domUtils = baidu.editor.dom.domUtils,
- uiUtils = baidu.editor.ui.uiUtils,
- Mask = baidu.editor.ui.Mask,
- UIBase = baidu.editor.ui.UIBase,
- Button = baidu.editor.ui.Button,
- Dialog = baidu.editor.ui.Dialog = function (options){
- this.initOptions(utils.extend({
- autoReset: true,
- draggable: true,
- onok: function (){},
- oncancel: function (){},
- onclose: function (t, ok){
- return ok ? this.onok() : this.oncancel();
- }
- },options));
- this.initDialog();
- };
- var modalMask;
- var dragMask;
- Dialog.prototype = {
- draggable: false,
- uiName: 'dialog',
- initDialog: function (){
- var me = this,
- theme=this.editor.options.theme;
- this.initUIBase();
- this.modalMask = (modalMask || (modalMask = new Mask({
- className: 'edui-dialog-modalmask',
- theme:theme
- })));
- this.dragMask = (dragMask || (dragMask = new Mask({
- className: 'edui-dialog-dragmask',
- theme:theme
- })));
- this.closeButton = new Button({
- className: 'edui-dialog-closebutton',
- title: me.closeDialog,
- theme:theme,
- onclick: function (){
- me.editor.curInput=null;
- me.editor.curOpinion = null;
- me.close(false);
- }
- });
- if (this.buttons) {
- for (var i=0; i<this.buttons.length; i++) {
- if (!(this.buttons[i] instanceof Button)) {
- this.buttons[i] = new Button(this.buttons[i]);
- }
- }
- }
- },
- fitSize: function (){
- var popBodyEl = this.getDom('body');
- // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
- // uiUtils.removeStyle(popBodyEl, 'width');
- // uiUtils.removeStyle(popBodyEl, 'height');
- // }
- var size = this.mesureSize();
- popBodyEl.style.width = size.width + 'px';
- popBodyEl.style.height = size.height + 'px';
- return size;
- },
- safeSetOffset: function (offset){
- var me = this;
- var el = me.getDom();
- var vpRect = uiUtils.getViewportRect();
- var rect = uiUtils.getClientRect(el);
- var left = offset.left;
- if (left + rect.width > vpRect.right) {
- left = vpRect.right - rect.width;
- }
- var top = offset.top;
- if (top + rect.height > vpRect.bottom) {
- top = vpRect.bottom - rect.height;
- }
- el.style.left = Math.max(left, 0) + 'px';
- el.style.top = Math.max(top, 0) + 'px';
- },
- showAtCenter: function (){
- this.getDom().style.display = '';
- var vpRect = uiUtils.getViewportRect();
- var popSize = this.fitSize();
- var titleHeight = this.getDom('titlebar').offsetHeight | 0;
- var left = vpRect.width / 2 - popSize.width / 2;
- var top = vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
- var popEl = this.getDom();
- this.safeSetOffset({
- left: Math.max(left | 0, 0),
- top: Math.max(top | 0, 0)
- });
- if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
- popEl.className += ' edui-state-centered';
- }
- this._show();
- },
- getContentHtml: function (){
- var contentHtml = '';
- if (typeof this.content == 'string') {
- contentHtml = this.content;
- } else if (this.iframeUrl) {
- contentHtml = '<span id="'+ this.id +'_contmask" class="dialogcontmask"></span><iframe id="'+ this.id +
- '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="'+ this.iframeUrl +'"></iframe>';
- }
- return contentHtml;
- },
- getHtmlTpl: function (){
- var footHtml = '';
- if (this.buttons) {
- var buff = [];
- for (var i=0; i<this.buttons.length; i++) {
- buff[i] = this.buttons[i].renderHtml();
- }
- footHtml = '<div class="%%-foot">' +
- '<div id="##_buttons" class="%%-buttons">' + buff.join('') + '</div>' +
- '</div>';
- }
- return '<div id="##" class="%%"><div class="%%-wrap"><div id="##_body" class="%%-body">' +
- '<div class="%%-shadow"></div>' +
- '<div id="##_titlebar" class="%%-titlebar">' +
- '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
- '<span class="%%-caption">' + (this.title || '') + '</span>' +
- '</div>' +
- this.closeButton.renderHtml() +
- '</div>' +
- '<div id="##_content" class="%%-content">'+ ( this.autoReset ? '' : this.getContentHtml()) +'</div>' +
- footHtml +
- '</div></div></div>';
- },
- postRender: function (){
- // todo: 保持居中/记住上次关闭位置选项
- if (!this.modalMask.getDom()) {
- this.modalMask.render();
- this.modalMask.hide();
- }
- if (!this.dragMask.getDom()) {
- this.dragMask.render();
- this.dragMask.hide();
- }
- var me = this;
- this.addListener('show', function (){
- me.modalMask.show(this.getDom().style.zIndex - 2);
- });
- this.addListener('hide', function (){
- me.modalMask.hide();
- });
- if (this.buttons) {
- for (var i=0; i<this.buttons.length; i++) {
- this.buttons[i].postRender();
- }
- }
- domUtils.on(window, 'resize', function (){
- setTimeout(function (){
- if (!me.isHidden()) {
- me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
- }
- });
- });
- this._hide();
- },
- mesureSize: function (){
- var body = this.getDom('body');
- var width = uiUtils.getClientRect(this.getDom('content')).width;
- var dialogBodyStyle = body.style;
- dialogBodyStyle.width = width;
- return uiUtils.getClientRect(body);
- },
- _onTitlebarMouseDown: function (evt, el){
- if (this.draggable) {
- var rect;
- var vpRect = uiUtils.getViewportRect();
- var me = this;
- uiUtils.startDrag(evt, {
- ondragstart: function (){
- rect = uiUtils.getClientRect(me.getDom());
- me.getDom('contmask').style.visibility = 'visible';
- me.dragMask.show(me.getDom().style.zIndex - 1);
- },
- ondragmove: function (x, y){
- var left = rect.left + x;
- var top = rect.top + y;
- me.safeSetOffset({
- left: left,
- top: top
- });
- },
- ondragstop: function (){
- me.getDom('contmask').style.visibility = 'hidden';
- domUtils.removeClasses(me.getDom(), ['edui-state-centered']);
- me.dragMask.hide();
- }
- });
- }
- },
- clearContent:function(){
- var d = this.getDom('');
- d.className = '';//重新设置dialog的样式
- d.innerHTML = '';
- },
- reset: function (){
- this.getDom('content').innerHTML = this.getContentHtml();
- },
- _show: function (){
- if (this._hidden) {
- this.getDom().style.display = '';
- //要高过编辑器的zindxe
- this.editor.container.style.zIndex && (this.getDom().style.zIndex = this.editor.container.style.zIndex * 1 + 10);
- this._hidden = false;
- this.fireEvent('show');
- baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = this.getDom().style.zIndex - 4;
- }
- },
- isHidden: function (){
- return this._hidden;
- },
- _hide: function (){
- if (!this._hidden) {
- this.getDom().style.display = 'none';
- this.getDom().style.zIndex = '';
- this._hidden = true;
- this.fireEvent('hide');
- }
- },
- open: function (){
- if (this.autoReset) {
- //有可能还没有渲染
- try{
- this.reset();
- }catch(e){
- this.render();
- this.open()
- }
- }
- this.showAtCenter();
- if (this.iframeUrl) {
- try {
- this.getDom('iframe').focus();
- } catch(ex){}
- }
- },
- _onCloseButtonClick: function (evt, el){
- this.close(false);
- },
- close: function (ok){
- if (this.fireEvent('close', ok) !== false) {
- this._hide();
- }
- }
- };
- utils.inherits(Dialog, UIBase);
- })();
|