2944f133d0fd49a6ddf2715b3a394a72a25e03a8.svn-base 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. // Copyright (c) 2009, Baidu Inc. All rights reserved.
  2. //
  3. // Licensed under the BSD License
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http:// tangram.baidu.com/license.html
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @namespace T Tangram七巧板
  16. * @name T
  17. * @version 1.6.0
  18. */
  19. /**
  20. * 声明baidu包
  21. * @author: allstar, erik, meizz, berg
  22. */
  23. var T,
  24. baidu = T = baidu || {version: "1.5.0"};
  25. baidu.guid = "$BAIDU$";
  26. baidu.$$ = window[baidu.guid] = window[baidu.guid] || {global:{}};
  27. /**
  28. * 使用flash资源封装的一些功能
  29. * @namespace baidu.flash
  30. */
  31. baidu.flash = baidu.flash || {};
  32. /**
  33. * 操作dom的方法
  34. * @namespace baidu.dom
  35. */
  36. baidu.dom = baidu.dom || {};
  37. /**
  38. * 从文档中获取指定的DOM元素
  39. * @name baidu.dom.g
  40. * @function
  41. * @grammar baidu.dom.g(id)
  42. * @param {string|HTMLElement} id 元素的id或DOM元素.
  43. * @shortcut g,T.G
  44. * @meta standard
  45. * @see baidu.dom.q
  46. *
  47. * @return {HTMLElement|null} 获取的元素,查找不到时返回null,如果参数不合法,直接返回参数.
  48. */
  49. baidu.dom.g = function(id) {
  50. if (!id) return null;
  51. if ('string' == typeof id || id instanceof String) {
  52. return document.getElementById(id);
  53. } else if (id.nodeName && (id.nodeType == 1 || id.nodeType == 9)) {
  54. return id;
  55. }
  56. return null;
  57. };
  58. baidu.g = baidu.G = baidu.dom.g;
  59. /**
  60. * 操作数组的方法
  61. * @namespace baidu.array
  62. */
  63. baidu.array = baidu.array || {};
  64. /**
  65. * 遍历数组中所有元素
  66. * @name baidu.array.each
  67. * @function
  68. * @grammar baidu.array.each(source, iterator[, thisObject])
  69. * @param {Array} source 需要遍历的数组
  70. * @param {Function} iterator 对每个数组元素进行调用的函数,该函数有两个参数,第一个为数组元素,第二个为数组索引值,function (item, index)。
  71. * @param {Object} [thisObject] 函数调用时的this指针,如果没有此参数,默认是当前遍历的数组
  72. * @remark
  73. * each方法不支持对Object的遍历,对Object的遍历使用baidu.object.each 。
  74. * @shortcut each
  75. * @meta standard
  76. *
  77. * @returns {Array} 遍历的数组
  78. */
  79. baidu.each = baidu.array.forEach = baidu.array.each = function (source, iterator, thisObject) {
  80. var returnValue, item, i, len = source.length;
  81. if ('function' == typeof iterator) {
  82. for (i = 0; i < len; i++) {
  83. item = source[i];
  84. returnValue = iterator.call(thisObject || source, item, i);
  85. if (returnValue === false) {
  86. break;
  87. }
  88. }
  89. }
  90. return source;
  91. };
  92. /**
  93. * 对语言层面的封装,包括类型判断、模块扩展、继承基类以及对象自定义事件的支持。
  94. * @namespace baidu.lang
  95. */
  96. baidu.lang = baidu.lang || {};
  97. /**
  98. * 判断目标参数是否为function或Function实例
  99. * @name baidu.lang.isFunction
  100. * @function
  101. * @grammar baidu.lang.isFunction(source)
  102. * @param {Any} source 目标参数
  103. * @version 1.2
  104. * @see baidu.lang.isString,baidu.lang.isObject,baidu.lang.isNumber,baidu.lang.isArray,baidu.lang.isElement,baidu.lang.isBoolean,baidu.lang.isDate
  105. * @meta standard
  106. * @returns {boolean} 类型判断结果
  107. */
  108. baidu.lang.isFunction = function (source) {
  109. return '[object Function]' == Object.prototype.toString.call(source);
  110. };
  111. /**
  112. * 判断目标参数是否string类型或String对象
  113. * @name baidu.lang.isString
  114. * @function
  115. * @grammar baidu.lang.isString(source)
  116. * @param {Any} source 目标参数
  117. * @shortcut isString
  118. * @meta standard
  119. * @see baidu.lang.isObject,baidu.lang.isNumber,baidu.lang.isArray,baidu.lang.isElement,baidu.lang.isBoolean,baidu.lang.isDate
  120. *
  121. * @returns {boolean} 类型判断结果
  122. */
  123. baidu.lang.isString = function (source) {
  124. return '[object String]' == Object.prototype.toString.call(source);
  125. };
  126. baidu.isString = baidu.lang.isString;
  127. /**
  128. * 判断浏览器类型和特性的属性
  129. * @namespace baidu.browser
  130. */
  131. baidu.browser = baidu.browser || {};
  132. /**
  133. * 判断是否为opera浏览器
  134. * @property opera opera版本号
  135. * @grammar baidu.browser.opera
  136. * @meta standard
  137. * @see baidu.browser.ie,baidu.browser.firefox,baidu.browser.safari,baidu.browser.chrome
  138. * @returns {Number} opera版本号
  139. */
  140. /**
  141. * opera 从10开始不是用opera后面的字符串进行版本的判断
  142. * 在Browser identification最后添加Version + 数字进行版本标识
  143. * opera后面的数字保持在9.80不变
  144. */
  145. baidu.browser.opera = /opera(\/| )(\d+(\.\d+)?)(.+?(version\/(\d+(\.\d+)?)))?/i.test(navigator.userAgent) ? + ( RegExp["\x246"] || RegExp["\x242"] ) : undefined;
  146. /**
  147. * 在目标元素的指定位置插入HTML代码
  148. * @name baidu.dom.insertHTML
  149. * @function
  150. * @grammar baidu.dom.insertHTML(element, position, html)
  151. * @param {HTMLElement|string} element 目标元素或目标元素的id
  152. * @param {string} position 插入html的位置信息,取值为beforeBegin,afterBegin,beforeEnd,afterEnd
  153. * @param {string} html 要插入的html
  154. * @remark
  155. *
  156. * 对于position参数,大小写不敏感<br>
  157. * 参数的意思:beforeBegin&lt;span&gt;afterBegin this is span! beforeEnd&lt;/span&gt; afterEnd <br />
  158. * 此外,如果使用本函数插入带有script标签的HTML字符串,script标签对应的脚本将不会被执行。
  159. *
  160. * @shortcut insertHTML
  161. * @meta standard
  162. *
  163. * @returns {HTMLElement} 目标元素
  164. */
  165. baidu.dom.insertHTML = function (element, position, html) {
  166. element = baidu.dom.g(element);
  167. var range,begin;
  168. if (element.insertAdjacentHTML && !baidu.browser.opera) {
  169. element.insertAdjacentHTML(position, html);
  170. } else {
  171. range = element.ownerDocument.createRange();
  172. position = position.toUpperCase();
  173. if (position == 'AFTERBEGIN' || position == 'BEFOREEND') {
  174. range.selectNodeContents(element);
  175. range.collapse(position == 'AFTERBEGIN');
  176. } else {
  177. begin = position == 'BEFOREBEGIN';
  178. range[begin ? 'setStartBefore' : 'setEndAfter'](element);
  179. range.collapse(begin);
  180. }
  181. range.insertNode(range.createContextualFragment(html));
  182. }
  183. return element;
  184. };
  185. baidu.insertHTML = baidu.dom.insertHTML;
  186. /**
  187. * 操作flash对象的方法,包括创建flash对象、获取flash对象以及判断flash插件的版本号
  188. * @namespace baidu.swf
  189. */
  190. baidu.swf = baidu.swf || {};
  191. /**
  192. * 浏览器支持的flash插件版本
  193. * @property version 浏览器支持的flash插件版本
  194. * @grammar baidu.swf.version
  195. * @return {String} 版本号
  196. * @meta standard
  197. */
  198. baidu.swf.version = (function () {
  199. var n = navigator;
  200. if (n.plugins && n.mimeTypes.length) {
  201. var plugin = n.plugins["Shockwave Flash"];
  202. if (plugin && plugin.description) {
  203. return plugin.description
  204. .replace(/([a-zA-Z]|\s)+/, "")
  205. .replace(/(\s)+r/, ".") + ".0";
  206. }
  207. } else if (window.ActiveXObject && !window.opera) {
  208. for (var i = 12; i >= 2; i--) {
  209. try {
  210. var c = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + i);
  211. if (c) {
  212. var version = c.GetVariable("$version");
  213. return version.replace(/WIN/g,'').replace(/,/g,'.');
  214. }
  215. } catch(e) {}
  216. }
  217. }
  218. })();
  219. /**
  220. * 操作字符串的方法
  221. * @namespace baidu.string
  222. */
  223. baidu.string = baidu.string || {};
  224. /**
  225. * 对目标字符串进行html编码
  226. * @name baidu.string.encodeHTML
  227. * @function
  228. * @grammar baidu.string.encodeHTML(source)
  229. * @param {string} source 目标字符串
  230. * @remark
  231. * 编码字符有5个:&<>"'
  232. * @shortcut encodeHTML
  233. * @meta standard
  234. * @see baidu.string.decodeHTML
  235. *
  236. * @returns {string} html编码后的字符串
  237. */
  238. baidu.string.encodeHTML = function (source) {
  239. return String(source)
  240. .replace(/&/g,'&amp;')
  241. .replace(/</g,'&lt;')
  242. .replace(/>/g,'&gt;')
  243. .replace(/"/g, "&quot;")
  244. .replace(/'/g, "&#39;");
  245. };
  246. baidu.encodeHTML = baidu.string.encodeHTML;
  247. /**
  248. * 创建flash对象的html字符串
  249. * @name baidu.swf.createHTML
  250. * @function
  251. * @grammar baidu.swf.createHTML(options)
  252. *
  253. * @param {Object} options 创建flash的选项参数
  254. * @param {string} options.id 要创建的flash的标识
  255. * @param {string} options.url flash文件的url
  256. * @param {String} options.errorMessage 未安装flash player或flash player版本号过低时的提示
  257. * @param {string} options.ver 最低需要的flash player版本号
  258. * @param {string} options.width flash的宽度
  259. * @param {string} options.height flash的高度
  260. * @param {string} options.align flash的对齐方式,允许值:middle/left/right/top/bottom
  261. * @param {string} options.base 设置用于解析swf文件中的所有相对路径语句的基本目录或URL
  262. * @param {string} options.bgcolor swf文件的背景色
  263. * @param {string} options.salign 设置缩放的swf文件在由width和height设置定义的区域内的位置。允许值:l/r/t/b/tl/tr/bl/br
  264. * @param {boolean} options.menu 是否显示右键菜单,允许值:true/false
  265. * @param {boolean} options.loop 播放到最后一帧时是否重新播放,允许值: true/false
  266. * @param {boolean} options.play flash是否在浏览器加载时就开始播放。允许值:true/false
  267. * @param {string} options.quality 设置flash播放的画质,允许值:low/medium/high/autolow/autohigh/best
  268. * @param {string} options.scale 设置flash内容如何缩放来适应设置的宽高。允许值:showall/noborder/exactfit
  269. * @param {string} options.wmode 设置flash的显示模式。允许值:window/opaque/transparent
  270. * @param {string} options.allowscriptaccess 设置flash与页面的通信权限。允许值:always/never/sameDomain
  271. * @param {string} options.allownetworking 设置swf文件中允许使用的网络API。允许值:all/internal/none
  272. * @param {boolean} options.allowfullscreen 是否允许flash全屏。允许值:true/false
  273. * @param {boolean} options.seamlesstabbing 允许设置执行无缝跳格,从而使用户能跳出flash应用程序。该参数只能在安装Flash7及更高版本的Windows中使用。允许值:true/false
  274. * @param {boolean} options.devicefont 设置静态文本对象是否以设备字体呈现。允许值:true/false
  275. * @param {boolean} options.swliveconnect 第一次加载flash时浏览器是否应启动Java。允许值:true/false
  276. * @param {Object} options.vars 要传递给flash的参数,支持JSON或string类型。
  277. *
  278. * @see baidu.swf.create
  279. * @meta standard
  280. * @returns {string} flash对象的html字符串
  281. */
  282. baidu.swf.createHTML = function (options) {
  283. options = options || {};
  284. var version = baidu.swf.version,
  285. needVersion = options['ver'] || '6.0.0',
  286. vUnit1, vUnit2, i, k, len, item, tmpOpt = {},
  287. encodeHTML = baidu.string.encodeHTML;
  288. for (k in options) {
  289. tmpOpt[k] = options[k];
  290. }
  291. options = tmpOpt;
  292. if (version) {
  293. version = version.split('.');
  294. needVersion = needVersion.split('.');
  295. for (i = 0; i < 3; i++) {
  296. vUnit1 = parseInt(version[i], 10);
  297. vUnit2 = parseInt(needVersion[i], 10);
  298. if (vUnit2 < vUnit1) {
  299. break;
  300. } else if (vUnit2 > vUnit1) {
  301. return '';
  302. }
  303. }
  304. } else {
  305. return '';
  306. }
  307. var vars = options['vars'],
  308. objProperties = ['classid', 'codebase', 'id', 'width', 'height', 'align'];
  309. options['align'] = options['align'] || 'middle';
  310. options['classid'] = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
  311. options['codebase'] = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0';
  312. options['movie'] = options['url'] || '';
  313. delete options['vars'];
  314. delete options['url'];
  315. if ('string' == typeof vars) {
  316. options['flashvars'] = vars;
  317. } else {
  318. var fvars = [];
  319. for (k in vars) {
  320. item = vars[k];
  321. fvars.push(k + "=" + encodeURIComponent(item));
  322. }
  323. options['flashvars'] = fvars.join('&');
  324. }
  325. var str = ['<object '];
  326. for (i = 0, len = objProperties.length; i < len; i++) {
  327. item = objProperties[i];
  328. str.push(' ', item, '="', encodeHTML(options[item]), '"');
  329. }
  330. str.push('>');
  331. var params = {
  332. 'wmode' : 1,
  333. 'scale' : 1,
  334. 'quality' : 1,
  335. 'play' : 1,
  336. 'loop' : 1,
  337. 'menu' : 1,
  338. 'salign' : 1,
  339. 'bgcolor' : 1,
  340. 'base' : 1,
  341. 'allowscriptaccess' : 1,
  342. 'allownetworking' : 1,
  343. 'allowfullscreen' : 1,
  344. 'seamlesstabbing' : 1,
  345. 'devicefont' : 1,
  346. 'swliveconnect' : 1,
  347. 'flashvars' : 1,
  348. 'movie' : 1
  349. };
  350. for (k in options) {
  351. item = options[k];
  352. k = k.toLowerCase();
  353. if (params[k] && (item || item === false || item === 0)) {
  354. str.push('<param name="' + k + '" value="' + encodeHTML(item) + '" />');
  355. }
  356. }
  357. options['src'] = options['movie'];
  358. options['name'] = options['id'];
  359. delete options['id'];
  360. delete options['movie'];
  361. delete options['classid'];
  362. delete options['codebase'];
  363. options['type'] = 'application/x-shockwave-flash';
  364. options['pluginspage'] = 'http://www.macromedia.com/go/getflashplayer';
  365. str.push('<embed');
  366. var salign;
  367. for (k in options) {
  368. item = options[k];
  369. if (item || item === false || item === 0) {
  370. if ((new RegExp("^salign\x24", "i")).test(k)) {
  371. salign = item;
  372. continue;
  373. }
  374. str.push(' ', k, '="', encodeHTML(item), '"');
  375. }
  376. }
  377. if (salign) {
  378. str.push(' salign="', encodeHTML(salign), '"');
  379. }
  380. str.push('></embed></object>');
  381. return str.join('');
  382. };
  383. /**
  384. * 在页面中创建一个flash对象
  385. * @name baidu.swf.create
  386. * @function
  387. * @grammar baidu.swf.create(options[, container])
  388. *
  389. * @param {Object} options 创建flash的选项参数
  390. * @param {string} options.id 要创建的flash的标识
  391. * @param {string} options.url flash文件的url
  392. * @param {String} options.errorMessage 未安装flash player或flash player版本号过低时的提示
  393. * @param {string} options.ver 最低需要的flash player版本号
  394. * @param {string} options.width flash的宽度
  395. * @param {string} options.height flash的高度
  396. * @param {string} options.align flash的对齐方式,允许值:middle/left/right/top/bottom
  397. * @param {string} options.base 设置用于解析swf文件中的所有相对路径语句的基本目录或URL
  398. * @param {string} options.bgcolor swf文件的背景色
  399. * @param {string} options.salign 设置缩放的swf文件在由width和height设置定义的区域内的位置。允许值:l/r/t/b/tl/tr/bl/br
  400. * @param {boolean} options.menu 是否显示右键菜单,允许值:true/false
  401. * @param {boolean} options.loop 播放到最后一帧时是否重新播放,允许值: true/false
  402. * @param {boolean} options.play flash是否在浏览器加载时就开始播放。允许值:true/false
  403. * @param {string} options.quality 设置flash播放的画质,允许值:low/medium/high/autolow/autohigh/best
  404. * @param {string} options.scale 设置flash内容如何缩放来适应设置的宽高。允许值:showall/noborder/exactfit
  405. * @param {string} options.wmode 设置flash的显示模式。允许值:window/opaque/transparent
  406. * @param {string} options.allowscriptaccess 设置flash与页面的通信权限。允许值:always/never/sameDomain
  407. * @param {string} options.allownetworking 设置swf文件中允许使用的网络API。允许值:all/internal/none
  408. * @param {boolean} options.allowfullscreen 是否允许flash全屏。允许值:true/false
  409. * @param {boolean} options.seamlesstabbing 允许设置执行无缝跳格,从而使用户能跳出flash应用程序。该参数只能在安装Flash7及更高版本的Windows中使用。允许值:true/false
  410. * @param {boolean} options.devicefont 设置静态文本对象是否以设备字体呈现。允许值:true/false
  411. * @param {boolean} options.swliveconnect 第一次加载flash时浏览器是否应启动Java。允许值:true/false
  412. * @param {Object} options.vars 要传递给flash的参数,支持JSON或string类型。
  413. *
  414. * @param {HTMLElement|string} [container] flash对象的父容器元素,不传递该参数时在当前代码位置创建flash对象。
  415. * @meta standard
  416. * @see baidu.swf.createHTML,baidu.swf.getMovie
  417. */
  418. baidu.swf.create = function (options, target) {
  419. options = options || {};
  420. var html = baidu.swf.createHTML(options)
  421. || options['errorMessage']
  422. || '';
  423. if (target && 'string' == typeof target) {
  424. target = document.getElementById(target);
  425. }
  426. baidu.dom.insertHTML( target || document.body ,'beforeEnd',html );
  427. };
  428. /**
  429. * 判断是否为ie浏览器
  430. * @name baidu.browser.ie
  431. * @field
  432. * @grammar baidu.browser.ie
  433. * @returns {Number} IE版本号
  434. */
  435. baidu.browser.ie = baidu.ie = /msie (\d+\.\d+)/i.test(navigator.userAgent) ? (document.documentMode || + RegExp['\x241']) : undefined;
  436. /**
  437. * 移除数组中的项
  438. * @name baidu.array.remove
  439. * @function
  440. * @grammar baidu.array.remove(source, match)
  441. * @param {Array} source 需要移除项的数组
  442. * @param {Any} match 要移除的项
  443. * @meta standard
  444. * @see baidu.array.removeAt
  445. *
  446. * @returns {Array} 移除后的数组
  447. */
  448. baidu.array.remove = function (source, match) {
  449. var len = source.length;
  450. while (len--) {
  451. if (len in source && source[len] === match) {
  452. source.splice(len, 1);
  453. }
  454. }
  455. return source;
  456. };
  457. /**
  458. * 判断目标参数是否Array对象
  459. * @name baidu.lang.isArray
  460. * @function
  461. * @grammar baidu.lang.isArray(source)
  462. * @param {Any} source 目标参数
  463. * @meta standard
  464. * @see baidu.lang.isString,baidu.lang.isObject,baidu.lang.isNumber,baidu.lang.isElement,baidu.lang.isBoolean,baidu.lang.isDate
  465. *
  466. * @returns {boolean} 类型判断结果
  467. */
  468. baidu.lang.isArray = function (source) {
  469. return '[object Array]' == Object.prototype.toString.call(source);
  470. };
  471. /**
  472. * 将一个变量转换成array
  473. * @name baidu.lang.toArray
  474. * @function
  475. * @grammar baidu.lang.toArray(source)
  476. * @param {mix} source 需要转换成array的变量
  477. * @version 1.3
  478. * @meta standard
  479. * @returns {array} 转换后的array
  480. */
  481. baidu.lang.toArray = function (source) {
  482. if (source === null || source === undefined)
  483. return [];
  484. if (baidu.lang.isArray(source))
  485. return source;
  486. if (typeof source.length !== 'number' || typeof source === 'string' || baidu.lang.isFunction(source)) {
  487. return [source];
  488. }
  489. if (source.item) {
  490. var l = source.length, array = new Array(l);
  491. while (l--)
  492. array[l] = source[l];
  493. return array;
  494. }
  495. return [].slice.call(source);
  496. };
  497. /**
  498. * 获得flash对象的实例
  499. * @name baidu.swf.getMovie
  500. * @function
  501. * @grammar baidu.swf.getMovie(name)
  502. * @param {string} name flash对象的名称
  503. * @see baidu.swf.create
  504. * @meta standard
  505. * @returns {HTMLElement} flash对象的实例
  506. */
  507. baidu.swf.getMovie = function (name) {
  508. var movie = document[name], ret;
  509. return baidu.browser.ie == 9 ?
  510. movie && movie.length ?
  511. (ret = baidu.array.remove(baidu.lang.toArray(movie),function(item){
  512. return item.tagName.toLowerCase() != "embed";
  513. })).length == 1 ? ret[0] : ret
  514. : movie
  515. : movie || window[name];
  516. };
  517. baidu.flash._Base = (function(){
  518. var prefix = 'bd__flash__';
  519. /**
  520. * 创建一个随机的字符串
  521. * @private
  522. * @return {String}
  523. */
  524. function _createString(){
  525. return prefix + Math.floor(Math.random() * 2147483648).toString(36);
  526. };
  527. /**
  528. * 检查flash状态
  529. * @private
  530. * @param {Object} target flash对象
  531. * @return {Boolean}
  532. */
  533. function _checkReady(target){
  534. if(typeof target !== 'undefined' && typeof target.flashInit !== 'undefined' && target.flashInit()){
  535. return true;
  536. }else{
  537. return false;
  538. }
  539. };
  540. /**
  541. * 调用之前进行压栈的函数
  542. * @private
  543. * @param {Array} callQueue 调用队列
  544. * @param {Object} target flash对象
  545. * @return {Null}
  546. */
  547. function _callFn(callQueue, target){
  548. var result = null;
  549. callQueue = callQueue.reverse();
  550. baidu.each(callQueue, function(item){
  551. result = target.call(item.fnName, item.params);
  552. item.callBack(result);
  553. });
  554. };
  555. /**
  556. * 为传入的匿名函数创建函数名
  557. * @private
  558. * @param {String|Function} fun 传入的匿名函数或者函数名
  559. * @return {String}
  560. */
  561. function _createFunName(fun){
  562. var name = '';
  563. if(baidu.lang.isFunction(fun)){
  564. name = _createString();
  565. window[name] = function(){
  566. fun.apply(window, arguments);
  567. };
  568. return name;
  569. }else if(baidu.lang.isString){
  570. return fun;
  571. }
  572. };
  573. /**
  574. * 绘制flash
  575. * @private
  576. * @param {Object} options 创建参数
  577. * @return {Object}
  578. */
  579. function _render(options){
  580. if(!options.id){
  581. options.id = _createString();
  582. }
  583. var container = options.container || '';
  584. delete(options.container);
  585. baidu.swf.create(options, container);
  586. return baidu.swf.getMovie(options.id);
  587. };
  588. return function(options, callBack){
  589. var me = this,
  590. autoRender = (typeof options.autoRender !== 'undefined' ? options.autoRender : true),
  591. createOptions = options.createOptions || {},
  592. target = null,
  593. isReady = false,
  594. callQueue = [],
  595. timeHandle = null,
  596. callBack = callBack || [];
  597. /**
  598. * 将flash文件绘制到页面上
  599. * @public
  600. * @return {Null}
  601. */
  602. me.render = function(){
  603. target = _render(createOptions);
  604. if(callBack.length > 0){
  605. baidu.each(callBack, function(funName, index){
  606. callBack[index] = _createFunName(options[funName] || new Function());
  607. });
  608. }
  609. me.call('setJSFuncName', [callBack]);
  610. };
  611. /**
  612. * 返回flash状态
  613. * @return {Boolean}
  614. */
  615. me.isReady = function(){
  616. return isReady;
  617. };
  618. /**
  619. * 调用flash接口的统一入口
  620. * @param {String} fnName 调用的函数名
  621. * @param {Array} params 传入的参数组成的数组,若不许要参数,需传入空数组
  622. * @param {Function} [callBack] 异步调用后将返回值作为参数的调用回调函数,如无返回值,可以不传入此参数
  623. * @return {Null}
  624. */
  625. me.call = function(fnName, params, callBack){
  626. if(!fnName) return null;
  627. callBack = callBack || new Function();
  628. var result = null;
  629. if(isReady){
  630. result = target.call(fnName, params);
  631. callBack(result);
  632. }else{
  633. callQueue.push({
  634. fnName: fnName,
  635. params: params,
  636. callBack: callBack
  637. });
  638. (!timeHandle) && (timeHandle = setInterval(_check, 200));
  639. }
  640. };
  641. /**
  642. * 为传入的匿名函数创建函数名
  643. * @public
  644. * @param {String|Function} fun 传入的匿名函数或者函数名
  645. * @return {String}
  646. */
  647. me.createFunName = function(fun){
  648. return _createFunName(fun);
  649. };
  650. /**
  651. * 检查flash是否ready, 并进行调用
  652. * @private
  653. * @return {Null}
  654. */
  655. function _check(){
  656. if(_checkReady(target)){
  657. clearInterval(timeHandle);
  658. timeHandle = null;
  659. _call();
  660. isReady = true;
  661. }
  662. };
  663. /**
  664. * 调用之前进行压栈的函数
  665. * @private
  666. * @return {Null}
  667. */
  668. function _call(){
  669. _callFn(callQueue, target);
  670. callQueue = [];
  671. }
  672. autoRender && me.render();
  673. };
  674. })();
  675. /**
  676. * 创建flash based imageUploader
  677. * @class
  678. * @grammar baidu.flash.imageUploader(options)
  679. * @param {Object} createOptions 创建flash时需要的参数,请参照baidu.swf.create文档
  680. * @config {Object} vars 创建imageUploader时所需要的参数
  681. * @config {Number} vars.gridWidth 每一个预览图片所占的宽度,应该为flash寛的整除
  682. * @config {Number} vars.gridHeight 每一个预览图片所占的高度,应该为flash高的整除
  683. * @config {Number} vars.picWidth 单张预览图片的宽度
  684. * @config {Number} vars.picHeight 单张预览图片的高度
  685. * @config {String} vars.uploadDataFieldName POST请求中图片数据的key,默认值'picdata'
  686. * @config {String} vars.picDescFieldName POST请求中图片描述的key,默认值'picDesc'
  687. * @config {Number} vars.maxSize 文件的最大体积,单位'MB'
  688. * @config {Number} vars.compressSize 上传前如果图片体积超过该值,会先压缩
  689. * @config {Number} vars.maxNum:32 最大上传多少个文件
  690. * @config {Number} vars.compressLength 能接受的最大边长,超过该值会等比压缩
  691. * @config {String} vars.url 上传的url地址
  692. * @config {Number} vars.mode mode == 0时,是使用滚动条,mode == 1时,拉伸flash, 默认值为0
  693. * @see baidu.swf.createHTML
  694. * @param {String} backgroundUrl 背景图片路径
  695. * @param {String} listBacgroundkUrl 布局控件背景
  696. * @param {String} buttonUrl 按钮图片不背景
  697. * @param {String|Function} selectFileCallback 选择文件的回调
  698. * @param {String|Function} exceedFileCallback文件超出限制的最大体积时的回调
  699. * @param {String|Function} deleteFileCallback 删除文件的回调
  700. * @param {String|Function} startUploadCallback 开始上传某个文件时的回调
  701. * @param {String|Function} uploadCompleteCallback 某个文件上传完成的回调
  702. * @param {String|Function} uploadErrorCallback 某个文件上传失败的回调
  703. * @param {String|Function} allCompleteCallback 全部上传完成时的回调
  704. * @param {String|Function} changeFlashHeight 改变Flash的高度,mode==1的时候才有用
  705. */
  706. baidu.flash.imageUploader = baidu.flash.imageUploader || function(options){
  707. var me = this,
  708. options = options || {},
  709. _flash = new baidu.flash._Base(options, [
  710. 'selectFileCallback',
  711. 'exceedFileCallback',
  712. 'deleteFileCallback',
  713. 'startUploadCallback',
  714. 'uploadCompleteCallback',
  715. 'uploadErrorCallback',
  716. 'allCompleteCallback',
  717. 'changeFlashHeight'
  718. ]);
  719. /**
  720. * 开始或回复上传图片
  721. * @public
  722. * @return {Null}
  723. */
  724. me.upload = function(){
  725. _flash.call('upload');
  726. };
  727. /**
  728. * 暂停上传图片
  729. * @public
  730. * @return {Null}
  731. */
  732. me.pause = function(){
  733. _flash.call('pause');
  734. };
  735. me.addCustomizedParams = function(index,obj){
  736. _flash.call('addCustomizedParams',[index,obj]);
  737. }
  738. };
  739. /**
  740. * 操作原生对象的方法
  741. * @namespace baidu.object
  742. */
  743. baidu.object = baidu.object || {};
  744. /**
  745. * 将源对象的所有属性拷贝到目标对象中
  746. * @author erik
  747. * @name baidu.object.extend
  748. * @function
  749. * @grammar baidu.object.extend(target, source)
  750. * @param {Object} target 目标对象
  751. * @param {Object} source 源对象
  752. * @see baidu.array.merge
  753. * @remark
  754. *
  755. 1.目标对象中,与源对象key相同的成员将会被覆盖。<br>
  756. 2.源对象的prototype成员不会拷贝。
  757. * @shortcut extend
  758. * @meta standard
  759. *
  760. * @returns {Object} 目标对象
  761. */
  762. baidu.extend =
  763. baidu.object.extend = function (target, source) {
  764. for (var p in source) {
  765. if (source.hasOwnProperty(p)) {
  766. target[p] = source[p];
  767. }
  768. }
  769. return target;
  770. };
  771. /**
  772. * 创建flash based fileUploader
  773. * @class
  774. * @grammar baidu.flash.fileUploader(options)
  775. * @param {Object} options
  776. * @config {Object} createOptions 创建flash时需要的参数,请参照baidu.swf.create文档
  777. * @config {String} createOptions.width
  778. * @config {String} createOptions.height
  779. * @config {Number} maxNum 最大可选文件数
  780. * @config {Function|String} selectFile
  781. * @config {Function|String} exceedMaxSize
  782. * @config {Function|String} deleteFile
  783. * @config {Function|String} uploadStart
  784. * @config {Function|String} uploadComplete
  785. * @config {Function|String} uploadError
  786. * @config {Function|String} uploadProgress
  787. */
  788. baidu.flash.fileUploader = baidu.flash.fileUploader || function(options){
  789. var me = this,
  790. options = options || {};
  791. options.createOptions = baidu.extend({
  792. wmod: 'transparent'
  793. },options.createOptions || {});
  794. var _flash = new baidu.flash._Base(options, [
  795. 'selectFile',
  796. 'exceedMaxSize',
  797. 'deleteFile',
  798. 'uploadStart',
  799. 'uploadComplete',
  800. 'uploadError',
  801. 'uploadProgress'
  802. ]);
  803. _flash.call('setMaxNum', options.maxNum ? [options.maxNum] : [1]);
  804. /**
  805. * 设置当鼠标移动到flash上时,是否变成手型
  806. * @public
  807. * @param {Boolean} isCursor
  808. * @return {Null}
  809. */
  810. me.setHandCursor = function(isCursor){
  811. _flash.call('setHandCursor', [isCursor || false]);
  812. };
  813. /**
  814. * 设置鼠标相应函数名
  815. * @param {String|Function} fun
  816. */
  817. me.setMSFunName = function(fun){
  818. _flash.call('setMSFunName',[_flash.createFunName(fun)]);
  819. };
  820. /**
  821. * 执行上传操作
  822. * @param {String} url 上传的url
  823. * @param {String} fieldName 上传的表单字段名
  824. * @param {Object} postData 键值对,上传的POST数据
  825. * @param {Number|Array|null|-1} [index]上传的文件序列
  826. * Int值上传该文件
  827. * Array一次串行上传该序列文件
  828. * -1/null上传所有文件
  829. * @return {Null}
  830. */
  831. me.upload = function(url, fieldName, postData, index){
  832. if(typeof url !== 'string' || typeof fieldName !== 'string') return null;
  833. if(typeof index === 'undefined') index = -1;
  834. _flash.call('upload', [url, fieldName, postData, index]);
  835. };
  836. /**
  837. * 取消上传操作
  838. * @public
  839. * @param {Number|-1} index
  840. */
  841. me.cancel = function(index){
  842. if(typeof index === 'undefined') index = -1;
  843. _flash.call('cancel', [index]);
  844. };
  845. /**
  846. * 删除文件
  847. * @public
  848. * @param {Number|Array} [index] 要删除的index,不传则全部删除
  849. * @param {Function} callBack
  850. * @param
  851. * */
  852. me.deleteFile = function(index, callBack){
  853. var callBackAll = function(list){
  854. callBack && callBack(list);
  855. };
  856. if(typeof index === 'undefined'){
  857. _flash.call('deleteFilesAll', [], callBackAll);
  858. return;
  859. };
  860. if(typeof index === 'Number') index = [index];
  861. index.sort(function(a,b){
  862. return b-a;
  863. });
  864. baidu.each(index, function(item){
  865. _flash.call('deleteFileBy', item, callBackAll);
  866. });
  867. };
  868. /**
  869. * 添加文件类型,支持macType
  870. * @public
  871. * @param {Object|Array[Object]} type {description:String, extention:String}
  872. * @return {Null};
  873. */
  874. me.addFileType = function(type){
  875. var type = type || [[]];
  876. if(type instanceof Array) type = [type];
  877. else type = [[type]];
  878. _flash.call('addFileTypes', type);
  879. };
  880. /**
  881. * 设置文件类型,支持macType
  882. * @public
  883. * @param {Object|Array[Object]} type {description:String, extention:String}
  884. * @return {Null};
  885. */
  886. me.setFileType = function(type){
  887. var type = type || [[]];
  888. if(type instanceof Array) type = [type];
  889. else type = [[type]];
  890. _flash.call('setFileTypes', type);
  891. };
  892. /**
  893. * 设置可选文件的数量限制
  894. * @public
  895. * @param {Number} num
  896. * @return {Null}
  897. */
  898. me.setMaxNum = function(num){
  899. _flash.call('setMaxNum', [num]);
  900. };
  901. /**
  902. * 设置可选文件大小限制,以兆M为单位
  903. * @public
  904. * @param {Number} num,0为无限制
  905. * @return {Null}
  906. */
  907. me.setMaxSize = function(num){
  908. _flash.call('setMaxSize', [num]);
  909. };
  910. /**
  911. * @public
  912. */
  913. me.getFileAll = function(callBack){
  914. _flash.call('getFileAll', [], callBack);
  915. };
  916. /**
  917. * @public
  918. * @param {Number} index
  919. * @param {Function} [callBack]
  920. */
  921. me.getFileByIndex = function(index, callBack){
  922. _flash.call('getFileByIndex', [], callBack);
  923. };
  924. /**
  925. * @public
  926. * @param {Number} index
  927. * @param {function} [callBack]
  928. */
  929. me.getStatusByIndex = function(index, callBack){
  930. _flash.call('getStatusByIndex', [], callBack);
  931. };
  932. };
  933. /**
  934. * 使用动态script标签请求服务器资源,包括由服务器端的回调和浏览器端的回调
  935. * @namespace baidu.sio
  936. */
  937. baidu.sio = baidu.sio || {};
  938. /**
  939. *
  940. * @param {HTMLElement} src script节点
  941. * @param {String} url script节点的地址
  942. * @param {String} [charset] 编码
  943. */
  944. baidu.sio._createScriptTag = function(scr, url, charset){
  945. scr.setAttribute('type', 'text/javascript');
  946. charset && scr.setAttribute('charset', charset);
  947. scr.setAttribute('src', url);
  948. document.getElementsByTagName('head')[0].appendChild(scr);
  949. };
  950. /**
  951. * 删除script的属性,再删除script标签,以解决修复内存泄漏的问题
  952. *
  953. * @param {HTMLElement} src script节点
  954. */
  955. baidu.sio._removeScriptTag = function(scr){
  956. if (scr.clearAttributes) {
  957. scr.clearAttributes();
  958. } else {
  959. for (var attr in scr) {
  960. if (scr.hasOwnProperty(attr)) {
  961. delete scr[attr];
  962. }
  963. }
  964. }
  965. if(scr && scr.parentNode){
  966. scr.parentNode.removeChild(scr);
  967. }
  968. scr = null;
  969. };
  970. /**
  971. * 通过script标签加载数据,加载完成由浏览器端触发回调
  972. * @name baidu.sio.callByBrowser
  973. * @function
  974. * @grammar baidu.sio.callByBrowser(url, opt_callback, opt_options)
  975. * @param {string} url 加载数据的url
  976. * @param {Function|string} opt_callback 数据加载结束时调用的函数或函数名
  977. * @param {Object} opt_options 其他可选项
  978. * @config {String} [charset] script的字符集
  979. * @config {Integer} [timeOut] 超时时间,超过这个时间将不再响应本请求,并触发onfailure函数
  980. * @config {Function} [onfailure] timeOut设定后才生效,到达超时时间时触发本函数
  981. * @remark
  982. * 1、与callByServer不同,callback参数只支持Function类型,不支持string。
  983. * 2、如果请求了一个不存在的页面,callback函数在IE/opera下也会被调用,因此使用者需要在onsuccess函数中判断数据是否正确加载。
  984. * @meta standard
  985. * @see baidu.sio.callByServer
  986. */
  987. baidu.sio.callByBrowser = function (url, opt_callback, opt_options) {
  988. var scr = document.createElement("SCRIPT"),
  989. scriptLoaded = 0,
  990. options = opt_options || {},
  991. charset = options['charset'],
  992. callback = opt_callback || function(){},
  993. timeOut = options['timeOut'] || 0,
  994. timer;
  995. scr.onload = scr.onreadystatechange = function () {
  996. if (scriptLoaded) {
  997. return;
  998. }
  999. var readyState = scr.readyState;
  1000. if ('undefined' == typeof readyState
  1001. || readyState == "loaded"
  1002. || readyState == "complete") {
  1003. scriptLoaded = 1;
  1004. try {
  1005. callback();
  1006. clearTimeout(timer);
  1007. } finally {
  1008. scr.onload = scr.onreadystatechange = null;
  1009. baidu.sio._removeScriptTag(scr);
  1010. }
  1011. }
  1012. };
  1013. if( timeOut ){
  1014. timer = setTimeout(function(){
  1015. scr.onload = scr.onreadystatechange = null;
  1016. baidu.sio._removeScriptTag(scr);
  1017. options.onfailure && options.onfailure();
  1018. }, timeOut);
  1019. }
  1020. baidu.sio._createScriptTag(scr, url, charset);
  1021. };
  1022. /**
  1023. * 通过script标签加载数据,加载完成由服务器端触发回调
  1024. * @name baidu.sio.callByServer
  1025. * @function
  1026. * @grammar baidu.sio.callByServer(url, callback[, opt_options])
  1027. * @param {string} url 加载数据的url.
  1028. * @param {Function|string} callback 服务器端调用的函数或函数名。如果没有指定本参数,将在URL中寻找options['queryField']做为callback的方法名.
  1029. * @param {Object} opt_options 加载数据时的选项.
  1030. * @config {string} [charset] script的字符集
  1031. * @config {string} [queryField] 服务器端callback请求字段名,默认为callback
  1032. * @config {Integer} [timeOut] 超时时间(单位:ms),超过这个时间将不再响应本请求,并触发onfailure函数
  1033. * @config {Function} [onfailure] timeOut设定后才生效,到达超时时间时触发本函数
  1034. * @remark
  1035. * 如果url中已经包含key为“options['queryField']”的query项,将会被替换成callback中参数传递或自动生成的函数名。
  1036. * @meta standard
  1037. * @see baidu.sio.callByBrowser
  1038. */
  1039. baidu.sio.callByServer = /**@function*/function(url, callback, opt_options) {
  1040. var scr = document.createElement('SCRIPT'),
  1041. prefix = 'bd__cbs__',
  1042. callbackName,
  1043. callbackImpl,
  1044. options = opt_options || {},
  1045. charset = options['charset'],
  1046. queryField = options['queryField'] || 'callback',
  1047. timeOut = options['timeOut'] || 0,
  1048. timer,
  1049. reg = new RegExp('(\\?|&)' + queryField + '=([^&]*)'),
  1050. matches;
  1051. if (baidu.lang.isFunction(callback)) {
  1052. callbackName = prefix + Math.floor(Math.random() * 2147483648).toString(36);
  1053. window[callbackName] = getCallBack(0);
  1054. } else if(baidu.lang.isString(callback)){
  1055. callbackName = callback;
  1056. } else {
  1057. if (matches = reg.exec(url)) {
  1058. callbackName = matches[2];
  1059. }
  1060. }
  1061. if( timeOut ){
  1062. timer = setTimeout(getCallBack(1), timeOut);
  1063. }
  1064. url = url.replace(reg, '\x241' + queryField + '=' + callbackName);
  1065. if (url.search(reg) < 0) {
  1066. url += (url.indexOf('?') < 0 ? '?' : '&') + queryField + '=' + callbackName;
  1067. }
  1068. baidu.sio._createScriptTag(scr, url, charset);
  1069. /*
  1070. * 返回一个函数,用于立即(挂在window上)或者超时(挂在setTimeout中)时执行
  1071. */
  1072. function getCallBack(onTimeOut){
  1073. /*global callbackName, callback, scr, options;*/
  1074. return function(){
  1075. try {
  1076. if( onTimeOut ){
  1077. options.onfailure && options.onfailure();
  1078. }else{
  1079. callback.apply(window, arguments);
  1080. clearTimeout(timer);
  1081. }
  1082. window[callbackName] = null;
  1083. delete window[callbackName];
  1084. } catch (exception) {
  1085. } finally {
  1086. baidu.sio._removeScriptTag(scr);
  1087. }
  1088. }
  1089. }
  1090. };
  1091. /**
  1092. * 通过请求一个图片的方式令服务器存储一条日志
  1093. * @function
  1094. * @grammar baidu.sio.log(url)
  1095. * @param {string} url 要发送的地址.
  1096. * @author: int08h,leeight
  1097. */
  1098. baidu.sio.log = function(url) {
  1099. var img = new Image(),
  1100. key = 'tangram_sio_log_' + Math.floor(Math.random() *
  1101. 2147483648).toString(36);
  1102. window[key] = img;
  1103. img.onload = img.onerror = img.onabort = function() {
  1104. img.onload = img.onerror = img.onabort = null;
  1105. window[key] = null;
  1106. img = null;
  1107. };
  1108. img.src = url;
  1109. };