/static/js/post_list.js

https://bitbucket.org/NickyHuang5293/ulip · JavaScript · 485 lines · 379 code · 74 blank · 32 comment · 12 complexity · cfb63c16ccdd6dba31a218a7be5ad391 MD5 · raw file

  1. /**
  2. * Created by PyCharm.
  3. * User: Administrator
  4. * Date: 12-5-3
  5. * Time: 下午9:10
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. (function(){
  9. window.Post = Backbone.Model.extend({
  10. urlRoot: POST_API,
  11. idAttribute:'post_id'
  12. });
  13. window.Comment = Backbone.Model.extend({
  14. urlRoot: COMMENT_API,
  15. idAttribute:'comment_id'
  16. });
  17. window.Posts = PaginatedCollection.extend({
  18. model: Post,
  19. urlRoot: POST_API,
  20. // properies needed for PaginatedCollection
  21. baseUrl:POST_API,
  22. limit: 10,
  23. filter_options : {"course__course_id":1},
  24. sort_field : "-ctime",
  25. // comparator: function(post){
  26. // return post.get("ctime");
  27. // },
  28. maybeFetch : function(options){
  29. if(this._fetched){
  30. options.success && options.success();
  31. return;
  32. }
  33. var self = this;
  34. var successWrapper = function(success){
  35. return function(){
  36. self._fetched = true;
  37. success && success.apply(this,arguments);
  38. };
  39. };
  40. options.success = successWrapper(options.success);
  41. this.fetch(options);
  42. }
  43. });
  44. window.Comments = Backbone.Collection.extend({
  45. model: Comment,
  46. urlRoot: COMMENT_API,
  47. maybeFetch : function(options){
  48. if(this._fetched){
  49. options.success && options.success();
  50. return;
  51. }
  52. var self = this;
  53. var successWrapper = function(success){
  54. return function(){
  55. self._fetched = true;
  56. success && success.apply(this,arguments);
  57. };
  58. };
  59. options.success = successWrapper(options.success);
  60. this.fetch(options);
  61. }
  62. });
  63. window.PostView = Backbone.View.extend({
  64. tagName: 'div',
  65. className: 'postAbs',
  66. events: {
  67. 'click .delete': 'clear',
  68. 'click .show-comment' :'show_comment',
  69. 'click .comment-submit': 'add_comment'
  70. },
  71. initialize: function(){
  72. this.model.bind('change', this.change_view, this);
  73. this.model.bind('destroy', this.remove, this);
  74. $(this.el).data("comment_state","hide");
  75. this.comment_collection = new Comments();
  76. },
  77. change_view: function(){
  78. this.$('.comment-num').html(this.model.get("comment_count"));
  79. },
  80. clear: function(e){
  81. e.preventDefault();
  82. this.model.destroy({
  83. success: function(){ },
  84. error: function(){ alert("delete failed!"); },
  85. wait:true
  86. });
  87. },
  88. showCommentPanel: function(){
  89. this.$(".comment-panel").slideDown();
  90. },
  91. hideCommentPanel: function(){
  92. this.$(".comment-panel").slideUp();
  93. },
  94. show_comment: function(e){
  95. e.preventDefault();
  96. if($(this.el).data("comment_state")=="hide"){
  97. this.showCommentPanel();
  98. $(this.el).data("comment_state","show");
  99. var view = new CommentSlideDownView({
  100. el: this.$('.comment-panel'),
  101. collection: this.comment_collection,
  102. model: this.model
  103. });
  104. view.setPostPt(this);
  105. view.render(this.model);
  106. this.comment_collection.fetch({
  107. fail: function(){alert("failed to load comments");},
  108. data: {"post__post_id": this.model.id}
  109. });
  110. }
  111. else{
  112. this.hideCommentPanel();
  113. $(this.el).data("comment_state","hide");
  114. }
  115. },
  116. add_comment: function(){
  117. var content = this.$('#new-comment-content').val();
  118. if(content){
  119. // add new comment
  120. post_list_app.comments.create({
  121. school: SCHOOL,
  122. course: COURSE,
  123. creator: PARTICIPANT,
  124. content: content,
  125. post: POST_API + this.model.id +"/",
  126. isdeleted : false
  127. },{ wait: true});
  128. this.$('#new-comment-content').val('');
  129. // update the comment-count
  130. this.model.save(
  131. {comment_count: this.model.get("comment_count") + 1} ,
  132. {error: function(){alert("fail update")}}
  133. );
  134. }
  135. },
  136. render: function(){
  137. $(this.el).html(ich.postAbsTemplate(this.model.toJSON()));
  138. return this;
  139. }
  140. });
  141. window.CommentView = Backbone.View.extend({
  142. tagName: 'div',
  143. className: 'comment',
  144. events:{
  145. 'click .delete-comment': 'clear'
  146. },
  147. initialize: function(){
  148. this.model.bind('change', this.render, this);
  149. this.model.bind('destroy', this.remove, this);
  150. },
  151. setPost: function(post){
  152. this.post = post;
  153. },
  154. clear: function(e){
  155. //window.alert('deleting comment');
  156. e.preventDefault();
  157. this.model.destroy({
  158. error: function(){ alert("delete failed!"); },
  159. wait:true
  160. });
  161. // update the comment-count
  162. this.post.save(
  163. {comment_count: this.post.get("comment_count") - 1},
  164. {error: function(){ alert("fail update"); }}
  165. );
  166. },
  167. render: function(){
  168. $(this.el).html(ich.commentTemplate(this.model.toJSON()));
  169. return this;
  170. }
  171. });
  172. window.PostListView = Backbone.View.extend({
  173. initialize: function(){
  174. _.bindAll(this, 'addOne', 'appendOne','addAll');
  175. this.collection.bind('add', this.addOne);
  176. this.collection.bind('reset', this.addAll, this);
  177. //$(window).bind('scroll', this.collection, this.loadMore);
  178. this.views = [];
  179. },
  180. addAll: function(){
  181. this.views = []; //view.remove()清除
  182. this.collection.each(this.appendOne);
  183. },
  184. //used when fetch old posts
  185. appendOne: function(post){
  186. var view = new PostView({
  187. model: post
  188. });
  189. $(this.el).append(view.render().el);
  190. this.views.push(view);
  191. view.bind('all', this.rethrow, this);
  192. },
  193. //used when create new post
  194. addOne: function(post){
  195. var view = new PostView({
  196. model: post
  197. });
  198. $(this.el).prepend(view.render().el);
  199. this.views.push(view);
  200. view.bind('all', this.rethrow, this);
  201. },
  202. rethrow: function(){
  203. this.trigger.apply(this, arguments);
  204. }
  205. });
  206. window.EditorView = Backbone.View.extend({
  207. events:{
  208. 'click .b-submit': 'createPost'
  209. },
  210. createPost: function(){
  211. //var content = this.$('#textContent').val();
  212. var content = this.$('#textContent').editorBox("get_code");
  213. if(content){
  214. this.collection.create({
  215. school: SCHOOL,
  216. course: COURSE,
  217. creator: PARTICIPANT,
  218. content: content,
  219. comment_count : 0,
  220. isdeleted : false
  221. },{ wait: true});
  222. this.$('#textContent').val('');
  223. }
  224. },
  225. render: function(){
  226. $(this.el).html(ich.postEditor());
  227. }
  228. });
  229. window.CommentSlideDownView = Backbone.View.extend({
  230. events:{
  231. 'click .comment-submit': 'addComment',
  232. 'click .view-all': 'view_all_comments'
  233. },
  234. initialize: function(){
  235. _.bindAll(this, 'addOne', 'addAll');
  236. this.collection.bind('add', this.addOne);
  237. this.collection.bind('reset', this.addAll, this);
  238. this.views = [];
  239. this.postpt = null;
  240. },
  241. addAll: function(){
  242. this.views = [];
  243. this.$('.comments').html("");
  244. this.collection.each(this.addOne);
  245. },
  246. addOne: function(comment){
  247. var view = new CommentView({
  248. model: comment
  249. });
  250. view.setPost(this.post);
  251. this.$(".comments").prepend(view.render().el);
  252. this.views.push(view);
  253. view.bind('all', this.rethrow, this);
  254. },
  255. //set PostView pointer(this)
  256. setPostPt: function(pt){
  257. this.postpt = pt;
  258. },
  259. view_all_comments: function(e){
  260. e.preventDefault();
  261. //popup window
  262. $.blockUI({
  263. message: $('.view-all-panel'), //pop up element
  264. css: { //element css
  265. textAlign:'left',
  266. top: '50%',
  267. left: '50%',
  268. marginLeft: '-350px',
  269. marginTop: '-250px',
  270. background: 'transparent',
  271. width: '700px',
  272. height: '500px',
  273. cursor: 'arrow',
  274. border: 'none'
  275. },
  276. overlayCSS: {
  277. cursor:'arrow'
  278. }
  279. });
  280. $('.blockOverlay').click($.unblockUI); //close when click on overlay
  281. $('.view-all-panel #close').click($.unblockUI); //close when click on close-button
  282. $('.view-all-panel .popup-comment').click(function(e){e.stopPropagation();});
  283. $('.view-all-panel').click( $.unblockUI);
  284. //hide comment slide down panel
  285. $(this.el).hide();
  286. $(this.postpt.el).data("comment_state","hide");
  287. post_list_app.popupComment.render( this.model);
  288. post_list_app.comments.fetch({
  289. fail: function(){alert("fail");},
  290. data: {"post__post_id": this.model.id}
  291. });
  292. },
  293. addComment: function(){
  294. var content = this.$('#new-comment-content').val();
  295. if(content){
  296. // add new comment
  297. this.collection.create({
  298. school: SCHOOL,
  299. course: COURSE,
  300. creator: PARTICIPANT,
  301. content: content,
  302. post: POST_API + this.post.id +"/",
  303. isdeleted : false
  304. },{ wait: true});
  305. this.$('#new-comment-content').val('');
  306. // update the comment-count
  307. this.post.save(
  308. {comment_count: this.post.get("comment_count") + 1} ,
  309. {error: function(){alert("fail update")}}
  310. );
  311. }
  312. },
  313. render: function(post){
  314. this.post = post;
  315. $(this.el).html(ich.commentSlideDown(post.toJSON()));
  316. }
  317. });
  318. window.CommentPopupView = Backbone.View.extend({
  319. events:{
  320. 'click .headAction': 'closePopup',
  321. 'click .comment-submit': 'addComment'
  322. },
  323. initialize: function(){
  324. _.bindAll(this, 'addOne', 'addAll');
  325. this.collection.bind('add', this.addOne);
  326. this.collection.bind('reset', this.addAll, this);
  327. this.views = [];
  328. },
  329. addAll: function(){
  330. this.views = [];
  331. this.collection.each(this.addOne);
  332. },
  333. addOne: function(comment){
  334. var view = new CommentView({
  335. model: comment
  336. });
  337. view.setPost(this.post);
  338. this.$(".comments").prepend(view.render().el);
  339. this.views.push(view);
  340. view.bind('all', this.rethrow, this);
  341. },
  342. closePopup: function(){
  343. $(this.el).hide();
  344. },
  345. addComment: function(){
  346. var content = this.$('#new-comment-content').val();
  347. if(content){
  348. // add new comment
  349. this.collection.create({
  350. school: SCHOOL,
  351. course: COURSE,
  352. creator: PARTICIPANT,
  353. content: content,
  354. post: POST_API + this.post.id +"/",
  355. isdeleted : false
  356. },{ wait: true});
  357. this.$('#new-comment-content').val('');
  358. // update the comment-count
  359. this.post.save(
  360. {comment_count: this.post.get("comment_count") + 1} ,
  361. {error: function(){alert("fail update")}}
  362. );
  363. }
  364. },
  365. render: function(post){
  366. this.post = post;
  367. $(this.el).html(ich.commentPopup(this.post.toJSON()));
  368. //this.postDetailView.render();
  369. }
  370. });
  371. window.PostDetailView = Backbone.View.extend({
  372. render: function(){
  373. $(this.el).html(ich.postDetail(this.model.toJSON()));
  374. return this;
  375. }
  376. });
  377. window.PostPaginatedCollection = PaginatedCollection.extend({
  378. baseUrl : "/api/v1/post/"
  379. });
  380. $(function(){
  381. window.post_list_app = window.post_list_app || {};
  382. post_list_app.posts = new Posts();
  383. post_list_app.comments = new Comments();
  384. //initialize the post editor view
  385. post_list_app.editor = new EditorView({
  386. el: $("#composer"),
  387. collection: post_list_app.posts
  388. });
  389. post_list_app.editor.render();
  390. //initialize the post list view
  391. post_list_app.postlist = new PostListView({
  392. el:$(".postArea"),
  393. collection: post_list_app.posts
  394. });
  395. post_list_app.posts.maybeFetch({
  396. success: _.bind(post_list_app.postlist.render, post_list_app.postlist)
  397. //data: {"course__course_id":1,"order_by":"-ctime"}
  398. });
  399. //initialize the popup view, but do not show it
  400. post_list_app.popupComment = new CommentPopupView({
  401. el:$(".view-all-panel .popup-comment"),
  402. collection: post_list_app.comments
  403. });
  404. //$("#textContent").val("aaaaa");
  405. require([
  406. 'tinymce.editor_box'
  407. ], function() {
  408. $("#textContent").editorBox();
  409. });
  410. });
  411. })();