PageRenderTime 54ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/web xuanthuc.com php/web xuanthuc.com/wp-content/plugins/posts-to-posts/admin/box.js

https://gitlab.com/phamngsinh/baitaplon_sinhvien
JavaScript | 449 lines | 409 code | 39 blank | 1 comment | 30 complexity | 96209216abc3713b20ab0faf6d46ba9d MD5 | raw file
  1. // Generated by CoffeeScript 1.6.2
  2. (function() {
  3. var Candidate, Candidates, CandidatesView, Connection, Connections, ConnectionsView, CreatePostView, ENTER_KEY, MetaboxView, get_mustache_template, remove_row, row_wait;
  4. ENTER_KEY = 13;
  5. row_wait = function($td) {
  6. return $td.find('.p2p-icon').css('background-image', 'url(' + P2PAdminL10n.spinner + ')');
  7. };
  8. remove_row = function($td) {
  9. var $table;
  10. $table = $td.closest('table');
  11. $td.closest('tr').remove();
  12. if (!$table.find('tbody tr').length) {
  13. return $table.hide();
  14. }
  15. };
  16. get_mustache_template = function(name) {
  17. return jQuery('#p2p-template-' + name).html();
  18. };
  19. Candidate = Backbone.Model.extend({});
  20. Connection = Backbone.Model.extend({});
  21. Candidates = Backbone.Model.extend({
  22. sync: function() {
  23. var params,
  24. _this = this;
  25. params = {
  26. subaction: 'search'
  27. };
  28. return this.ajax_request(params, function(response) {
  29. var _ref;
  30. _this.total_pages = ((_ref = response.navigation) != null ? _ref['total-pages-raw'] : void 0) || 1;
  31. return _this.trigger('sync', response);
  32. });
  33. },
  34. validate: function(attrs) {
  35. var _ref;
  36. if ((0 < (_ref = attrs['paged']) && _ref <= this.total_pages)) {
  37. return null;
  38. }
  39. return 'invalid page';
  40. }
  41. });
  42. Connections = Backbone.Collection.extend({
  43. model: Connection,
  44. createItemAndConnect: function(title) {
  45. var data,
  46. _this = this;
  47. data = {
  48. subaction: 'create_post',
  49. post_title: title
  50. };
  51. return this.ajax_request(data, function(response) {
  52. return _this.trigger('create', response);
  53. });
  54. },
  55. create: function(candidate) {
  56. var data,
  57. _this = this;
  58. data = {
  59. subaction: 'connect',
  60. to: candidate.get('id')
  61. };
  62. return this.ajax_request(data, function(response) {
  63. return _this.trigger('create', response);
  64. });
  65. },
  66. "delete": function(connection) {
  67. var data,
  68. _this = this;
  69. data = {
  70. subaction: 'disconnect',
  71. p2p_id: connection.get('id')
  72. };
  73. return this.ajax_request(data, function(response) {
  74. return _this.trigger('delete', response, connection);
  75. });
  76. },
  77. clear: function() {
  78. var data,
  79. _this = this;
  80. data = {
  81. subaction: 'clear_connections'
  82. };
  83. return this.ajax_request(data, function(response) {
  84. return _this.trigger('clear', response);
  85. });
  86. }
  87. });
  88. ConnectionsView = Backbone.View.extend({
  89. events: {
  90. 'click th.p2p-col-delete .p2p-icon': 'clear',
  91. 'click td.p2p-col-delete .p2p-icon': 'delete'
  92. },
  93. initialize: function(options) {
  94. this.maybe_make_sortable();
  95. this.collection.on('create', this.afterCreate, this);
  96. return this.collection.on('clear', this.afterClear, this);
  97. },
  98. maybe_make_sortable: function() {
  99. if (this.$('th.p2p-col-order').length) {
  100. return this.$('tbody').sortable({
  101. handle: 'td.p2p-col-order',
  102. helper: function(e, ui) {
  103. ui.children().each(function() {
  104. var $this;
  105. $this = jQuery(this);
  106. return $this.width($this.width());
  107. });
  108. return ui;
  109. }
  110. });
  111. }
  112. },
  113. clear: function(ev) {
  114. var $td;
  115. ev.preventDefault();
  116. if (!confirm(P2PAdminL10n.deleteConfirmMessage)) {
  117. return;
  118. }
  119. $td = jQuery(ev.target).closest('td');
  120. row_wait($td);
  121. return this.collection.clear();
  122. },
  123. afterClear: function() {
  124. return this.$el.hide().find('tbody').html('');
  125. },
  126. "delete": function(ev) {
  127. var $td, req;
  128. ev.preventDefault();
  129. $td = jQuery(ev.target).closest('td');
  130. row_wait($td);
  131. req = this.collection["delete"](new Connection({
  132. id: $td.find('input').val()
  133. }));
  134. req.done(function() {
  135. return remove_row($td);
  136. });
  137. return null;
  138. },
  139. afterCreate: function(response) {
  140. this.$el.show().find('tbody').append(response.row);
  141. return this.collection.trigger('append', response);
  142. }
  143. });
  144. CandidatesView = Backbone.View.extend({
  145. template: Mustache.compile(get_mustache_template('tab-list')),
  146. events: {
  147. 'keypress :text': 'handleReturn',
  148. 'keyup :text': 'handleSearch',
  149. 'click .p2p-prev, .p2p-next': 'changePage',
  150. 'click td.p2p-col-create div': 'promote'
  151. },
  152. initialize: function(options) {
  153. this.spinner = options.spinner;
  154. options.connections.on('delete', this.afterCandidatesRefreshed, this);
  155. options.connections.on('clear', this.afterCandidatesRefreshed, this);
  156. this.collection.on('sync', this.afterCandidatesRefreshed, this);
  157. this.collection.on('error', this.afterInvalid, this);
  158. return this.collection.on('invalid', this.afterInvalid, this);
  159. },
  160. promote: function(ev) {
  161. var $td, req,
  162. _this = this;
  163. ev.preventDefault();
  164. $td = jQuery(ev.target).closest('td');
  165. row_wait($td);
  166. req = this.options.connections.create(new Candidate({
  167. id: $td.find('div').data('item-id')
  168. }));
  169. req.done(function() {
  170. if (_this.options.duplicate_connections) {
  171. return $td.find('.p2p-icon').css('background-image', '');
  172. } else {
  173. return remove_row($td);
  174. }
  175. });
  176. return null;
  177. },
  178. handleReturn: function(ev) {
  179. if (ev.keyCode === ENTER_KEY) {
  180. ev.preventDefault();
  181. }
  182. return null;
  183. },
  184. handleSearch: function(ev) {
  185. var $searchInput, delayed,
  186. _this = this;
  187. if (delayed !== void 0) {
  188. clearTimeout(delayed);
  189. }
  190. $searchInput = jQuery(ev.target);
  191. delayed = setTimeout(function() {
  192. var searchStr;
  193. searchStr = $searchInput.val();
  194. if (searchStr === _this.collection.get('s')) {
  195. return;
  196. }
  197. _this.spinner.insertAfter(_this.searchInput).show();
  198. return _this.collection.save({
  199. 's': searchStr,
  200. 'paged': 1
  201. });
  202. }, 400);
  203. return null;
  204. },
  205. changePage: function(ev) {
  206. var $navButton, new_page;
  207. $navButton = jQuery(ev.currentTarget);
  208. new_page = this.collection.get('paged');
  209. if ($navButton.hasClass('p2p-prev')) {
  210. new_page--;
  211. } else {
  212. new_page++;
  213. }
  214. this.spinner.appendTo(this.$('.p2p-navigation'));
  215. return this.collection.save('paged', new_page);
  216. },
  217. afterCandidatesRefreshed: function(response) {
  218. this.spinner.remove();
  219. this.$('button, .p2p-results, .p2p-navigation, .p2p-notice').remove();
  220. if ('string' !== typeof response) {
  221. response = this.template(response);
  222. }
  223. return this.$el.append(response);
  224. },
  225. afterInvalid: function() {
  226. return this.spinner.remove();
  227. }
  228. });
  229. CreatePostView = Backbone.View.extend({
  230. events: {
  231. 'click button': 'createItem',
  232. 'keypress :text': 'handleReturn'
  233. },
  234. initialize: function(options) {
  235. this.createButton = this.$('button');
  236. return this.createInput = this.$(':text');
  237. },
  238. handleReturn: function(ev) {
  239. if (ev.keyCode === ENTER_KEY) {
  240. this.createButton.click();
  241. ev.preventDefault();
  242. }
  243. return null;
  244. },
  245. createItem: function(ev) {
  246. var req, title,
  247. _this = this;
  248. ev.preventDefault();
  249. if (this.createButton.hasClass('inactive')) {
  250. return false;
  251. }
  252. title = this.createInput.val();
  253. if (title === '') {
  254. this.createInput.focus();
  255. return;
  256. }
  257. this.createButton.addClass('inactive');
  258. req = this.collection.createItemAndConnect(title);
  259. req.done(function() {
  260. _this.createInput.val('');
  261. return _this.createButton.removeClass('inactive');
  262. });
  263. return null;
  264. }
  265. });
  266. MetaboxView = Backbone.View.extend({
  267. events: {
  268. 'click .p2p-toggle-tabs': 'toggleTabs',
  269. 'click .wp-tab-bar li': 'setActiveTab'
  270. },
  271. initialize: function(options) {
  272. this.spinner = options.spinner;
  273. this.initializedCandidates = false;
  274. options.connections.on('append', this.afterConnectionAppended, this);
  275. options.connections.on('clear', this.afterConnectionDeleted, this);
  276. return options.connections.on('delete', this.afterConnectionDeleted, this);
  277. },
  278. toggleTabs: function(ev) {
  279. var $tabs;
  280. ev.preventDefault();
  281. $tabs = this.$('.p2p-create-connections-tabs');
  282. $tabs.toggle();
  283. if (!this.initializedCandidates && $tabs.is(':visible')) {
  284. this.options.candidates.sync();
  285. this.initializedCandidates = true;
  286. }
  287. return null;
  288. },
  289. setActiveTab: function(ev) {
  290. var $tab;
  291. ev.preventDefault();
  292. $tab = jQuery(ev.currentTarget);
  293. this.$('.wp-tab-bar li').removeClass('wp-tab-active');
  294. $tab.addClass('wp-tab-active');
  295. return this.$el.find('.tabs-panel').hide().end().find($tab.data('ref')).show().find(':text').focus();
  296. },
  297. afterConnectionAppended: function(response) {
  298. if ('one' === this.options.cardinality) {
  299. return this.$('.p2p-create-connections').hide();
  300. }
  301. },
  302. afterConnectionDeleted: function(response) {
  303. if ('one' === this.options.cardinality) {
  304. return this.$('.p2p-create-connections').show();
  305. }
  306. }
  307. });
  308. window.P2PAdmin = {
  309. Candidate: Candidate,
  310. Connection: Connection,
  311. boxes: {}
  312. };
  313. jQuery(function() {
  314. var clearVal, setVal;
  315. if (!jQuery('<input placeholder="1" />')[0].placeholder) {
  316. setVal = function() {
  317. var $this;
  318. $this = jQuery(this);
  319. if (!$this.val()) {
  320. $this.val($this.attr('placeholder'));
  321. $this.addClass('p2p-placeholder');
  322. }
  323. return void 0;
  324. };
  325. clearVal = function() {
  326. var $this;
  327. $this = jQuery(this);
  328. if ($this.hasClass('p2p-placeholder')) {
  329. $this.val('');
  330. $this.removeClass('p2p-placeholder');
  331. }
  332. return void 0;
  333. };
  334. jQuery('.p2p-search input[placeholder]').each(setVal).focus(clearVal).blur(setVal);
  335. }
  336. Mustache.compilePartial('table-row', get_mustache_template('table-row'));
  337. return jQuery('.p2p-box').each(function() {
  338. var $metabox, $spinner, ajax_request, candidates, candidatesView, connections, connectionsView, createPostView, ctype, metaboxView;
  339. $metabox = jQuery(this);
  340. $spinner = jQuery('<img>', {
  341. 'src': P2PAdminL10n.spinner,
  342. 'class': 'p2p-spinner'
  343. });
  344. candidates = new Candidates({
  345. 's': '',
  346. 'paged': 1
  347. });
  348. candidates.total_pages = $metabox.find('.p2p-total').data('num') || 1;
  349. ctype = {
  350. p2p_type: $metabox.data('p2p_type'),
  351. direction: $metabox.data('direction'),
  352. from: jQuery('#post_ID').val()
  353. };
  354. ajax_request = function(options, callback) {
  355. var params;
  356. params = _.extend({}, options, candidates.attributes, ctype, {
  357. action: 'p2p_box',
  358. nonce: P2PAdminL10n.nonce
  359. });
  360. return jQuery.post(ajaxurl, params, function(response) {
  361. var e;
  362. try {
  363. response = jQuery.parseJSON(response);
  364. } catch (_error) {
  365. e = _error;
  366. if (typeof console !== "undefined" && console !== null) {
  367. console.error('Malformed response', response);
  368. }
  369. return;
  370. }
  371. if (response.error) {
  372. return alert(response.error);
  373. } else {
  374. return callback(response);
  375. }
  376. });
  377. };
  378. candidates.ajax_request = ajax_request;
  379. connections = new Connections;
  380. connections.ajax_request = ajax_request;
  381. connectionsView = new ConnectionsView({
  382. el: $metabox.find('.p2p-connections'),
  383. collection: connections,
  384. candidates: candidates
  385. });
  386. candidatesView = new CandidatesView({
  387. el: $metabox.find('.p2p-tab-search'),
  388. collection: candidates,
  389. connections: connections,
  390. spinner: $spinner,
  391. duplicate_connections: $metabox.data('duplicate_connections')
  392. });
  393. createPostView = new CreatePostView({
  394. el: $metabox.find('.p2p-tab-create-post'),
  395. collection: connections
  396. });
  397. metaboxView = new MetaboxView({
  398. el: $metabox,
  399. spinner: $spinner,
  400. cardinality: $metabox.data('cardinality'),
  401. candidates: candidates,
  402. connections: connections
  403. });
  404. return P2PAdmin.boxes[ctype.p2p_type] = {
  405. candidates: candidates,
  406. connections: connections
  407. };
  408. });
  409. });
  410. }).call(this);