1 /**
  2  * xiamiMusic button
  3  * @author yiminghe@gmail.com
  4  */
  5 KISSY.add("editor/plugin/xiamiMusic/index", function (S, Editor, FlashBaseClass, flashUtils) {
  6     var CLS_XIAMI = "ke_xiami",
  7         TYPE_XIAMI = "xiamiMusic";
  8 
  9     function XiamiMusic() {
 10         XiamiMusic.superclass.constructor.apply(this, arguments);
 11     }
 12 
 13     S.extend(XiamiMusic, FlashBaseClass, {
 14         _updateTip:function (tipUrlEl, selectedFlash) {
 15             var self = this,
 16                 editor = self.get("editor"),
 17                 r = editor.restoreRealElement(selectedFlash);
 18             if (r) {
 19                 tipUrlEl.html(selectedFlash.attr("title"));
 20                 tipUrlEl.attr("href", self._getFlashUrl(r));
 21             }
 22         }
 23     });
 24 
 25 
 26     function XiamiMusicPlugin(config) {
 27         this.config=config||{};
 28     }
 29 
 30     S.augment(XiamiMusicPlugin, {
 31         renderUI:function (editor) {
 32 
 33 
 34             var dataProcessor = editor.htmlDataProcessor,
 35                 dataFilter = dataProcessor && dataProcessor.dataFilter;
 36 
 37             function checkXiami(url) {
 38                 return /xiami\.com/i.test(url);
 39             }
 40 
 41             dataFilter && dataFilter.addRules({
 42                 tags:{
 43                     'object':function (element) {
 44                         var //增加音乐名字提示
 45                             title = element.getAttribute("title"),
 46                             i,
 47                             c,
 48                             classId = element.getAttribute("classid");
 49                         var childNodes = element.childNodes;
 50                         if (!classId) {
 51                             // Look for the inner <embed>
 52                             for (i = 0; i < childNodes.length; i++) {
 53                                 c = childNodes[ i ];
 54                                 if (c.nodeName == 'embed') {
 55                                     if (!flashUtils.isFlashEmbed(c)) {
 56                                         return null;
 57                                     }
 58                                     if (checkXiami(c.attributes.src)) {
 59                                         return dataProcessor.createFakeParserElement(element, CLS_XIAMI, TYPE_XIAMI, true, {
 60                                             title:title
 61                                         });
 62                                     }
 63                                 }
 64                             }
 65                             return null;
 66                         }
 67                         for (i = 0; i < childNodes.length; i++) {
 68                             c = childNodes[ i ];
 69                             //innerHTML 会莫名首字母大写,还会加入一些属性
 70                             //Movie
 71                             if (c.nodeName == 'param'
 72                                 && c.getAttribute("name") == "movie") {
 73                                 if (checkXiami(c.getAttribute("value"))) {
 74                                     return dataProcessor.createFakeParserElement(element,
 75                                         CLS_XIAMI, TYPE_XIAMI, true, {
 76                                             title:title
 77                                         });
 78                                 }
 79                             }
 80                         }
 81                     },
 82 
 83                     'embed':function (element) {
 84                         if (flashUtils.isFlashEmbed(element) &&
 85                             checkXiami(element.getAttribute("src"))) {
 86                             return dataProcessor.createFakeParserElement(element,
 87                                 CLS_XIAMI, TYPE_XIAMI, true, {
 88                                     title:element.getAttribute("title")
 89                                 });
 90                         }
 91                     }
 92                     //4 比 flash 的优先级 5 高!
 93                 }}, 4);
 94 
 95             var xiamiMusic = new XiamiMusic({
 96                 editor:editor,
 97                 cls:CLS_XIAMI,
 98                 type:TYPE_XIAMI,
 99                 bubbleId:"xiami",
100                 pluginConfig:this.config,
101                 contextMenuId:"xiami",
102                 contextMenuHandlers:{
103                     虾米属性:function () {
104                         var selectedEl = this.get("editorSelectedEl");
105                         if (selectedEl) {
106                             xiamiMusic.show(selectedEl);
107                         }
108                     }
109                 }
110             });
111 
112             editor.addButton("xiamiMusic", {
113                 tooltip:"插入虾米音乐",
114                 listeners:{
115                     click:function () {
116                         xiamiMusic.show();
117                     }
118                 },
119                 mode:Editor.WYSIWYG_MODE
120             });
121 
122         }
123     });
124 
125 
126     return XiamiMusicPlugin;
127 }, {
128     requires:['editor', '../flashCommon/baseClass', '../flashCommon/utils']
129 });