list.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. UE.parse.register("list", function(utils) {
  2. var customCss = [],
  3. customStyle = {
  4. cn: "cn-1-",
  5. cn1: "cn-2-",
  6. cn2: "cn-3-",
  7. num: "num-1-",
  8. num1: "num-2-",
  9. num2: "num-3-",
  10. dash: "dash",
  11. dot: "dot"
  12. };
  13. utils.extend(this, {
  14. liiconpath: "http://bs.baidu.com/listicon/",
  15. listDefaultPaddingLeft: "20"
  16. });
  17. var root = this.root,
  18. ols = root.getElementsByTagName("ol"),
  19. uls = root.getElementsByTagName("ul"),
  20. selector = this.selector;
  21. if (ols.length) {
  22. applyStyle.call(this, ols);
  23. }
  24. if (uls.length) {
  25. applyStyle.call(this, uls);
  26. }
  27. if (ols.length || uls.length) {
  28. customCss.push(selector + " .list-paddingleft-1{padding-left:0}");
  29. customCss.push(
  30. selector +
  31. " .list-paddingleft-2{padding-left:" +
  32. this.listDefaultPaddingLeft +
  33. "px}"
  34. );
  35. customCss.push(
  36. selector +
  37. " .list-paddingleft-3{padding-left:" +
  38. this.listDefaultPaddingLeft * 2 +
  39. "px}"
  40. );
  41. utils.cssRule(
  42. "list",
  43. selector +
  44. " ol," +
  45. selector +
  46. " ul{margin:0;padding:0;}\n" +
  47. selector +
  48. " li{clear:both;}\n" +
  49. customCss.join("\n"),
  50. document
  51. );
  52. }
  53. function applyStyle(nodes) {
  54. var T = this;
  55. utils.each(nodes, function(list) {
  56. if (list.className && /custom_/i.test(list.className)) {
  57. var listStyle = list.className.match(/custom_(\w+)/)[1];
  58. if (listStyle == "dash" || listStyle == "dot") {
  59. utils.pushItem(
  60. customCss,
  61. selector +
  62. " li.list-" +
  63. customStyle[listStyle] +
  64. "{background-image:url(" +
  65. T.liiconpath +
  66. customStyle[listStyle] +
  67. ".gif)}"
  68. );
  69. utils.pushItem(
  70. customCss,
  71. selector +
  72. " ul.custom_" +
  73. listStyle +
  74. "{list-style:none;} " +
  75. selector +
  76. " ul.custom_" +
  77. listStyle +
  78. " li{background-position:0 3px;background-repeat:no-repeat}"
  79. );
  80. } else {
  81. var index = 1;
  82. utils.each(list.childNodes, function(li) {
  83. if (li.tagName == "LI") {
  84. utils.pushItem(
  85. customCss,
  86. selector +
  87. " li.list-" +
  88. customStyle[listStyle] +
  89. index +
  90. "{background-image:url(" +
  91. T.liiconpath +
  92. "list-" +
  93. customStyle[listStyle] +
  94. index +
  95. ".gif)}"
  96. );
  97. index++;
  98. }
  99. });
  100. utils.pushItem(
  101. customCss,
  102. selector +
  103. " ol.custom_" +
  104. listStyle +
  105. "{list-style:none;}" +
  106. selector +
  107. " ol.custom_" +
  108. listStyle +
  109. " li{background-position:0 3px;background-repeat:no-repeat}"
  110. );
  111. }
  112. switch (listStyle) {
  113. case "cn":
  114. utils.pushItem(
  115. customCss,
  116. selector +
  117. " li.list-" +
  118. listStyle +
  119. "-paddingleft-1{padding-left:25px}"
  120. );
  121. utils.pushItem(
  122. customCss,
  123. selector +
  124. " li.list-" +
  125. listStyle +
  126. "-paddingleft-2{padding-left:40px}"
  127. );
  128. utils.pushItem(
  129. customCss,
  130. selector +
  131. " li.list-" +
  132. listStyle +
  133. "-paddingleft-3{padding-left:55px}"
  134. );
  135. break;
  136. case "cn1":
  137. utils.pushItem(
  138. customCss,
  139. selector +
  140. " li.list-" +
  141. listStyle +
  142. "-paddingleft-1{padding-left:30px}"
  143. );
  144. utils.pushItem(
  145. customCss,
  146. selector +
  147. " li.list-" +
  148. listStyle +
  149. "-paddingleft-2{padding-left:40px}"
  150. );
  151. utils.pushItem(
  152. customCss,
  153. selector +
  154. " li.list-" +
  155. listStyle +
  156. "-paddingleft-3{padding-left:55px}"
  157. );
  158. break;
  159. case "cn2":
  160. utils.pushItem(
  161. customCss,
  162. selector +
  163. " li.list-" +
  164. listStyle +
  165. "-paddingleft-1{padding-left:40px}"
  166. );
  167. utils.pushItem(
  168. customCss,
  169. selector +
  170. " li.list-" +
  171. listStyle +
  172. "-paddingleft-2{padding-left:55px}"
  173. );
  174. utils.pushItem(
  175. customCss,
  176. selector +
  177. " li.list-" +
  178. listStyle +
  179. "-paddingleft-3{padding-left:68px}"
  180. );
  181. break;
  182. case "num":
  183. case "num1":
  184. utils.pushItem(
  185. customCss,
  186. selector +
  187. " li.list-" +
  188. listStyle +
  189. "-paddingleft-1{padding-left:25px}"
  190. );
  191. break;
  192. case "num2":
  193. utils.pushItem(
  194. customCss,
  195. selector +
  196. " li.list-" +
  197. listStyle +
  198. "-paddingleft-1{padding-left:35px}"
  199. );
  200. utils.pushItem(
  201. customCss,
  202. selector +
  203. " li.list-" +
  204. listStyle +
  205. "-paddingleft-2{padding-left:40px}"
  206. );
  207. break;
  208. case "dash":
  209. utils.pushItem(
  210. customCss,
  211. selector +
  212. " li.list-" +
  213. listStyle +
  214. "-paddingleft{padding-left:35px}"
  215. );
  216. break;
  217. case "dot":
  218. utils.pushItem(
  219. customCss,
  220. selector +
  221. " li.list-" +
  222. listStyle +
  223. "-paddingleft{padding-left:20px}"
  224. );
  225. }
  226. }
  227. });
  228. }
  229. });