1 /**
  2  *  BaseClass for Flash Based plugin.
  3  *  @author yiminghe@gmail.com
  4  */
  5 KISSY.add("editor/plugin/flashCommon/baseClass", function (S, Editor, ContextMenu, Bubble, DialogLoader, flashUtils) {
  6 
  7     var Node = S.Node;
  8 
  9     /**
 10      * 写成类的形式而不是一个简单的button命令配置,为了可以override
 11      * 所有基于 flash 的插件基类,使用 template 模式抽象
 12      */
 13     function Flash() {
 14         Flash.superclass.constructor.apply(this, arguments);
 15         this._init();
 16     }
 17 
 18     var tipHtml = ' <a ' +
 19         'class="ks-editor-bubble-url" ' +
 20         'target="_blank" ' +
 21         'href="#">{label}</a>   |   '
 22         + ' <span class="ks-editor-bubble-link ks-editor-bubble-change">编辑</span>   |   '
 23         + ' <span class="ks-editor-bubble-link ks-editor-bubble-remove">删除</span>';
 24 
 25     Flash.ATTRS = {
 26         cls:{},
 27         type:{},
 28         label:{
 29             value:"在新窗口查看"
 30         },
 31         bubbleId:{},
 32         contextMenuId:{},
 33         contextMenuHandlers:{}
 34     };
 35 
 36     S.extend(Flash, S.Base, {
 37         _init:function () {
 38             var self = this,
 39                 cls = self.get("cls"),
 40                 editor = self.get("editor"),
 41                 children = [],
 42                 bubbleId = self.get("bubbleId"),
 43                 contextMenuId = self.get("contextMenuId"),
 44                 contextMenuHandlers = self.get("contextMenuHandlers");
 45 
 46             S.each(contextMenuHandlers, function (h, content) {
 47                 children.push({
 48                     content:content
 49                 })
 50             });
 51 
 52             editor.addContextMenu(contextMenuId, "." + cls, {
 53                 width:"120px",
 54                 children:children,
 55                 listeners:{
 56                     click:function (e) {
 57                         var content = e.target.get("content");
 58                         if (contextMenuHandlers[content]) {
 59                             contextMenuHandlers[content].call(this);
 60                         }
 61                     }
 62                 }
 63             });
 64 
 65             editor.addBubble(bubbleId, function (el) {
 66                 return el.hasClass(cls, undefined) && el;
 67             }, {
 68                 listeners:{
 69                     afterRenderUI:// 注册泡泡,selectionChange时检测
 70                         function () {
 71                             var bubble = this,
 72                                 el = bubble.get("contentEl");
 73                             el.html(S.substitute(tipHtml, {
 74                                 label:self.get("label")
 75                             }));
 76                             var tipUrlEl = el.one(".ks-editor-bubble-url"),
 77                                 tipChangeEl = el.one(".ks-editor-bubble-change"),
 78                                 tipRemoveEl = el.one(".ks-editor-bubble-remove");
 79 
 80                             // ie focus not lose
 81                             Editor.Utils.preventFocus(el);
 82 
 83                             tipChangeEl.on("click", function (ev) {
 84                                 // 回调show,传入选中元素
 85                                 self.show(bubble.get("editorSelectedEl"));
 86                                 ev.halt();
 87                             });
 88 
 89                             tipRemoveEl.on("click", function (ev) {
 90                                 // chrome remove 后会没有焦点
 91                                 if (S.UA['webkit']) {
 92                                     var r = editor.getSelection().getRanges(),
 93                                         r0 = r && r[0];
 94                                     if (r0) {
 95                                         r0.collapse(true);
 96                                         r0.select();
 97                                     }
 98                                 }
 99                                 bubble.get("editorSelectedEl").remove();
100                                 bubble.hide();
101                                 editor.notifySelectionChange();
102                                 ev.halt();
103                             });
104 
105                             /*
106                              位置变化,在显示前就设置内容,防止ie6 iframe遮罩不能正确大小
107                              */
108                             bubble.on("show", function () {
109                                 var a = bubble.get("editorSelectedEl");
110                                 if (a) {
111                                     self._updateTip(tipUrlEl, a);
112                                 }
113                             });
114                         }
115                 }
116             })
117 
118 
119             editor.docReady(function () {
120                 //注册双击,双击时检测
121                 editor.get("document").on("dblclick", self._dbClick, self);
122             });
123         },
124 
125         /**
126          * 子类覆盖,如何从flash url得到合适的应用表示地址
127          * @override
128          * @param r flash 元素
129          */
130         _getFlashUrl:function (r) {
131             return flashUtils.getUrl(r);
132         },
133         /**
134          * 更新泡泡弹出的界面,子类覆盖
135          * @override
136          * @param tipUrlElEl
137          * @param selectedFlash
138          */
139         _updateTip:function (tipUrlElEl, selectedFlash) {
140             var self = this,
141                 editor = self.get("editor"),
142                 r = editor.restoreRealElement(selectedFlash);
143             if (!r) {
144                 return;
145             }
146             var url = self._getFlashUrl(r);
147             tipUrlElEl.attr("href", url);
148         },
149 
150         //根据图片标志触发本插件应用
151         _dbClick:function (ev) {
152             var self = this,
153                 t = new Node(ev.target);
154             if (t.nodeName() === "img" && t.hasClass(self.get("cls"), undefined)) {
155                 self.show(t);
156                 ev.halt();
157             }
158         },
159 
160         show:function (selectedEl) {
161             var self = this,
162                 editor = self.get("editor");
163             DialogLoader.useDialog(editor, self.get("type"),
164                 self.get("pluginConfig"),
165                 selectedEl);
166         }
167     });
168 
169     return Flash;
170 
171 }, {
172     requires:['editor', '../contextmenu/', '../bubble/', '../dialogLoader/', './utils']
173 });