topic-page.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. KISSY.add('op_dever/topic-page',function (S, Node, DOM, Base, Event, IO) {
  2. var $ = Node.all;
  3. var TopicPage = function (target, config) {
  4. var self = this;
  5. if(!config) config = {};
  6. if(target) S.mix(config, {target: target});
  7. TopicPage.superclass.constructor.call(self, config);
  8. return self;
  9. }
  10. S.extend(TopicPage, Base, {
  11. /*设置页面数据*/
  12. setData: function (data) {
  13. var self = this;
  14. self.data = data;
  15. return self;
  16. },
  17. getData: function () {
  18. var self = this;
  19. return self.data;
  20. },
  21. /*代理页面渲染*/
  22. render: function () {
  23. var self = this;
  24. self._load(self._render);
  25. return self;
  26. },
  27. /*页面图片加载完成后回调*/
  28. _load: function (callback) {
  29. var self = this;
  30. var waitLoadingImgs = [];
  31. if (!S.isUndefined(self.data.background_image)) {
  32. waitLoadingImgs.push(self.data.background_image);
  33. }
  34. if (!S.isUndefined(self.data.button_image)) {
  35. waitLoadingImgs.push(self.data.button_image.url);
  36. }
  37. if (!S.isUndefined(self.data.upper_image)) {
  38. waitLoadingImgs.push(self.data.upper_image.url);
  39. }
  40. var hasLoadingTimes = 0;
  41. for (var i = 0; i < waitLoadingImgs.length; i++) {
  42. var oi = new Image();
  43. oi.src = waitLoadingImgs[i];
  44. oi.onload = function () {
  45. ++hasLoadingTimes;
  46. };
  47. }
  48. var ID = setInterval(function () {
  49. if (hasLoadingTimes == waitLoadingImgs.length) {
  50. clearInterval(ID);
  51. S.log('image loading done');
  52. //self._sleep(3);
  53. callback(self);
  54. }
  55. }, 10);
  56. return self;
  57. },
  58. _sleep: function (s) {
  59. var start = new Date().getTime();
  60. while(true) {
  61. if (new Date().getTime()-start > s*1000)
  62. break;
  63. }
  64. },
  65. /*页面渲染*/
  66. _render: function (self) {
  67. var data = self.data,
  68. target = self.get('target');
  69. target.html('');
  70. // if (!S.isUndefined(data.background_color)) {
  71. // target.css({'background-color': data.background_color});
  72. // }
  73. if (!S.isUndefined(data.background_image) && (S.isUndefined(data.background_isShow) || data.background_isShow == true) ) {
  74. var $topic_bg_image = $('<div class="ks-topic-bg-image"></div>').appendTo(target);
  75. $topic_bg_image.css({'background-image': 'url('+data.background_image+')'});
  76. }
  77. if (!S.isUndefined(data.upper_image) && (S.isUndefined(data.upper_image.isShow) || data.upper_image.isShow == true)) {
  78. var upper_image_width = Math.round(data.upper_image.width/2),
  79. upper_image_height = Math.round(data.upper_image.height/2);
  80. if (S.isUndefined(data.move)) { //默认不移动
  81. data.move = '';
  82. }
  83. var $style = {};
  84. var $anim_style = {'opacity':'1'};
  85. if (!S.isUndefined(data.position)) {
  86. switch (data.position) {
  87. case 'center': //居中
  88. $style = {
  89. 'left':'50%',
  90. 'top':'50%',
  91. 'margin-left':'-'+upper_image_width/2+'px',
  92. 'margin-top':'-'+upper_image_height/2+'px',
  93. 'opacity': '0'
  94. };
  95. if (data.move == 'left') {
  96. $style.left = '70%';
  97. $anim_style.left = '50%';
  98. } else if (data.move == 'up') {
  99. $style.top = '70%';
  100. $anim_style.top = '50%';
  101. }
  102. break;
  103. case 'leftup': //左上
  104. $style = {
  105. 'left':'0%',
  106. 'top':'0%',
  107. 'opacity': '0'
  108. };
  109. if (data.move == 'left') {
  110. $style.left = '20%';
  111. $anim_style.left = '0%';
  112. } else if (data.move == 'up') {
  113. $style.top = '20%';
  114. $anim_style.top = '0%';
  115. }
  116. break;
  117. case 'leftdown': //左下
  118. $style = {
  119. 'left':'0%',
  120. 'bottom':'0%',
  121. 'opacity': '0'
  122. };
  123. if (data.move == 'left') {
  124. $style.left = '20%';
  125. $anim_style.left = '0%';
  126. } else if (data.move == 'up') {
  127. $style.bottom = '-20%';
  128. $anim_style.bottom = '0%';
  129. }
  130. break;
  131. case 'rightup': //右上
  132. $style = {
  133. 'right':'0%',
  134. 'top':'0%',
  135. 'opacity': '0'
  136. };
  137. if (data.move == 'left') {
  138. $style.right = '-20%';
  139. $anim_style.right = '0%';
  140. } else if (data.move == 'up') {
  141. $style.top = '20%';
  142. $anim_style.top = '0%';
  143. }
  144. break;
  145. case 'rightdown': //右下
  146. $style = {
  147. 'right':'0%',
  148. 'bottom':'0%',
  149. 'opacity':'0'
  150. };
  151. if (data.move == 'left') {
  152. $style.right = '-20%';
  153. $anim_style.right = '0%';
  154. } else if (data.move == 'up') {
  155. $style.bottom = '-20%';
  156. $anim_style.bottom = '0%';
  157. }
  158. break;
  159. default:
  160. alert('未知操作');
  161. }
  162. } else { //默认样式
  163. $style = {'left':'50%', 'top':'70%', 'margin-left':'-'+upper_image_width/2+'px', 'margin-top':'-'+upper_image_height/2+'px', 'opacity': '0'};
  164. $anim_style = {'top':'50%', 'opacity':'1'};
  165. }
  166. $style.width = upper_image_width; //上层图片缩放一倍
  167. var new_upper_image = $('<img class="layer_anim" src="'+data.upper_image.url+'" />');
  168. new_upper_image.css($style);
  169. target.append(new_upper_image);
  170. setTimeout(function () {
  171. new_upper_image.css($anim_style);
  172. }, 1);
  173. }
  174. if (!S.isUndefined(data.button_image) && (S.isUndefined(data.button_image.isShow) || data.button_image.isShow == true)) {
  175. var button_image_width = Math.round(data.button_image.width/2),
  176. button_image_height = Math.round(data.button_image.height/2);
  177. $style = {};
  178. if (!S.isUndefined(data.button_position)) {
  179. switch (data.button_position) {
  180. case 'up':
  181. $style = {
  182. 'left':'50%',
  183. 'top':'20%',
  184. 'margin-left':'-'+button_image_width/2+'px'
  185. }
  186. break;
  187. case 'center':
  188. $style = {
  189. 'left':'50%',
  190. 'top':'50%',
  191. 'margin-left':'-'+button_image_width/2+'px',
  192. 'margin-top':'-'+button_image_height/2+'px'
  193. }
  194. break;
  195. case 'down':
  196. $style = {
  197. 'left':'50%',
  198. 'bottom':'20%',
  199. 'margin-left':'-'+button_image_width/2+'px'
  200. }
  201. break;
  202. default:
  203. alert('未知操作');
  204. }
  205. } else {
  206. $style = {
  207. 'left':'50%',
  208. 'top':'50%',
  209. 'margin-left':'-'+button_image_width/2+'px',
  210. 'margin-top':'-'+button_image_height/2+'px'
  211. }
  212. }
  213. $style.width = button_image_width; //上层图片缩放一倍
  214. if (!S.isUndefined(data.button_link)) {
  215. var new_button_image = $('<a class="topic_button" href="'+data.button_link+'" ><img src="'+data.button_image.url+'" /></a>');
  216. } else {
  217. var new_button_image = $('<img class="topic_button" src="'+data.button_image.url+'" />');
  218. }
  219. new_button_image.css($style);
  220. target.append(new_button_image);
  221. }
  222. S.log('render done!');
  223. return self;
  224. }
  225. }, {
  226. ATTRS: {
  227. target: {
  228. value: "",
  229. getter: function (v) {
  230. return $(v);
  231. }
  232. }
  233. }
  234. });
  235. return TopicPage;
  236. }, {requires:['node', 'dom', 'base', 'event', 'io', './topic-page.css']});