PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/build/seed-debug.js

https://github.com/atgwwx/kissy
JavaScript | 3493 lines | 2381 code | 249 blank | 863 comment | 479 complexity | 5f15647ec8de1016d71ea5d53b62e4fa MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. Copyright 2014, KISSY v5.0.0
  3. MIT Licensed
  4. build time: Jul 1 22:55
  5. */
  6. /**
  7. * @ignore
  8. * A seed where KISSY grows up from, KISS Yeah !
  9. * @author https://github.com/kissyteam/kissy/contributors
  10. */
  11. /**
  12. * The KISSY global namespace object. you can use
  13. *
  14. *
  15. * KISSY.each/mix
  16. *
  17. * to do basic operation. or
  18. *
  19. *
  20. * KISSY.use('overlay,node', function(S, Overlay, Node){
  21. * //
  22. * });
  23. *
  24. * to do complex task with modules.
  25. * @singleton
  26. * @class KISSY
  27. */
  28. /* exported KISSY */
  29. /*jshint -W079 */
  30. var KISSY = (function (undefined) {
  31. // --no-module-wrap--
  32. var self = this,
  33. S;
  34. S = {
  35. /**
  36. * The build time of the library.
  37. * NOTICE: '20140701225543' will replace with current timestamp when compressing.
  38. * @private
  39. * @type {String}
  40. */
  41. __BUILD_TIME: '20140701225543',
  42. /**
  43. * KISSY Environment.
  44. * @private
  45. * @type {Object}
  46. */
  47. Env: {
  48. host: self,
  49. mods: {}
  50. },
  51. /**
  52. * KISSY Config.
  53. * If load kissy.js, Config.debug defaults to true.
  54. * Else If load kissy-min.js, Config.debug defaults to false.
  55. * @private
  56. * @property {Object} Config
  57. * @property {Boolean} Config.debug
  58. * @member KISSY
  59. */
  60. Config: {
  61. debug: '@DEBUG@',
  62. packages: {},
  63. fns: {}
  64. },
  65. /**
  66. * The version of the library.
  67. * NOTICE: '5.0.0' will replace with current version when compressing.
  68. * @type {String}
  69. */
  70. version: '5.0.0',
  71. /**
  72. * set KISSY configuration
  73. * @param {Object|String} configName Config object or config key.
  74. * @param {String} configName.base KISSY 's base path. Default: get from loader(-min).js or seed(-min).js
  75. * @param {String} configName.tag KISSY 's timestamp for native module. Default: KISSY 's build time.
  76. * @param {Boolean} configName.debug whether to enable debug mod.
  77. * @param {Boolean} configName.combine whether to enable combo.
  78. * @param {Object} configName.logger logger config
  79. * @param {Object[]} configName.logger.excludes exclude configs
  80. * @param {Object} configName.logger.excludes.0 a single exclude config
  81. * @param {RegExp} configName.logger.excludes.0.logger matched logger will be excluded from logging
  82. * @param {String} configName.logger.excludes.0.minLevel minimum logger level (enum of debug info warn error)
  83. * @param {String} configName.logger.excludes.0.maxLevel maximum logger level (enum of debug info warn error)
  84. * @param {Object[]} configName.logger.includes include configs
  85. * @param {Object} configName.logger.includes.0 a single include config
  86. * @param {RegExp} configName.logger.includes.0.logger matched logger will be included from logging
  87. * @param {String} configName.logger.excludes.0.minLevel minimum logger level (enum of debug info warn error)
  88. * @param {String} configName.logger.excludes.0.maxLevel maximum logger level (enum of debug info warn error)
  89. * @param {Object} configName.packages Packages definition with package name as the key.
  90. * @param {String} configName.packages.base Package base path.
  91. * @param {String} configName.packages.tag Timestamp for this package's module file.
  92. * @param {String} configName.packages.debug Whether force debug mode for current package.
  93. * @param {String} configName.packages.combine Whether allow combine for current package modules.
  94. * can only be used in production mode.
  95. * @param [configValue] config value.
  96. *
  97. * for example:
  98. * @example
  99. * KISSY.config({
  100. * combine: true,
  101. * base: '',
  102. * packages: {
  103. * 'gallery': {
  104. * base: 'http://a.tbcdn.cn/s/kissy/gallery/'
  105. * }
  106. * },
  107. * modules: {
  108. * 'gallery/x/y': {
  109. * requires: ['gallery/x/z']
  110. * }
  111. * }
  112. * });
  113. */
  114. config: function (configName, configValue) {
  115. var cfg,
  116. r,
  117. self = this,
  118. fn,
  119. Config = S.Config,
  120. configFns = Config.fns;
  121. if (typeof configName === 'string') {
  122. cfg = configFns[configName];
  123. if (configValue === undefined) {
  124. if (cfg) {
  125. r = cfg.call(self);
  126. } else {
  127. r = Config[configName];
  128. }
  129. } else {
  130. if (cfg) {
  131. r = cfg.call(self, configValue);
  132. } else {
  133. Config[configName] = configValue;
  134. }
  135. }
  136. } else {
  137. for (var p in configName) {
  138. configValue = configName[p];
  139. fn = configFns[p];
  140. if (fn) {
  141. fn.call(self, configValue);
  142. } else {
  143. Config[p] = configValue;
  144. }
  145. }
  146. }
  147. return r;
  148. }
  149. };
  150. var Loader = S.Loader = {};
  151. if (typeof location !== 'undefined') {
  152. if (location.search.indexOf('ks-debug') !== -1) {
  153. S.Config.debug = true;
  154. }
  155. }
  156. /**
  157. * Loader Status Enum
  158. * @enum {Number} KISSY.Loader.Status
  159. */
  160. Loader.Status = {
  161. /** error */
  162. ERROR: -1,
  163. /** init */
  164. INIT: 0,
  165. /** loading */
  166. LOADING: 1,
  167. /** loaded */
  168. LOADED: 2,
  169. /** attaching */
  170. ATTACHING: 3,
  171. /** attached */
  172. ATTACHED: 4
  173. };
  174. return S;
  175. })();/**
  176. * logger utils
  177. * @author yiminghe@gmail.com
  178. */
  179. (function (S) {
  180. function getLogger(logger) {
  181. var obj = {};
  182. for (var cat in loggerLevel) {
  183. /*jshint loopfunc: true*/
  184. (function (obj, cat) {
  185. obj[cat] = function (msg) {
  186. return LoggerManager.log(msg, cat, logger);
  187. };
  188. })(obj, cat);
  189. }
  190. return obj;
  191. }
  192. var config = {};
  193. if ('@DEBUG@') {
  194. config = {
  195. excludes: [
  196. {
  197. logger: /^s\/.*/,
  198. maxLevel: 'info',
  199. minLevel: 'debug'
  200. }
  201. ]
  202. };
  203. }
  204. var loggerLevel = {
  205. debug: 10,
  206. info: 20,
  207. warn: 30,
  208. error: 40
  209. };
  210. var LoggerManager = {
  211. config: function (cfg) {
  212. config = cfg || config;
  213. return config;
  214. },
  215. /**
  216. * Prints debug info.
  217. * @param msg {String} the message to log.
  218. * @param {String} [cat] the log category for the message. Default
  219. * categories are 'info', 'warn', 'error', 'time' etc.
  220. * @param {String} [logger] the logger of the the message (opt)
  221. */
  222. log: function (msg, cat, logger) {
  223. if ('@DEBUG@') {
  224. var matched = 1;
  225. if (logger) {
  226. var list, i, l, level, minLevel, maxLevel, reg;
  227. cat = cat || 'debug';
  228. level = loggerLevel[cat] || loggerLevel.debug;
  229. if ((list = config.includes)) {
  230. matched = 0;
  231. for (i = 0; i < list.length; i++) {
  232. l = list[i];
  233. reg = l.logger;
  234. maxLevel = loggerLevel[l.maxLevel] || loggerLevel.error;
  235. minLevel = loggerLevel[l.minLevel] || loggerLevel.debug;
  236. if (minLevel <= level && maxLevel >= level && logger.match(reg)) {
  237. matched = 1;
  238. break;
  239. }
  240. }
  241. } else if ((list = config.excludes)) {
  242. matched = 1;
  243. for (i = 0; i < list.length; i++) {
  244. l = list[i];
  245. reg = l.logger;
  246. maxLevel = loggerLevel[l.maxLevel] || loggerLevel.error;
  247. minLevel = loggerLevel[l.minLevel] || loggerLevel.debug;
  248. if (minLevel <= level && maxLevel >= level && logger.match(reg)) {
  249. matched = 0;
  250. break;
  251. }
  252. }
  253. }
  254. if (matched) {
  255. msg = logger + ': ' + msg;
  256. }
  257. }
  258. /*global console*/
  259. if (matched) {
  260. if (typeof console !== 'undefined' && console.log) {
  261. console[cat && console[cat] ? cat : 'log'](msg);
  262. }
  263. return msg;
  264. }
  265. }
  266. return undefined;
  267. },
  268. /**
  269. * get log instance for specified logger
  270. * @param {String} logger logger name
  271. * @returns {KISSY.LoggerManager} log instance
  272. */
  273. getLogger: function (logger) {
  274. return getLogger(logger);
  275. },
  276. /**
  277. * Throws error message.
  278. */
  279. error: function (msg) {
  280. if ('@DEBUG@') {
  281. // with stack info!
  282. throw msg instanceof Error ? msg : new Error(msg);
  283. }
  284. }
  285. };
  286. /**
  287. * Log class for specified logger
  288. * @class KISSY.LoggerManager
  289. * @private
  290. */
  291. /**
  292. * print debug log
  293. * @method debug
  294. * @member KISSY.LoggerManager
  295. * @param {String} str log str
  296. */
  297. /**
  298. * print info log
  299. * @method info
  300. * @member KISSY.LoggerManager
  301. * @param {String} str log str
  302. */
  303. /**
  304. * print warn log
  305. * @method log
  306. * @member KISSY.LoggerManager
  307. * @param {String} str log str
  308. */
  309. /**
  310. * print error log
  311. * @method error
  312. * @member KISSY.LoggerManager
  313. * @param {String} str log str
  314. */
  315. S.LoggerMangaer = LoggerManager;
  316. S.getLogger = LoggerManager.getLogger;
  317. S.log = LoggerManager.log;
  318. S.error = LoggerManager.error;
  319. S.Config.fns.logger = LoggerManager.config;
  320. })(KISSY);/**
  321. * @ignore
  322. * Utils for kissy loader
  323. * @author yiminghe@gmail.com
  324. */
  325. (function (S) {
  326. // --no-module-wrap--
  327. var Loader = S.Loader,
  328. Env = S.Env,
  329. mods = Env.mods,
  330. map = Array.prototype.map,
  331. host = Env.host,
  332. /**
  333. * @class KISSY.Loader.Utils
  334. * Utils for KISSY Loader
  335. * @singleton
  336. * @private
  337. */
  338. Utils = Loader.Utils = {},
  339. doc = host.document;
  340. function numberify(s) {
  341. var c = 0;
  342. // convert '1.2.3.4' to 1.234
  343. return parseFloat(s.replace(/\./g, function () {
  344. return (c++ === 0) ? '.' : '';
  345. }));
  346. }
  347. function splitSlash(str) {
  348. var parts = str.split(/\//);
  349. if (str.charAt(0) === '/' && parts[0]) {
  350. parts.unshift('');
  351. }
  352. if (str.charAt(str.length - 1) === '/' && str.length > 1 && parts[parts.length - 1]) {
  353. parts.push('');
  354. }
  355. return parts;
  356. }
  357. var m, v,
  358. ua = (host.navigator || {}).userAgent || '';
  359. // https://github.com/kissyteam/kissy/issues/545
  360. if (((m = ua.match(/AppleWebKit\/([\d.]*)/)) || (m = ua.match(/Safari\/([\d.]*)/))) && m[1]) {
  361. Utils.webkit = numberify(m[1]);
  362. }
  363. if ((m = ua.match(/Trident\/([\d.]*)/))) {
  364. Utils.trident = numberify(m[1]);
  365. }
  366. if ((m = ua.match(/Gecko/))) {
  367. Utils.gecko = 0.1; // Gecko detected, look for revision
  368. if ((m = ua.match(/rv:([\d.]*)/)) && m[1]) {
  369. Utils.gecko = numberify(m[1]);
  370. }
  371. }
  372. if ((m = ua.match(/MSIE ([^;]*)|Trident.*; rv(?:\s|:)?([0-9.]+)/)) &&
  373. (v = (m[1] || m[2]))) {
  374. Utils.ie = numberify(v);
  375. Utils.ieMode = doc.documentMode || Utils.ie;
  376. Utils.trident = Utils.trident || 1;
  377. }
  378. var urlReg = /http(s)?:\/\/([^/]+)(?::(\d+))?/,
  379. commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
  380. requireRegExp = /[^.'"]\s*require\s*\((['"])([^)]+)\1\)/g;
  381. function normalizeName(name) {
  382. // 'x/' 'x/y/z/'
  383. if (name.charAt(name.length - 1) === '/') {
  384. name += 'index';
  385. }
  386. // x.js === x
  387. if (Utils.endsWith(name, '.js')) {
  388. name = name.slice(0, -3);
  389. }
  390. return name;
  391. }
  392. function each(obj, fn) {
  393. var i = 0,
  394. myKeys, l;
  395. if (isArray(obj)) {
  396. l = obj.length;
  397. for (; i < l; i++) {
  398. if (fn(obj[i], i, obj) === false) {
  399. break;
  400. }
  401. }
  402. } else {
  403. myKeys = keys(obj);
  404. l = myKeys.length;
  405. for (; i < l; i++) {
  406. if (fn(obj[myKeys[i]], myKeys[i], obj) === false) {
  407. break;
  408. }
  409. }
  410. }
  411. }
  412. function keys(obj) {
  413. var ret = [];
  414. for (var key in obj) {
  415. ret.push(key);
  416. }
  417. return ret;
  418. }
  419. var isArray = Array.isArray || function (obj) {
  420. return Object.prototype.toString.call(obj) === '[object Array]';
  421. };
  422. function mix(to, from) {
  423. for (var i in from) {
  424. to[i] = from[i];
  425. }
  426. return to;
  427. }
  428. mix(Utils, {
  429. mix: mix,
  430. noop: function () {
  431. },
  432. map: map ?
  433. function (arr, fn, context) {
  434. return map.call(arr, fn, context || this);
  435. } :
  436. function (arr, fn, context) {
  437. var len = arr.length,
  438. res = new Array(len);
  439. for (var i = 0; i < len; i++) {
  440. var el = typeof arr === 'string' ? arr.charAt(i) : arr[i];
  441. if (el ||
  442. //ie<9 in invalid when typeof arr == string
  443. i in arr) {
  444. res[i] = fn.call(context || this, el, i, arr);
  445. }
  446. }
  447. return res;
  448. },
  449. startsWith: function (str, prefix) {
  450. return str.lastIndexOf(prefix, 0) === 0;
  451. },
  452. isEmptyObject: function (o) {
  453. for (var p in o) {
  454. if (p !== undefined) {
  455. return false;
  456. }
  457. }
  458. return true;
  459. },
  460. endsWith: function (str, suffix) {
  461. var ind = str.length - suffix.length;
  462. return ind >= 0 && str.indexOf(suffix, ind) === ind;
  463. },
  464. now: Date.now || function () {
  465. return +new Date();
  466. },
  467. each: each,
  468. keys: keys,
  469. isArray: isArray,
  470. indexOf: function (item, arr) {
  471. for (var i = 0, l = arr.length; i < l; i++) {
  472. if (arr[i] === item) {
  473. return i;
  474. }
  475. }
  476. return -1;
  477. },
  478. normalizeSlash: function (str) {
  479. return str.replace(/\\/g, '/');
  480. },
  481. normalizePath: function (parentPath, subPath) {
  482. var firstChar = subPath.charAt(0);
  483. if (firstChar !== '.') {
  484. return subPath;
  485. }
  486. var parts = splitSlash(parentPath);
  487. var subParts = splitSlash(subPath);
  488. parts.pop();
  489. for (var i = 0, l = subParts.length; i < l; i++) {
  490. var subPart = subParts[i];
  491. if (subPart === '.') {
  492. } else if (subPart === '..') {
  493. parts.pop();
  494. } else {
  495. parts.push(subPart);
  496. }
  497. }
  498. return parts.join('/').replace(/\/+/, '/');
  499. },
  500. isSameOriginAs: function (url1, url2) {
  501. var urlParts1 = url1.match(urlReg);
  502. var urlParts2 = url2.match(urlReg);
  503. return urlParts1[0] === urlParts2[0];
  504. },
  505. /**
  506. * get document head
  507. * @return {HTMLElement}
  508. */
  509. docHead: function () {
  510. return doc.getElementsByTagName('head')[0] || doc.documentElement;
  511. },
  512. /**
  513. * Returns hash code of a string djb2 algorithm
  514. * @param {String} str
  515. * @returns {String} hash code
  516. */
  517. getHash: function (str) {
  518. var hash = 5381,
  519. i;
  520. for (i = str.length; --i > -1;) {
  521. hash = ((hash << 5) + hash) + str.charCodeAt(i);
  522. /* hash * 33 + char */
  523. }
  524. return hash + '';
  525. },
  526. // ---------------------------------- for modules
  527. getRequiresFromFn: function (fn) {
  528. var requires = [];
  529. // Remove comments from the callback string,
  530. // look for require calls, and pull them into the dependencies,
  531. // but only if there are function args.
  532. fn.toString()
  533. .replace(commentRegExp, '')
  534. .replace(requireRegExp, function (match, _, dep) {
  535. requires.push(dep);
  536. });
  537. return requires;
  538. },
  539. // get a module from cache or create a module instance
  540. createModule: function (name, cfg) {
  541. var module = mods[name];
  542. if (!module) {
  543. name = normalizeName(name);
  544. module = mods[name];
  545. }
  546. if (module) {
  547. mix(module, cfg);
  548. // module definition changes requires
  549. if (cfg && cfg.requires) {
  550. module.setRequiresModules(cfg.requires);
  551. }
  552. return module;
  553. }
  554. // 防止 cfg 里有 tag,构建 fullpath 需要
  555. mods[name] = module = new Loader.Module(mix({
  556. name: name
  557. }, cfg));
  558. return module;
  559. },
  560. createModules: function (names) {
  561. return Utils.map(names, function (name) {
  562. return Utils.createModule(name);
  563. });
  564. },
  565. attachModules: function (mods) {
  566. var l = mods.length, i;
  567. for (i = 0; i < l; i++) {
  568. mods[i].attachRecursive();
  569. }
  570. },
  571. getModulesExports: function (mods) {
  572. var l = mods.length, i,
  573. ret = [];
  574. for (i = 0; i < l; i++) {
  575. ret.push(mods[i].getExports());
  576. }
  577. return ret;
  578. },
  579. addModule: function (name, factory, config) {
  580. var module = mods[name];
  581. if (module && module.factory !== undefined) {
  582. S.log(name + ' is defined more than once', 'warn');
  583. return;
  584. }
  585. Utils.createModule(name, mix({
  586. name: name,
  587. status: Loader.Status.LOADED,
  588. factory: factory
  589. }, config));
  590. }
  591. });
  592. })
  593. (KISSY);/**
  594. * @ignore
  595. * setup data structure for kissy loader
  596. * @author yiminghe@gmail.com
  597. */
  598. (function (S) {
  599. // --no-module-wrap--
  600. var Loader = S.Loader,
  601. Config = S.Config,
  602. Status = Loader.Status,
  603. ATTACHED = Status.ATTACHED,
  604. ATTACHING = Status.ATTACHING,
  605. Utils = Loader.Utils,
  606. startsWith = Utils.startsWith,
  607. createModule = Utils.createModule,
  608. mix = Utils.mix;
  609. function makeArray(arr) {
  610. var ret = [];
  611. for (var i = 0; i < arr.length; i++) {
  612. ret[i] = arr[i];
  613. }
  614. return ret;
  615. }
  616. function wrapUse(fn) {
  617. if (typeof fn === 'function') {
  618. return function () {
  619. fn.apply(this, makeArray(arguments).slice(1));
  620. };
  621. } else if (fn && fn.success) {
  622. var original = fn.success;
  623. fn.success = function () {
  624. original.apply(this, makeArray(arguments).slice(1));
  625. };
  626. return fn;
  627. }
  628. }
  629. function checkGlobalIfNotExist(self, property) {
  630. return property in self ? self[property] : Config[property];
  631. }
  632. /**
  633. * @class KISSY.Loader.Package
  634. * @private
  635. * This class should not be instantiated manually.
  636. */
  637. function Package(cfg) {
  638. mix(this, cfg);
  639. }
  640. Package.prototype = {
  641. constructor: Package,
  642. reset: function (cfg) {
  643. mix(this, cfg);
  644. },
  645. getFilter: function () {
  646. return checkGlobalIfNotExist(this, 'filter');
  647. },
  648. /**
  649. * Tag for package.
  650. * tag can not contain ".", eg: Math.random() !
  651. * @return {String}
  652. */
  653. getTag: function () {
  654. return checkGlobalIfNotExist(this, 'tag');
  655. },
  656. /**
  657. * get package url
  658. */
  659. getBase: function () {
  660. return this.base;
  661. },
  662. /**
  663. * Get charset for package.
  664. * @return {String}
  665. */
  666. getCharset: function () {
  667. return checkGlobalIfNotExist(this, 'charset');
  668. },
  669. /**
  670. * Whether modules are combined for this package.
  671. * @return {Boolean}
  672. */
  673. isCombine: function () {
  674. return checkGlobalIfNotExist(this, 'combine');
  675. },
  676. /**
  677. * Get package group (for combo).
  678. * @returns {String}
  679. */
  680. getGroup: function () {
  681. return checkGlobalIfNotExist(this, 'group');
  682. }
  683. };
  684. Loader.Package = Package;
  685. /**
  686. * @class KISSY.Loader.Module
  687. * @private
  688. * This class should not be instantiated manually.
  689. */
  690. function Module(cfg) {
  691. var self = this;
  692. /**
  693. * exports of this module
  694. */
  695. self.exports = undefined;
  696. /**
  697. * status of current modules
  698. */
  699. self.status = Status.INIT;
  700. /**
  701. * name of this module
  702. */
  703. self.name = undefined;
  704. /**
  705. * factory of this module
  706. */
  707. self.factory = undefined;
  708. // lazy initialize and commonjs module format
  709. self.cjs = 1;
  710. mix(self, cfg);
  711. self.waits = {};
  712. var require = self._require = function (moduleName) {
  713. if (typeof moduleName === 'string') {
  714. var requiresModule = createModule(self.resolve(moduleName));
  715. Utils.attachModules(requiresModule.getNormalizedModules());
  716. return requiresModule.getExports();
  717. } else {
  718. require.async.apply(require, arguments);
  719. }
  720. };
  721. require.async = function (mods) {
  722. for (var i = 0; i < mods.length; i++) {
  723. mods[i] = self.resolve(mods[i]);
  724. }
  725. var args = makeArray(arguments);
  726. args[0] = mods;
  727. args[1] = wrapUse(args[1]);
  728. S.use.apply(S, args);
  729. };
  730. require.resolve = function (relativeName) {
  731. return self.resolve(relativeName);
  732. };
  733. require.toUrl = function (path) {
  734. var url = self.getUrl();
  735. var pathIndex = url.indexOf('//');
  736. if (pathIndex === -1) {
  737. pathIndex = 0;
  738. } else {
  739. pathIndex = url.indexOf('/', pathIndex + 2);
  740. if (pathIndex === -1) {
  741. pathIndex = 0;
  742. }
  743. }
  744. var rest = url.substring(pathIndex);
  745. path = Utils.normalizePath(rest, path);
  746. return url.substring(0, pathIndex) + path;
  747. };
  748. require.load = S.getScript;
  749. // relative name resolve cache
  750. // self.resolveCache = {};
  751. }
  752. Module.prototype = {
  753. kissy: 1,
  754. constructor: Module,
  755. require: function (moduleName) {
  756. return S.require(this.resolve(moduleName));
  757. },
  758. resolve: function (relativeName) {
  759. return Utils.normalizePath(this.name, relativeName);
  760. // var resolveCache = this.resolveCache;
  761. // if (resolveCache[relativeName]) {
  762. // return resolveCache[relativeName];
  763. // }
  764. // resolveCache[relativeName] = Utils.normalizePath(this.name, relativeName);
  765. // return resolveCache[relativeName];
  766. },
  767. add: function (loader) {
  768. this.waits[loader.id] = loader;
  769. },
  770. remove: function (loader) {
  771. delete this.waits[loader.id];
  772. },
  773. contains: function (loader) {
  774. return this.waits[loader.id];
  775. },
  776. flush: function () {
  777. Utils.each(this.waits, function (loader) {
  778. loader.flush();
  779. });
  780. this.waits = {};
  781. },
  782. /**
  783. * Get the type if current Module
  784. * @return {String} css or js
  785. */
  786. getType: function () {
  787. var self = this,
  788. v = self.type;
  789. if (!v) {
  790. if (Utils.endsWith(self.name, '.css')) {
  791. v = 'css';
  792. } else {
  793. v = 'js';
  794. }
  795. self.type = v;
  796. }
  797. return v;
  798. },
  799. getExports: function () {
  800. return this.getNormalizedModules()[0].exports;
  801. },
  802. getAlias: function () {
  803. var self = this,
  804. name = self.name;
  805. if (self.normalizedAlias) {
  806. return self.normalizedAlias;
  807. }
  808. var alias = getShallowAlias(self);
  809. var ret = [];
  810. if (alias[0] === name) {
  811. ret = alias;
  812. } else {
  813. for (var i = 0, l = alias.length; i < l; i++) {
  814. var aliasItem = alias[i];
  815. if (aliasItem && aliasItem !== name) {
  816. var mod = createModule(aliasItem);
  817. var normalAlias = mod.getAlias();
  818. if (normalAlias) {
  819. ret.push.apply(ret, normalAlias);
  820. } else {
  821. ret.push(aliasItem);
  822. }
  823. }
  824. }
  825. }
  826. self.normalizedAlias = ret;
  827. return ret;
  828. },
  829. getNormalizedModules: function () {
  830. var self = this;
  831. if (self.normalizedModules) {
  832. return self.normalizedModules;
  833. }
  834. self.normalizedModules = Utils.map(self.getAlias(), function (alias) {
  835. return createModule(alias);
  836. });
  837. return self.normalizedModules;
  838. },
  839. /**
  840. * Get the path url of current module if load dynamically
  841. * @return {String}
  842. */
  843. getUrl: function () {
  844. var self = this;
  845. if (!self.url) {
  846. self.url = Utils.normalizeSlash(S.Config.resolveModFn(self));
  847. }
  848. return self.url;
  849. },
  850. /**
  851. * Get the package which current module belongs to.
  852. * @return {KISSY.Loader.Package}
  853. */
  854. getPackage: function () {
  855. var self = this;
  856. if (!('packageInfo' in self)) {
  857. var name = self.name;
  858. // absolute path
  859. if (startsWith(name, '/') ||
  860. startsWith(name, 'http://') ||
  861. startsWith(name, 'https://') ||
  862. startsWith(name, 'file://')) {
  863. self.packageInfo = null;
  864. return;
  865. }
  866. var packages = Config.packages,
  867. modNameSlash = self.name + '/',
  868. pName = '',
  869. p;
  870. for (p in packages) {
  871. if (startsWith(modNameSlash, p + '/') && p.length > pName.length) {
  872. pName = p;
  873. }
  874. }
  875. self.packageInfo = packages[pName] || packages.core;
  876. }
  877. return self.packageInfo;
  878. },
  879. /**
  880. * Get the tag of current module.
  881. * tag can not contain ".", eg: Math.random() !
  882. * @return {String}
  883. */
  884. getTag: function () {
  885. var self = this;
  886. return self.tag || self.getPackage() && self.getPackage().getTag();
  887. },
  888. /**
  889. * Get the charset of current module
  890. * @return {String}
  891. */
  892. getCharset: function () {
  893. var self = this;
  894. return self.charset || self.getPackage() && self.getPackage().getCharset();
  895. },
  896. setRequiresModules: function (requires) {
  897. var self = this;
  898. var requiredModules = self.requiredModules = Utils.map(normalizeRequires(requires, self), function (m) {
  899. return createModule(m);
  900. });
  901. var normalizedRequiredModules = [];
  902. Utils.each(requiredModules, function (mod) {
  903. normalizedRequiredModules.push.apply(normalizedRequiredModules, mod.getNormalizedModules());
  904. });
  905. self.normalizedRequiredModules = normalizedRequiredModules;
  906. },
  907. getNormalizedRequiredModules: function () {
  908. var self = this;
  909. if (self.normalizedRequiredModules) {
  910. return self.normalizedRequiredModules;
  911. }
  912. self.setRequiresModules(self.requires);
  913. return self.normalizedRequiredModules;
  914. },
  915. getRequiredModules: function () {
  916. var self = this;
  917. if (self.requiredModules) {
  918. return self.requiredModules;
  919. }
  920. self.setRequiresModules(self.requires);
  921. return self.requiredModules;
  922. },
  923. attachSelf: function () {
  924. var self = this,
  925. status = self.status,
  926. factory = self.factory,
  927. exports;
  928. if (status === Status.ATTACHED || status < Status.LOADED) {
  929. return true;
  930. }
  931. if (typeof factory === 'function') {
  932. self.exports = {};
  933. // compatible and efficiency
  934. // KISSY.add(function(S,undefined){})
  935. // 需要解开 index,相对路径
  936. // 但是需要保留 alias,防止值不对应
  937. //noinspection JSUnresolvedFunction
  938. exports = factory.apply(self,
  939. // KISSY.add(function(S){module.require}) lazy initialize
  940. (
  941. self.cjs ?
  942. [S, self._require, self.exports, self] :
  943. [S].concat(Utils.map(self.getRequiredModules(), function (m) {
  944. return m.getExports();
  945. }))
  946. )
  947. );
  948. if (exports !== undefined) {
  949. // noinspection JSUndefinedPropertyAssignment
  950. self.exports = exports;
  951. }
  952. } else {
  953. //noinspection JSUndefinedPropertyAssignment
  954. self.exports = factory;
  955. }
  956. self.status = ATTACHED;
  957. if (self.afterAttach) {
  958. self.afterAttach(self.exports);
  959. }
  960. },
  961. attachRecursive: function () {
  962. var self = this,
  963. status;
  964. status = self.status;
  965. // attached or circular dependency
  966. if (status >= ATTACHING || status < Status.LOADED) {
  967. return self;
  968. }
  969. self.status = ATTACHING;
  970. if (self.cjs) {
  971. // commonjs format will call require in module code again
  972. self.attachSelf();
  973. } else {
  974. Utils.each(self.getNormalizedRequiredModules(), function (m) {
  975. m.attachRecursive();
  976. });
  977. self.attachSelf();
  978. }
  979. return self;
  980. },
  981. undef: function () {
  982. this.status = Status.INIT;
  983. delete this.factory;
  984. delete this.exports;
  985. }
  986. };
  987. function pluginAlias(name) {
  988. var index = name.indexOf('!');
  989. if (index !== -1) {
  990. var pluginName = name.substring(0, index);
  991. name = name.substring(index + 1);
  992. var Plugin = createModule(pluginName).attachRecursive().exports || {};
  993. if (Plugin.alias) {
  994. name = Plugin.alias(S, name, pluginName);
  995. }
  996. }
  997. return name;
  998. }
  999. function normalizeRequires(requires, self) {
  1000. requires = requires || [];
  1001. var l = requires.length;
  1002. for (var i = 0; i < l; i++) {
  1003. requires[i] = self.resolve(requires[i]);
  1004. }
  1005. return requires;
  1006. }
  1007. function getShallowAlias(mod) {
  1008. var name = mod.name,
  1009. packageInfo,
  1010. alias = mod.alias;
  1011. if (typeof alias === 'string') {
  1012. mod.alias = alias = [alias];
  1013. }
  1014. if (alias) {
  1015. return alias;
  1016. }
  1017. packageInfo = mod.getPackage();
  1018. if (packageInfo && packageInfo.alias) {
  1019. alias = packageInfo.alias(name);
  1020. }
  1021. alias = mod.alias = alias || [
  1022. pluginAlias(name)
  1023. ];
  1024. return alias;
  1025. }
  1026. Loader.Module = Module;
  1027. })(KISSY);/**
  1028. * @ignore
  1029. * script/css load across browser
  1030. * @author yiminghe@gmail.com
  1031. */
  1032. (function (S) {
  1033. // --no-module-wrap--
  1034. var logger = S.getLogger('s/loader/getScript');
  1035. var CSS_POLL_INTERVAL = 30,
  1036. Utils = S.Loader.Utils,
  1037. // central poll for link node
  1038. timer = 0,
  1039. // node.id:{callback:callback,node:node}
  1040. monitors = {};
  1041. function startCssTimer() {
  1042. if (!timer) {
  1043. logger.debug('start css poll timer');
  1044. cssPoll();
  1045. }
  1046. }
  1047. function isCssLoaded(node, url) {
  1048. var loaded = 0;
  1049. if (Utils.webkit) {
  1050. // http://www.w3.org/TR/Dom-Level-2-Style/stylesheets.html
  1051. if (node.sheet) {
  1052. logger.debug('webkit css poll loaded: ' + url);
  1053. loaded = 1;
  1054. }
  1055. } else if (node.sheet) {
  1056. try {
  1057. var cssRules = node.sheet.cssRules;
  1058. if (cssRules) {
  1059. logger.debug('same domain css poll loaded: ' + url);
  1060. loaded = 1;
  1061. }
  1062. } catch (ex) {
  1063. var exName = ex.name;
  1064. logger.debug('css poll exception: ' + exName + ' ' + ex.code + ' ' + url);
  1065. // http://www.w3.org/TR/dom/#dom-domexception-code
  1066. if (// exName == 'SecurityError' ||
  1067. // for old firefox
  1068. exName === 'NS_ERROR_DOM_SECURITY_ERR') {
  1069. logger.debug('css poll exception: ' + exName + 'loaded : ' + url);
  1070. loaded = 1;
  1071. }
  1072. }
  1073. }
  1074. return loaded;
  1075. }
  1076. // single thread is ok
  1077. function cssPoll() {
  1078. for (var url in monitors) {
  1079. var callbackObj = monitors[url],
  1080. node = callbackObj.node;
  1081. if (isCssLoaded(node, url)) {
  1082. if (callbackObj.callback) {
  1083. callbackObj.callback.call(node);
  1084. }
  1085. delete monitors[url];
  1086. }
  1087. }
  1088. if (Utils.isEmptyObject(monitors)) {
  1089. logger.debug('clear css poll timer');
  1090. timer = 0;
  1091. } else {
  1092. timer = setTimeout(cssPoll, CSS_POLL_INTERVAL);
  1093. }
  1094. }
  1095. // refer : http://lifesinger.org/lab/2011/load-js-css/css-preload.html
  1096. // 暂时不考虑如何判断失败,如 404 等
  1097. Utils.pollCss = function (node, callback) {
  1098. var href = node.href,
  1099. arr;
  1100. arr = monitors[href] = {};
  1101. arr.node = node;
  1102. arr.callback = callback;
  1103. startCssTimer();
  1104. };
  1105. Utils.isCssLoaded = isCssLoaded;
  1106. })(KISSY);
  1107. /*
  1108. References:
  1109. - http://unixpapa.com/js/dyna.html
  1110. - http://www.blaze.io/technical/ies-premature-execution-problem/
  1111. `onload` event is supported in WebKit since 535.23
  1112. - https://bugs.webkit.org/show_activity.cgi?id=38995
  1113. `onload/onerror` event is supported since Firefox 9.0
  1114. - https://bugzilla.mozilla.org/show_bug.cgi?id=185236
  1115. - https://developer.mozilla.org/en/HTML/Element/link#Stylesheet_load_events
  1116. monitor css onload across browsers.issue about 404 failure.
  1117. - firefox not ok(4 is wrong):
  1118. - http://yearofmoo.com/2011/03/cross-browser-stylesheet-preloading/
  1119. - all is ok
  1120. - http://lifesinger.org/lab/2011/load-js-css/css-preload.html
  1121. - others
  1122. - http://www.zachleat.com/web/load-css-dynamically/
  1123. *//**
  1124. * @ignore
  1125. * getScript support for css and js callback after load
  1126. * @author yiminghe@gmail.com
  1127. */
  1128. (function (S) {
  1129. // --no-module-wrap--
  1130. var MILLISECONDS_OF_SECOND = 1000,
  1131. win = S.Env.host,
  1132. doc = win.document,
  1133. Utils = S.Loader.Utils,
  1134. // solve concurrent requesting same script file
  1135. jsCssCallbacks = {},
  1136. webkit = Utils.webkit,
  1137. headNode;
  1138. /**
  1139. * Load a javascript/css file from the server using a GET HTTP request,
  1140. * then execute it.
  1141. *
  1142. * for example:
  1143. * @example
  1144. * getScript(url, success, charset);
  1145. * // or
  1146. * getScript(url, {
  1147. * charset: string
  1148. * success: fn,
  1149. * error: fn,
  1150. * timeout: number
  1151. * });
  1152. *
  1153. * Note 404/500 status in ie<9 will trigger success callback.
  1154. * If you want a jsonp operation, please use {@link KISSY.IO} instead.
  1155. *
  1156. * @param {String} url resource's url
  1157. * @param {Function|Object} [success] success callback or config
  1158. * @param {Function} [success.success] success callback
  1159. * @param {Function} [success.error] error callback
  1160. * @param {Number} [success.timeout] timeout (s)
  1161. * @param {String} [success.charset] charset of current resource
  1162. * @param {String} [charset] charset of current resource
  1163. * @return {HTMLElement} script/style node
  1164. * @member KISSY
  1165. */
  1166. S.getScript = function (url, success, charset) {
  1167. // can not use KISSY.Uri, url can not be encoded for some url
  1168. // eg: /??dom.js,event.js , ? , should not be encoded
  1169. var config = success,
  1170. css = Utils.endsWith(url, '.css'),
  1171. error, timeout, attrs, callbacks, timer;
  1172. if (typeof config === 'object') {
  1173. success = config.success;
  1174. error = config.error;
  1175. timeout = config.timeout;
  1176. charset = config.charset;
  1177. attrs = config.attrs;
  1178. }
  1179. if (css && Utils.ieMode < 10) {
  1180. if (doc.getElementsByTagName('style').length + doc.getElementsByTagName('link').length >= 31) {
  1181. if (win.console) {
  1182. win.console.error('style and link\'s number is more than 31.' +
  1183. 'ie < 10 can not insert link: ' + url);
  1184. }
  1185. if (error) {
  1186. error();
  1187. }
  1188. return;
  1189. }
  1190. }
  1191. callbacks = jsCssCallbacks[url] = jsCssCallbacks[url] || [];
  1192. callbacks.push([success, error]);
  1193. if (callbacks.length > 1) {
  1194. return callbacks.node;
  1195. }
  1196. var node = doc.createElement(css ? 'link' : 'script'),
  1197. clearTimer = function () {
  1198. if (timer) {
  1199. clearTimeout(timer);
  1200. timer = undefined;
  1201. }
  1202. };
  1203. if (attrs) {
  1204. Utils.each(attrs, function (v, n) {
  1205. node.setAttribute(n, v);
  1206. });
  1207. }
  1208. if (charset) {
  1209. node.charset = charset;
  1210. }
  1211. if (css) {
  1212. node.href = url;
  1213. node.rel = 'stylesheet';
  1214. // set media to something non-matching to ensure it'll fetch without blocking render
  1215. node.media = 'async';
  1216. } else {
  1217. node.src = url;
  1218. node.async = true;
  1219. }
  1220. callbacks.node = node;
  1221. var end = function (error) {
  1222. var index = error,
  1223. fn;
  1224. clearTimer();
  1225. // set media back to `all` so that the stylesheet applies once it loads
  1226. // https://github.com/filamentgroup/loadCSS
  1227. if (css) {
  1228. node.media = 'all';
  1229. }
  1230. Utils.each(jsCssCallbacks[url], function (callback) {
  1231. if ((fn = callback[index])) {
  1232. fn.call(node);
  1233. }
  1234. });
  1235. delete jsCssCallbacks[url];
  1236. };
  1237. var useNative = 'onload' in node;
  1238. // onload for webkit 535.23 Firefox 9.0
  1239. // https://bugs.webkit.org/show_activity.cgi?id=38995
  1240. // https://bugzilla.mozilla.org/show_bug.cgi?id=185236
  1241. // https://developer.mozilla.org/en/HTML/Element/link#Stylesheet_load_events
  1242. // phantomjs 1.7 == webkit 534.34
  1243. var forceCssPoll = S.Config.forceCssPoll ||
  1244. (webkit && webkit < 536) ||
  1245. // unknown browser defaults to css poll
  1246. // https://github.com/kissyteam/kissy/issues/607
  1247. (!webkit && !Utils.trident && !Utils.gecko);
  1248. if (css && forceCssPoll && useNative) {
  1249. useNative = false;
  1250. }
  1251. function onload() {
  1252. var readyState = node.readyState;
  1253. if (!readyState ||
  1254. readyState === 'loaded' ||
  1255. readyState === 'complete') {
  1256. node.onreadystatechange = node.onload = null;
  1257. end(0);
  1258. }
  1259. }
  1260. //标准浏览器 css and all script
  1261. if (useNative) {
  1262. node.onload = onload;
  1263. node.onerror = function () {
  1264. node.onerror = null;
  1265. end(1);
  1266. };
  1267. } else if (css) {
  1268. // old chrome/firefox for css
  1269. Utils.pollCss(node, function () {
  1270. end(0);
  1271. });
  1272. } else {
  1273. node.onreadystatechange = onload;
  1274. }
  1275. if (timeout) {
  1276. timer = setTimeout(function () {
  1277. end(1);
  1278. }, timeout * MILLISECONDS_OF_SECOND);
  1279. }
  1280. if (!headNode) {
  1281. headNode = Utils.docHead();
  1282. }
  1283. if (css) {
  1284. // css order matters
  1285. // so can not use css in head
  1286. headNode.appendChild(node);
  1287. } else {
  1288. // can use js in head
  1289. headNode.insertBefore(node, headNode.firstChild);
  1290. }
  1291. return node;
  1292. };
  1293. })(KISSY);
  1294. /*
  1295. yiminghe@gmail.com refactor@2012-03-29
  1296. - 考虑连续重复请求单个 script 的情况,内部排队
  1297. yiminghe@gmail.com 2012-03-13
  1298. - getScript
  1299. - 404 in ie<9 trigger success , others trigger error
  1300. - syntax error in all trigger success
  1301. *//**
  1302. * @ignore
  1303. * Declare config info for KISSY.
  1304. * @author yiminghe@gmail.com
  1305. */
  1306. (function (S) {
  1307. // --no-module-wrap--
  1308. var Loader = S.Loader,
  1309. Package = Loader.Package,
  1310. Utils = Loader.Utils,
  1311. host = S.Env.host,
  1312. Config = S.Config,
  1313. location = host.location,
  1314. configFns = Config.fns;
  1315. // how to load mods by path
  1316. Config.loadModsFn = function (rs, config) {
  1317. S.getScript(rs.url, config);
  1318. };
  1319. // how to get mod url
  1320. Config.resolveModFn = function (mod) {
  1321. var name = mod.name,
  1322. filter, t, url,
  1323. // deprecated! do not use path config
  1324. subPath = mod.path;
  1325. var packageInfo = mod.getPackage();
  1326. if (!packageInfo) {
  1327. return name;
  1328. }
  1329. var packageBase = packageInfo.getBase();
  1330. var packageName = packageInfo.name;
  1331. var extname = mod.getType();
  1332. var suffix = '.' + extname;
  1333. if (!subPath) {
  1334. // special for css module
  1335. name = name.replace(/\.css$/, '');
  1336. filter = packageInfo.getFilter() || '';
  1337. if (typeof filter === 'function') {
  1338. subPath = filter(name, extname);
  1339. } else if (typeof filter === 'string') {
  1340. if (filter) {
  1341. filter = '-' + filter;
  1342. }
  1343. subPath = name + filter + suffix;
  1344. }
  1345. }
  1346. // core package
  1347. if (packageName === 'core') {
  1348. url = packageBase + subPath;
  1349. } else if (name === packageName) {
  1350. // packageName: a/y use('a/y');
  1351. // do not use this on production, can not be combo ed with other modules from same package
  1352. url = packageBase.substring(0, packageBase.length - 1) + filter + suffix;
  1353. } else {
  1354. subPath = subPath.substring(packageName.length + 1);
  1355. url = packageBase + subPath;
  1356. }
  1357. if ((t = mod.getTag())) {
  1358. t += suffix;
  1359. url += '?t=' + t;
  1360. }
  1361. return url;
  1362. };
  1363. configFns.requires = shortcut('requires');
  1364. configFns.alias = shortcut('alias');
  1365. configFns.packages = function (config) {
  1366. var Config = this.Config,
  1367. packages = Config.packages;
  1368. if (config) {
  1369. Utils.each(config, function (cfg, key) {
  1370. // object type
  1371. var name = cfg.name = cfg.name || key;
  1372. var base = cfg.base || cfg.path;
  1373. if (base) {
  1374. cfg.base = normalizePath(base, true);
  1375. }
  1376. if (packages[name]) {
  1377. packages[name].reset(cfg);
  1378. } else {
  1379. packages[name] = new Package(cfg);
  1380. }
  1381. });
  1382. return undefined;
  1383. } else if (config === false) {
  1384. Config.packages = {
  1385. core: packages.core
  1386. };
  1387. return undefined;
  1388. } else {
  1389. return packages;
  1390. }
  1391. };
  1392. configFns.modules = function (modules) {
  1393. if (modules) {
  1394. Utils.each(modules, function (modCfg, modName) {
  1395. var url = modCfg.url;
  1396. if (url) {
  1397. modCfg.url = normalizePath(url);
  1398. }
  1399. var mod = Utils.createModule(modName, modCfg);
  1400. // #485, invalid after add
  1401. if (mod.status === Loader.Status.INIT) {
  1402. Utils.mix(mod, modCfg);
  1403. }
  1404. });
  1405. }
  1406. };
  1407. configFns.base = function (base) {
  1408. var self = this,
  1409. corePackage = Config.packages.core;
  1410. if (!base) {
  1411. return corePackage && corePackage.getBase();
  1412. }
  1413. self.config('packages', {
  1414. core: {
  1415. base: base
  1416. }
  1417. });
  1418. return undefined;
  1419. };
  1420. function shortcut(attr) {
  1421. return function (config) {
  1422. var newCfg = {};
  1423. for (var name in config) {
  1424. newCfg[name] = {};
  1425. newCfg[name][attr] = config[name];
  1426. }
  1427. S.config('modules', newCfg);
  1428. };
  1429. }
  1430. function normalizePath(base, isDirectory) {
  1431. base = Utils.normalizeSlash(base);
  1432. if (isDirectory && base.charAt(base.length - 1) !== '/') {
  1433. base += '/';
  1434. }
  1435. if (location) {
  1436. if (Utils.startsWith(base, 'http:') ||
  1437. Utils.startsWith(base, 'https:') ||
  1438. Utils.startsWith(base, 'file:')) {
  1439. return base;
  1440. }
  1441. base = location.protocol + '//' + location.host + Utils.normalizePath(location.pathname, base);
  1442. }
  1443. return base;
  1444. }
  1445. })(KISSY);
  1446. /**
  1447. * combo loader for KISSY. using combo to load module files.
  1448. * @ignore
  1449. * @author yiminghe@gmail.com
  1450. */
  1451. (function (S, undefined) {
  1452. // --no-module-wrap--
  1453. var logger = S.getLogger('s/loader');
  1454. var Loader = S.Loader,
  1455. Config = S.Config,
  1456. Status = Loader.Status,
  1457. Utils = Loader.Utils,
  1458. addModule = Utils.addModule,
  1459. each = Utils.each,
  1460. getHash = Utils.getHash,
  1461. LOADING = Status.LOADING,
  1462. LOADED = Status.LOADED,
  1463. ERROR = Status.ERROR,
  1464. oldIE = Utils.ieMode && Utils.ieMode < 10;
  1465. function loadScripts(rss, callback, timeout) {
  1466. var count = rss && rss.length,
  1467. errorList = [],
  1468. successList = [];
  1469. function complete() {
  1470. if (!(--count)) {
  1471. callback(successList, errorList);
  1472. }
  1473. }
  1474. each(rss, function (rs) {
  1475. var mod;
  1476. var config = {
  1477. timeout: timeout,
  1478. success: function () {
  1479. successList.push(rs);
  1480. if (mod && currentMod) {
  1481. // standard browser(except ie9) fire load after KISSY.add immediately
  1482. logger.debug('standard browser get mod name after load: ' + mod.name);
  1483. addModule(mod.name, currentMod.factory, currentMod.config);
  1484. currentMod = undefined;
  1485. }
  1486. complete();
  1487. },
  1488. error: function () {
  1489. errorList.push(rs);
  1490. complete();
  1491. },

Large files files are truncated, but you can click here to view the full file