PageRenderTime 64ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/static/js/course_res.js

https://bitbucket.org/NickyHuang5293/ulip
JavaScript | 470 lines | 370 code | 84 blank | 16 comment | 16 complexity | be4d6944c1e55aef3b8291a0035de45f 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. /****************************************BOOK RESOURCE START********************************/
  10. //define model "ResBook"
  11. window.ResBook = Backbone.Model.extend({
  12. urlRoot: RES_BOOK_API,
  13. idAttribute:'book_id'
  14. });
  15. window.BookCollection = Backbone.Collection.extend({
  16. model: ResBook,
  17. urlRoot: RES_BOOK_API,
  18. maybeFetch : function(options){
  19. if(this._fetched){
  20. options.success && options.success();
  21. return;
  22. }
  23. var self = this;
  24. var successWrapper = function(success){
  25. return function(){
  26. self._fetched = true;
  27. success && success.apply(this,arguments);
  28. };
  29. };
  30. options.success = successWrapper(options.success);
  31. this.fetch(options);
  32. }
  33. });
  34. window.BookUnitView = Backbone.View.extend({
  35. tagName: 'div',
  36. className: 'unit-block',
  37. events: {
  38. 'click .delete-book': 'clear',
  39. 'click .edit-book': 'edit',
  40. 'click .edit-book-panel .save-modify': 'save_modify',
  41. 'click .edit-book-panel .cancel-modify': 'cancel_modify',
  42. 'click .unit-info, .book-detail-panel .book-slideup': 'ctrlDetailPanel'
  43. },
  44. initialize: function(){
  45. this.model.bind('change', this.render, this);
  46. this.model.bind('destroy', this.remove, this);
  47. $(this.el).data("detail_state","hide");
  48. },
  49. clear: function(e){
  50. e.preventDefault();
  51. this.model.destroy({
  52. success: function(){ },
  53. error: function(){ alert("delete failed!"); },
  54. wait:true
  55. });
  56. },
  57. showBookPanel: function(){
  58. this.$(".unit-info").hide();
  59. this.$(".edit-book-panel").slideDown();
  60. },
  61. hideBookPanel: function(){
  62. this.$(".edit-book-panel").slideUp();
  63. this.$(".unit-info").show();
  64. },
  65. edit: function(e){
  66. e.preventDefault();
  67. //init editor value
  68. if(this.model.get('isRequired'))
  69. this.$('.edit-book-panel .res-required #book-required').attr("checked","checked");
  70. else
  71. this.$('.edit-book-panel .res-required #book-supplemental').attr("checked","checked");
  72. this.$('.edit-book-panel .res-book-info #title').val(this.model.get('book_title'));
  73. this.$('.edit-book-panel .res-book-info #author').val(this.model.get('book_author'));
  74. this.$('.edit-book-panel .res-desc #textContent').val(this.model.get('description'));
  75. if($(this.el).data("detail_state")=="show"){
  76. this.hideDetailPanel(e);
  77. $(this.el).data("detail_state","hide");
  78. }
  79. this.showBookPanel();
  80. e.stopPropagation();
  81. },
  82. save_modify: function(){
  83. var obj = {};
  84. obj['isRequired'] = this.$('#book-required').prop('checked');
  85. obj['book_title'] = this.$('#title').val();
  86. obj['book_author'] = this.$('#author').val();
  87. obj['description'] = this.$('#textContent').val();
  88. this.model.save(obj);
  89. this.hideBookPanel();
  90. },
  91. cancel_modify: function(){
  92. this.hideBookPanel();
  93. },
  94. ctrlDetailPanel: function(e){
  95. e.preventDefault();
  96. if($(this.el).data("detail_state")=="hide"){
  97. this.showDetailPanel();
  98. $(this.el).data("detail_state","show");
  99. }
  100. else{
  101. this.hideDetailPanel();
  102. $(this.el).data("detail_state","hide");
  103. }
  104. },
  105. showDetailPanel: function(){
  106. this.$(".book-detail-panel").slideDown();
  107. },
  108. hideDetailPanel:function(){
  109. this.$(".book-detail-panel").slideUp();
  110. },
  111. render: function(){
  112. var data = this.model.toJSON();
  113. data.description = textToPageEncode(data.description)
  114. $(this.el).html(ich.BookUnit(this.model.toJSON()));
  115. this.$('.book-detail-panel .res-desc-content').html(data.description);
  116. return this;
  117. }
  118. });
  119. window.BookListView = Backbone.View.extend({
  120. initialize: function(){
  121. _.bindAll(this, 'addOne', 'addAll');
  122. this.collection.bind('add', this.addOne);
  123. this.collection.bind('reset', this.addAll, this);
  124. this.views = [];
  125. },
  126. addAll: function(){
  127. this.views = [];
  128. this.collection.each(this.addOne);
  129. },
  130. addOne: function(resbook){
  131. var view = new BookUnitView({
  132. model: resbook
  133. });
  134. $(this.el).prepend(view.render().el);
  135. this.views.push(view);
  136. view.bind('all', this.rethrow, this);
  137. },
  138. rethrow: function(){
  139. this.trigger.apply(this, arguments);
  140. }
  141. });
  142. /****************************************BOOK RESOURCE END********************************/
  143. /****************************************LINK RESOURCE START********************************/
  144. //define model "ResLink"
  145. window.ResLink = Backbone.Model.extend({
  146. urlRoot: RES_LINK_API,
  147. idAttribute:'link_id'
  148. });
  149. window.LinkCollection = Backbone.Collection.extend({
  150. model: ResLink,
  151. urlRoot: RES_LINK_API,
  152. maybeFetch : function(options){
  153. if(this._fetched){
  154. options.success && options.success();
  155. return;
  156. }
  157. var self = this;
  158. var successWrapper = function(success){
  159. return function(){
  160. self._fetched = true;
  161. success && success.apply(this,arguments);
  162. };
  163. };
  164. options.success = successWrapper(options.success);
  165. this.fetch(options);
  166. }
  167. });
  168. window.LinkUnitView = Backbone.View.extend({
  169. tagName: 'div',
  170. className: 'unit-block',
  171. events: {
  172. 'click .delete-link': 'clear',
  173. 'click .edit-link': 'edit',
  174. 'click .unit-info .name': 'view_link',
  175. 'click .edit-link-panel .save-modify': 'save_modify',
  176. 'click .edit-link-panel .cancel-modify': 'cancel_modify',
  177. 'click .unit-info, .link-detail-panel .link-slideup': 'ctrlDetailPanel'
  178. },
  179. initialize: function(){
  180. this.model.bind('change', this.render, this);
  181. this.model.bind('destroy', this.remove, this);
  182. $(this.el).data("detail_state","hide");
  183. },
  184. clear: function(e){
  185. e.preventDefault();
  186. this.model.destroy({
  187. success: function(){ },
  188. error: function(){ alert("delete failed!"); },
  189. wait:true
  190. });
  191. },
  192. view_link: function(e){
  193. e.stopPropagation();
  194. },
  195. showLinkPanel: function(){
  196. this.$(".unit-info").hide();
  197. this.$(".edit-link-panel").slideDown();
  198. },
  199. hideLinkPanel: function(){
  200. this.$(".edit-link-panel").slideUp();
  201. this.$(".unit-info").show();
  202. },
  203. edit: function(e){
  204. e.preventDefault();
  205. //init editor value
  206. if(this.model.get('isRequired'))
  207. this.$('.edit-link-panel .res-required #link-required').attr("checked","checked");
  208. else
  209. this.$('.edit-link-panel .res-required #link-supplemental').attr("checked","checked");
  210. this.$('.edit-link-panel .res-link-info #url').val(this.model.get('link_url'));
  211. this.$('.edit-link-panel .res-link-info #name').val(this.model.get('link_name'));
  212. this.$('.edit-link-panel .res-desc #textContent').val(this.model.get('description'));
  213. if($(this.el).data("detail_state")=="show"){
  214. this.hideDetailPanel(e);
  215. $(this.el).data("detail_state","hide");
  216. }
  217. this.showLinkPanel();
  218. e.stopPropagation();
  219. },
  220. save_modify: function(){
  221. var obj = {};
  222. obj['isRequired'] = this.$('#required').prop('checked');
  223. obj['link_url'] = this.$('#url').val();
  224. obj['link_name'] = this.$('#name').val();
  225. obj['description'] = this.$('#textContent').val();
  226. this.model.save(obj);
  227. this.hideLinkPanel();
  228. },
  229. cancel_modify: function(){
  230. this.hideLinkPanel();
  231. },
  232. ctrlDetailPanel: function(e){
  233. e.preventDefault();
  234. if($(this.el).data("detail_state")=="hide"){
  235. this.showDetailPanel();
  236. $(this.el).data("detail_state","show");
  237. }
  238. else{
  239. this.hideDetailPanel(e);
  240. $(this.el).data("detail_state","hide");
  241. }
  242. },
  243. showDetailPanel: function(){
  244. this.$(".link-detail-panel").slideDown();
  245. },
  246. hideDetailPanel:function(e){
  247. this.$(".link-detail-panel").slideUp();
  248. },
  249. render: function(){
  250. var data = this.model.toJSON();
  251. data.description = textToPageEncode(data.description);
  252. $(this.el).html(ich.LinkUnit(this.model.toJSON()));
  253. this.$('.unit-info .link-info .description').html(data.description);
  254. return this;
  255. }
  256. });
  257. window.LinkListView = Backbone.View.extend({
  258. initialize: function(){
  259. _.bindAll(this, 'addOne', 'addAll');
  260. this.collection.bind('add', this.addOne);
  261. this.collection.bind('reset', this.addAll, this);
  262. this.views = [];
  263. },
  264. addAll: function(){
  265. this.views = [];
  266. this.collection.each(this.addOne);
  267. },
  268. addOne: function(reslink){
  269. var view = new LinkUnitView({
  270. model: reslink
  271. });
  272. $(this.el).prepend(view.render().el);
  273. this.views.push(view);
  274. view.bind('all', this.rethrow, this);
  275. },
  276. rethrow: function(){
  277. this.trigger.apply(this, arguments);
  278. }
  279. });
  280. /****************************************LINK RESOURCE END********************************/
  281. window.ResEditorView = Backbone.View.extend({
  282. events:{
  283. 'click .book-opt': 'showBookPanel',
  284. 'click .new-book-panel .cancel' : 'hideBookPanel',
  285. 'click .new-book-panel .save': 'createResBook',
  286. 'click .link-opt': 'showLinkPanel',
  287. 'click .new-link-panel .cancel' : 'hideLinkPanel',
  288. 'click .new-link-panel .save': 'createResLink'
  289. },
  290. showBookPanel: function(){
  291. this.resetBook();
  292. $(".type-selector").hide();
  293. $(".new-book-panel").slideDown();
  294. },
  295. hideBookPanel: function(){
  296. $(".new-book-panel").slideUp();
  297. $(".type-selector").show();
  298. },
  299. createResBook: function(){
  300. var bRequired = this.$('.new-book-panel #book-required').prop('checked');
  301. var title = this.$('.new-book-panel #title').val();
  302. var author = this.$('.new-book-panel #author').val();
  303. var desc = this.$('.new-book-panel #textContent').val();
  304. if(title){
  305. post_list_app.book_collection.create({
  306. school: SCHOOL,
  307. course: COURSE,
  308. creator: PARTICIPANT,
  309. isdeleted : false,
  310. isRequired: bRequired,
  311. book_title: title,
  312. book_author: author,
  313. description: desc
  314. },{ wait: true});
  315. this.hideBookPanel();
  316. }
  317. },
  318. resetBook: function(){
  319. this.$('.new-book-panel .res-required #book-required').attr("checked","checked");
  320. this.$('.new-book-panel #title').val("书名");
  321. this.$('.new-book-panel #author').val("作者");
  322. this.$('.new-book-panel #textContent').val("");
  323. },
  324. showLinkPanel: function(){
  325. this.resetLink();
  326. $(".type-selector").hide();
  327. $(".new-link-panel").slideDown();
  328. },
  329. hideLinkPanel: function(){
  330. $(".new-link-panel").slideUp();
  331. $(".type-selector").show();
  332. },
  333. createResLink: function(){
  334. var bRequired = this.$('.new-link-panel #link-required').prop('checked');
  335. var url = this.$('.new-link-panel #url').val();
  336. var name = this.$('.new-link-panel #name').val();
  337. var desc = this.$('.new-link-panel #textContent').val();
  338. if(url && name){
  339. post_list_app.link_collection.create({
  340. school: SCHOOL,
  341. course: COURSE,
  342. creator: PARTICIPANT,
  343. isdeleted : false,
  344. isRequired: bRequired,
  345. link_url: url,
  346. link_name: name,
  347. description: desc
  348. },{ wait: true});
  349. this.hideLinkPanel();
  350. }
  351. },
  352. resetLink: function(){
  353. this.$('.new-link-panel .res-required #link-required').attr("checked","checked");
  354. this.$('.new-link-panel #url').val("");
  355. this.$('.new-link-panel #name').val("");
  356. this.$('.new-link-panel #textContent').val("");
  357. },
  358. render: function(){
  359. $(this.el).html(ich.ResEditor());
  360. }
  361. });
  362. $(function(){
  363. window.post_list_app = window.post_list_app || {};
  364. post_list_app.book_collection = new BookCollection();
  365. post_list_app.link_collection = new LinkCollection();
  366. //initialize the post list view
  367. post_list_app.booklist = new BookListView({
  368. el:$(".resbook-list"),
  369. collection: post_list_app.book_collection
  370. });
  371. post_list_app.linklist = new LinkListView({
  372. el:$(".reslink-list"),
  373. collection: post_list_app.link_collection
  374. });
  375. post_list_app.book_collection.maybeFetch({
  376. success: _.bind(post_list_app.booklist.render, post_list_app.booklist)
  377. });
  378. post_list_app.link_collection.maybeFetch({
  379. success: _.bind(post_list_app.linklist.render, post_list_app.linklist)
  380. });
  381. var newEditor = new ResEditorView({
  382. el:$(".add_operation")
  383. });
  384. newEditor.render();
  385. });
  386. })();