/wp-content/themes/freelanceengine/assets/js/my-project.js

https://bitbucket.org/digitalklones/wordpress · JavaScript · 523 lines · 489 code · 26 blank · 8 comment · 46 complexity · e26590ef65ed85f21d45109d864fa30c MD5 · raw file

  1. (function ($, Views, Models, Collections) {
  2. //Freelancer Current tab
  3. var FreelancerCurrentProjectTab = $('#current-project-tab');
  4. var $freelancer_current = $('.freelancer-current-project-tab');
  5. Models.FreelancerCurrentProject = Backbone.Model.extend();
  6. Collections.FreelancerCurrentProjects = Backbone.Collection.extend({
  7. model: Models.FreelancerCurrentProject,
  8. action: 'ae-fetch-bid',
  9. initialize: function () {
  10. this.paged = 1;
  11. }
  12. });
  13. FreelancerCurrentProjectItem = Views.PostItem.extend({
  14. tagName: 'div',
  15. className: 'fre-table-row',
  16. template: _.template($('#freelancer_current_project_template_js').html()),
  17. events: {
  18. 'click .bid-action': 'bidActionModal'
  19. },
  20. bidActionModal: function (event) {
  21. var view = this;
  22. event.preventDefault();
  23. var $target = event.target;
  24. if ($target.getAttribute('data-action') == 'remove') {
  25. if (typeof view.modal_remove_bid == 'undefined') {
  26. view.modal_remove_bid = new Views.Modal_Remove_Bid();
  27. }
  28. view.modal_remove_bid.$('#bid-id').val($target.getAttribute('data-bid-id'));
  29. view.modal_remove_bid.openModal();
  30. } else if ($target.getAttribute('data-action') == 'cancel') {
  31. if (typeof view.modal_cancel_bid == 'undefined') {
  32. view.modal_cancel_bid = new Views.Modal_Cancel_Bid();
  33. }
  34. view.modal_cancel_bid.$('#bid-id').val($target.getAttribute('data-bid-id'));
  35. view.modal_cancel_bid.openModal();
  36. }
  37. }
  38. });
  39. ListFreelancerCurrentProject = Views.ListPost.extend({
  40. tagName: 'div',
  41. itemView: FreelancerCurrentProjectItem,
  42. itemClass: 'fre-table-row'
  43. });
  44. var FreelancerCurrentProjectCollection = new Collections.FreelancerCurrentProjects();
  45. if ($('#current_project_post_data').length > 0) {
  46. var data = JSON.parse($('#current_project_post_data').html());
  47. FreelancerCurrentProjectCollection = new Collections.FreelancerCurrentProjects(data);
  48. }
  49. new ListFreelancerCurrentProject({
  50. itemView: FreelancerCurrentProjectItem,
  51. collection: FreelancerCurrentProjectCollection,
  52. el: FreelancerCurrentProjectTab.find('.fre-current-table-rows')
  53. });
  54. new Views.BlockControl({
  55. el: $freelancer_current,
  56. collection: FreelancerCurrentProjectCollection,
  57. onBeforeFetch: function () {
  58. var view = this;
  59. view.blockUi.unblock();
  60. view.blockUi.block(view.$el);
  61. if ($freelancer_current.find('.project-no-results').length > 0) {
  62. $freelancer_current.find('.project-no-results').remove();
  63. }
  64. },
  65. onAfterFetch: function (result, res) {
  66. if (!res.success || result.length == 0) {
  67. $freelancer_current.find('.fre-table').after(ae_globals.text_message.no_project);
  68. }
  69. },
  70. onAfterInit: function () {
  71. var view = this;
  72. this.$('.clear-filter').on('click', function (event) {
  73. view.clearFilter(event);
  74. });
  75. },
  76. clearFilter: function (event) {
  77. event.preventDefault();
  78. var view = this,
  79. $target = $(event.currentTarget);
  80. $(view.$el.selector).find('form')[0].reset();
  81. $(view.$el.selector).find('form select').trigger('chosen:updated');
  82. view.query['s'] = '';
  83. view.query['bid_current_status'] = '';
  84. view.fetch($target);
  85. }
  86. });
  87. //Freelancer Previous tab
  88. var FreelancerPreviousProjectTab = $('#previous-project-tab');
  89. var $freelancer_previous = $('.freelancer-previous-project-tab');
  90. Models.FreelancerPreviousProject = Backbone.Model.extend();
  91. Collections.FreelancerPreviousProjects = Backbone.Collection.extend({
  92. model: Models.FreelancerPreviousProject,
  93. action: 'ae-fetch-bid',
  94. initialize: function () {
  95. this.paged = 1;
  96. }
  97. });
  98. FreelancerPreviousProjectItem = Views.PostItem.extend({
  99. tagName: 'div',
  100. className: 'fre-table-row',
  101. template: _.template($('#freelancer_previous_project_template_js').html()),
  102. onItemRendered: function () {
  103. var view = this;
  104. view.$('.rate-it').raty({
  105. readOnly: true,
  106. half: true,
  107. score: function () {
  108. return view.model.get('rating_score');
  109. },
  110. hints: raty.hint
  111. });
  112. }
  113. });
  114. ListFreelancerPreviousProject = Views.ListPost.extend({
  115. tagName: 'div',
  116. itemView: FreelancerPreviousProjectItem,
  117. itemClass: 'fre-table-row'
  118. });
  119. var FreelancerPreviousProjectCollection = new Collections.FreelancerPreviousProjects();
  120. if ($('#previous_project_post_data').length > 0) {
  121. var data = JSON.parse($('#previous_project_post_data').html());
  122. FreelancerPreviousProjectCollection = new Collections.FreelancerPreviousProjects(data);
  123. }
  124. new ListFreelancerPreviousProject({
  125. itemView: FreelancerPreviousProjectItem,
  126. collection: FreelancerPreviousProjectCollection,
  127. el: FreelancerPreviousProjectTab.find('.fre-previous-table-rows')
  128. });
  129. new Views.BlockControl({
  130. el: $freelancer_previous,
  131. collection: FreelancerPreviousProjectCollection,
  132. onBeforeFetch: function () {
  133. var view = this;
  134. view.blockUi.unblock();
  135. view.blockUi.block(view.$el);
  136. if ($freelancer_previous.find('.project-no-results').length > 0) {
  137. $freelancer_previous.find('.project-no-results').remove();
  138. }
  139. },
  140. onAfterFetch: function (result, res) {
  141. if (!res.success || result.length == 0) {
  142. $freelancer_previous.find('.fre-table').after(ae_globals.text_message.no_project);
  143. }
  144. },
  145. onAfterInit: function () {
  146. var view = this;
  147. this.$('.clear-filter').on('click', function (event) {
  148. view.clearFilter(event);
  149. });
  150. },
  151. clearFilter: function (event) {
  152. event.preventDefault();
  153. var view = this,
  154. $target = $(event.currentTarget);
  155. $(view.$el.selector).find('form')[0].reset();
  156. $(view.$el.selector).find('form select').trigger('chosen:updated');
  157. view.query['s'] = '';
  158. view.query['bid_previous_status'] = '';
  159. view.fetch($target);
  160. }
  161. });
  162. //Employer Current tab
  163. var EmployerCurrentProjectTab = $('#current-project-tab');
  164. var $employer_current = $('.employer-current-project-tab');
  165. Models.EmployerCurrentProject = Backbone.Model.extend();
  166. Collections.EmployerCurrentProjects = Backbone.Collection.extend({
  167. model: Models.EmployerCurrentProject,
  168. action: 'ae-fetch-projects',
  169. initialize: function () {
  170. this.paged = 1;
  171. }
  172. });
  173. EmployerCurrentProjectItem = Views.PostItem.extend({
  174. tagName: 'div',
  175. className: 'fre-table-row',
  176. template: _.template($('#employer_current_project_template_js').html()),
  177. events: {
  178. 'click .project-action': 'projectActionModal',
  179. },
  180. projectActionModal: function (event) {
  181. event.preventDefault();
  182. var $target = event.target;
  183. var view = this;
  184. if ($target.getAttribute('data-action') == 'delete') {
  185. if (typeof view.modal_delete_project == 'undefined') {
  186. view.modal_delete_project = new Views.Modal_Delete_Project();
  187. }
  188. view.modal_delete_project.$('#project-id').val($target.getAttribute('data-project-id'));
  189. view.modal_delete_project.openModal();
  190. } else if ($target.getAttribute('data-action') == 'archive') {
  191. if (typeof view.modal_project == 'undefined') {
  192. view.modal_project = new Views.Modal_Archive_Project();
  193. }
  194. view.modal_project.$('#project-id').val($target.getAttribute('data-project-id'));
  195. view.modal_project.openModal();
  196. }
  197. }
  198. });
  199. ListEmployerCurrentProject = Views.ListPost.extend({
  200. tagName: 'div',
  201. itemView: EmployerCurrentProjectItem,
  202. itemClass: 'fre-table-row'
  203. });
  204. var EmployerCurrentProjectCollection = new Collections.EmployerCurrentProjects();
  205. if ($('#current_project_post_data').length > 0) {
  206. var data = JSON.parse($('#current_project_post_data').html());
  207. EmployerCurrentProjectCollection = new Collections.EmployerCurrentProjects(data);
  208. }
  209. new ListEmployerCurrentProject({
  210. itemView: EmployerCurrentProjectItem,
  211. collection: EmployerCurrentProjectCollection,
  212. el: EmployerCurrentProjectTab.find('.fre-current-table-rows')
  213. });
  214. new Views.BlockControl({
  215. el: $employer_current,
  216. collection: EmployerCurrentProjectCollection,
  217. onBeforeFetch: function () {
  218. var view = this;
  219. view.blockUi.unblock();
  220. view.blockUi.block(view.$el);
  221. if ($employer_current.find('.project-no-results').length > 0) {
  222. $employer_current.find('.project-no-results').remove();
  223. }
  224. },
  225. onAfterFetch: function (result, res) {
  226. if (!res.success || result.length == 0) {
  227. $employer_current.find('.fre-table').after(ae_globals.text_message.no_project);
  228. }
  229. },
  230. onAfterInit: function () {
  231. var view = this;
  232. this.$('.clear-filter').on('click', function (event) {
  233. view.clearFilter(event);
  234. });
  235. },
  236. clearFilter: function (event) {
  237. event.preventDefault();
  238. var view = this,
  239. $target = $(event.currentTarget);
  240. $(view.$el.selector).find('form')[0].reset();
  241. $(view.$el.selector).find('form select').trigger('chosen:updated');
  242. view.query['project_current_status'] = '';
  243. view.query['s'] = '';
  244. view.fetch($target);
  245. }
  246. });
  247. //Employer Previous tab
  248. var EmployerPreviousProjectTab = $('#previous-project-tab');
  249. var $employer_previous = $('.employer-previous-project-tab');
  250. Models.EmployerPreviousProject = Backbone.Model.extend();
  251. Collections.EmployerPreviousProjects = Backbone.Collection.extend({
  252. model: Models.EmployerPreviousProject,
  253. action: 'ae-fetch-projects',
  254. initialize: function () {
  255. this.paged = 1;
  256. }
  257. });
  258. EmployerPreviousProjectItem = Views.PostItem.extend({
  259. tagName: 'div',
  260. className: 'fre-table-row',
  261. template: _.template($('#employer_previous_project_template_js').html()),
  262. onItemRendered: function () {
  263. var view = this;
  264. view.$('.rate-it').raty({
  265. readOnly: true,
  266. half: true,
  267. score: function () {
  268. return view.model.get('rating_score');
  269. },
  270. hints: raty.hint
  271. });
  272. }
  273. });
  274. ListEmployerPreviousProject = Views.ListPost.extend({
  275. tagName: 'div',
  276. itemView: EmployerPreviousProjectItem,
  277. itemClass: 'fre-table-row'
  278. });
  279. var EmployerPreviousProjectCollection = new Collections.EmployerPreviousProjects();
  280. if ($('#previous_project_post_data').length > 0) {
  281. var data = JSON.parse($('#previous_project_post_data').html());
  282. EmployerPreviousProjectCollection = new Collections.EmployerPreviousProjects(data);
  283. }
  284. new ListEmployerPreviousProject({
  285. itemView: EmployerPreviousProjectItem,
  286. collection: EmployerPreviousProjectCollection,
  287. el: EmployerPreviousProjectTab.find('.fre-previous-table-rows')
  288. });
  289. new Views.BlockControl({
  290. el: $employer_previous,
  291. collection: EmployerPreviousProjectCollection,
  292. onBeforeFetch: function () {
  293. var view = this;
  294. view.blockUi.unblock();
  295. view.blockUi.block(view.$el);
  296. if ($employer_previous.find('.project-no-results').length > 0) {
  297. $employer_previous.find('.project-no-results').remove();
  298. }
  299. },
  300. onAfterFetch: function (result, res) {
  301. if (!res.success || result.length == 0) {
  302. $employer_previous.find('.fre-table').after(ae_globals.text_message.no_project);
  303. }
  304. },
  305. onAfterInit: function () {
  306. var view = this;
  307. this.$('.clear-filter').on('click', function (event) {
  308. view.clearFilter(event);
  309. });
  310. },
  311. clearFilter: function (event) {
  312. event.preventDefault();
  313. var view = this,
  314. $target = $(event.currentTarget);
  315. $(view.$el.selector).find('form')[0].reset();
  316. $(view.$el.selector).find('form select').trigger('chosen:updated');
  317. view.query['s'] = '';
  318. view.query['project_previous_status'] = '';
  319. view.fetch($target);
  320. }
  321. });
  322. //Modal Cancel Bid
  323. Views.Modal_Cancel_Bid = AE.Views.Modal_Box.extend({
  324. el: '#modal_cancel_bid',
  325. events: {
  326. 'submit form.form-cancel-bid': 'cancelBid'
  327. },
  328. initialize: function () {
  329. AE.Views.Modal_Box.prototype.initialize.apply(this, arguments);
  330. this.blockUi = new Views.BlockUi();
  331. },
  332. cancelBid: function (event) {
  333. event.preventDefault();
  334. var view = this,
  335. $target = $(event.currentTarget),
  336. bid_id = this.$('#bid-id').val();
  337. $.ajax({
  338. url: ae_globals.ajaxURL,
  339. type: 'post',
  340. data: {
  341. ID: bid_id,
  342. action: 'ae-sync-bid',
  343. method: 'remove'
  344. },
  345. beforeSend: function () {
  346. view.blockUi.block($target);
  347. },
  348. success: function (res) {
  349. if (res.success) {
  350. $target.closest('.info-bidding').remove();
  351. AE.pubsub.trigger('ae:notification', {
  352. msg: res.msg,
  353. notice_type: 'success'
  354. });
  355. } else {
  356. AE.pubsub.trigger('ae:notification', {
  357. msg: res.msg,
  358. notice_type: 'error'
  359. });
  360. }
  361. location.reload();
  362. }
  363. });
  364. }
  365. });
  366. //Modal Cancel Bid
  367. Views.Modal_Remove_Bid = AE.Views.Modal_Box.extend({
  368. el: '#modal_remove_bid',
  369. events: {
  370. 'submit form.form-remove-bid': 'hideBid'
  371. },
  372. initialize: function () {
  373. AE.Views.Modal_Box.prototype.initialize.apply(this, arguments);
  374. this.blockUi = new Views.BlockUi();
  375. },
  376. hideBid: function (event) {
  377. event.preventDefault();
  378. var view = this,
  379. $target = $(event.currentTarget),
  380. bid_id = this.$('#bid-id').val();
  381. $.ajax({
  382. url: ae_globals.ajaxURL,
  383. type: 'post',
  384. data: {
  385. ID: bid_id,
  386. action: 'ae-bid-hide'
  387. },
  388. beforeSend: function () {
  389. view.blockUi.block($target);
  390. },
  391. success: function (res) {
  392. if (res.success) {
  393. $target.closest('.info-bidding').remove();
  394. AE.pubsub.trigger('ae:notification', {
  395. msg: res.msg,
  396. notice_type: 'success'
  397. });
  398. } else {
  399. AE.pubsub.trigger('ae:notification', {
  400. msg: res.msg,
  401. notice_type: 'error'
  402. });
  403. }
  404. location.reload();
  405. }
  406. });
  407. }
  408. });
  409. //Modal Archive Project
  410. Views.Modal_Archive_Project = AE.Views.Modal_Box.extend({
  411. el: '#modal_archive_project',
  412. events: {
  413. 'submit form.form-archive-project': 'archiveProject'
  414. },
  415. initialize: function () {
  416. AE.Views.Modal_Box.prototype.initialize.apply(this, arguments);
  417. this.blockUi = new Views.BlockUi();
  418. },
  419. archiveProject: function (event) {
  420. event.preventDefault();
  421. var view = this,
  422. $target = $(event.currentTarget),
  423. project_id = this.$('#project-id').val();
  424. $.ajax({
  425. url: ae_globals.ajaxURL,
  426. type: 'post',
  427. data: {
  428. ID: project_id,
  429. action: 'ae-project-action',
  430. method: 'archive'
  431. },
  432. beforeSend: function () {
  433. view.blockUi.block($target);
  434. },
  435. success: function (res) {
  436. if (res.success) {
  437. $target.closest('.info-bidding').remove();
  438. AE.pubsub.trigger('ae:notification', {
  439. msg: res.msg,
  440. notice_type: 'success'
  441. });
  442. } else {
  443. AE.pubsub.trigger('ae:notification', {
  444. msg: res.msg,
  445. notice_type: 'error'
  446. });
  447. }
  448. location.reload();
  449. }
  450. });
  451. }
  452. });
  453. //Modal Delete Project
  454. Views.Modal_Delete_Project = AE.Views.Modal_Box.extend({
  455. el: '#modal_delete_project',
  456. events: {
  457. 'submit form.form-delete-project': 'deleteProject'
  458. },
  459. initialize: function () {
  460. AE.Views.Modal_Box.prototype.initialize.apply(this, arguments);
  461. this.blockUi = new Views.BlockUi();
  462. },
  463. deleteProject: function (event) {
  464. event.preventDefault();
  465. var view = this,
  466. $target = $(event.currentTarget),
  467. project_id = this.$('#project-id').val();
  468. $.ajax({
  469. url: ae_globals.ajaxURL,
  470. type: 'post',
  471. data: {
  472. ID: project_id,
  473. action: 'ae-project-action',
  474. method: 'delete'
  475. },
  476. beforeSend: function () {
  477. view.blockUi.block($target);
  478. },
  479. success: function (res) {
  480. if (res.success) {
  481. $target.closest('.info-bidding').remove();
  482. AE.pubsub.trigger('ae:notification', {
  483. msg: res.msg,
  484. notice_type: 'success'
  485. });
  486. } else {
  487. AE.pubsub.trigger('ae:notification', {
  488. msg: res.msg,
  489. notice_type: 'error'
  490. });
  491. }
  492. location.reload();
  493. }
  494. });
  495. }
  496. });
  497. })(jQuery, AE.Views, AE.Models, AE.Collections);