1 ; 2 (function($we, $) { 3 4 var init = function(option) { 5 this.items = []; 6 this.itemNames = []; 7 this.option = option || {}; 8 this.option.tagName = this.option.tagName || "pre"; 9 }; 10 11 var start = function() { 12 var me = this; 13 $(document).ready(function() { 14 var bIncluded = typeof SyntaxHighlighter == "undefined"; 15 if (bIncluded) 16 _include.call(me); 17 18 _setItems.call(me); 19 20 _renderDir.call(me); 21 22 if (!bIncluded) 23 _renderCodes(); 24 }); 25 }; 26 27 var setOption = function(opt) { 28 if (typeof opt != "object") opt = {}; 29 30 for (var i in opt) { 31 this.option[i] = opt[i]; 32 } 33 }; 34 35 var _include = function() { 36 37 var path = $we.conf.ENV.__STATIC + '/js/plugin/syntaxhighlighter/'; 38 39 $we.utils.include('scripts/shCore.js', path, function() { 40 $we.utils.include(['scripts/shBrushJScript.js', 'scripts/shBrushXml.js'], path, function() { 41 _renderCodes(); 42 }); 43 }); 44 45 $we.utils.include('styles/shCoreDefault.css', path); 46 }; 47 48 var _setItems = function() { 49 var me = this; 50 $(this.option.tagName+'[attr="shrunner"]').each(function(index) { 51 var code = $(this).html(), 52 title = $(this).attr("title") || "", 53 type = $(this).attr("class") || "js"; 54 55 var item = $we.widget.add({ 56 name: "syntaxhighlighter.runner.item", 57 notifyTo: me 58 }, this, type, code, title, index+1); 59 60 me.items.push(item); 61 me.itemNames.push(title); 62 }); 63 }; 64 65 var _renderCodes = function() { 66 var __render = function() { 67 if (typeof SyntaxHighlighter != "undefined" 68 && typeof SyntaxHighlighter.brushes != "undefined" 69 && typeof SyntaxHighlighter.brushes.JScript != "undefined") { 70 SyntaxHighlighter.defaults['toolbar'] = false; 71 SyntaxHighlighter.highlight(); 72 } else { 73 setTimeout(__render, 200); 74 } 75 }; 76 77 __render(); 78 79 }; 80 81 var _renderDir = function() { 82 this.dir = $we.widget.add({ 83 name: "syntaxhighlighter.runner.directory", 84 notifyTo: this 85 }, this.itemNames); 86 }; 87 88 $we.widget.reg("syntaxhighlighter.runner", { 89 interfaces: { 90 start: start, 91 setOption: setOption 92 }, 93 init: init 94 }) 95 96 $we.comp.shrunner = $we.widget.add("syntaxhighlighter.runner"); 97 98 })($we, jQuery); 99 100 (function($we, $) { 101 var _html = [ 102 '<div id="shrunner_box_$index$" class="we_sh_box" attr="inner:root">', 103 '<h2>$title$</h2>', 104 '<div attr="inner:codeContainer">', 105 '<pre class="brush: $type$;" attr="inner:code">$code$</pre>', 106 '</div>', 107 // '<textarea attr="inner:editCode" style="width:500px;height:200px;border:1px solid;display:none">$code$</textarea>', 108 '<div>', 109 // '<button attr="inner:save;click:save" style="display:none;">保存代码</button>', 110 // '<button attr="inner:edit;click:edit">编辑代码</button>', 111 '<button attr="inner:run;click:run">运行代码</button>', 112 '</div>', 113 '</div>'].join(""); 114 115 var _codeHtml = '<pre class="brush: $type$;" attr="inner:code">$code$</pre>'; 116 117 var _tmpDiv = document.createElement("div"); 118 119 var init = function(el, type, code, title, index, option) { 120 this.type = type || "js"; 121 this.code = code || ""; 122 this.title = title || ""; 123 this.index = index || 1; 124 125 this.append(_tmpDiv, _html, { 126 type: this.type, 127 code: this.code, 128 title: this.title, 129 index: this.index 130 }); 131 132 $(this.node.root).insertBefore(el); 133 134 if (this.type != "js") 135 $(this.node.run).hide(); 136 137 $(el).remove(); 138 $(_tmpDiv).empty(); 139 }; 140 141 // var evtSave = function() { 142 // var curCode = $(this.node.editCode).val(); 143 // if (curCode != this.code) { 144 // this.code = curCode; 145 146 // $(this.node.codeContainer).empty(); 147 // _appendCode.call(this); 148 149 // SyntaxHighlighter.highlight(null, this.node.code); 150 // } 151 152 // $(this.node.editCode).hide(); 153 // $(this.node.save).hide(); 154 155 // }; 156 157 // var evtEdit = function() { 158 // $(this.node.editCode).show(); 159 // $(this.node.save).show(); 160 // }; 161 162 var evtRun = function() { 163 var code = this.node.codeContainer.childNodes[0].code || this.code; 164 try { 165 $we.utils.exec(code, true); 166 } catch (e) {} 167 }; 168 169 $we.widget.reg("syntaxhighlighter.runner.item", { 170 events: { 171 // save: evtSave, 172 // edit: evtEdit, 173 run: evtRun 174 }, 175 init: init 176 }) 177 178 })($we, jQuery); 179 180 (function() { 181 182 var _html = [ 183 '<div class="we_sh_directory">', 184 '<h3>目录</h3>', 185 '<a class="toggler" href="javascript:void(0);" attr="click:toggle">[收起]</a>', 186 '<div attr="inner:content"><ol attr="inner:ol"></ol></div>', 187 '</div>' 188 ].join(""); 189 190 var _item = '<li><a href="#shrunner_box_$index$" attr="click:go" name="#shrunner_box_$index$">$name$</a></li>'; 191 192 var init = function(items) { 193 this.append(document.body, _html); 194 195 var me = this; 196 $(items).each(function(index) { 197 me.append(me.node.ol, _item, { 198 index: index+1, 199 name: this.toString() 200 }); 201 }); 202 }; 203 204 var evtGo = function(e) { 205 var el = e.target, 206 to = $(el).attr("name"); 207 208 var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body'); 209 210 $($body).animate({scrollTop: $(to).offset().top}, 300, function() { 211 window.location.hash = to; 212 }); 213 214 e.preventDefault(); 215 }; 216 217 var evtToggle = function(e) { 218 var el = e.target; 219 $(this.node.content).slideToggle(300, function() { 220 if ($(this).is(":hidden")) 221 $(el).html("[展开]"); 222 else 223 $(el).html("[收起]"); 224 }); 225 }; 226 227 $we.widget.reg("syntaxhighlighter.runner.directory", { 228 events: { 229 go: evtGo, 230 toggle: evtToggle 231 }, 232 init: init 233 }); 234 })(); 235 236 define(["core"], function() { 237 return $we.comp.shrunner; 238 }); 239