PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/views/page.js

https://github.com/aduyng/travel-time-watcher-chrome-extension
JavaScript | 120 lines | 83 code | 27 blank | 10 comment | 5 complexity | 4055ea6f3be1519d75ee4d58459c256c MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause, GPL-2.0
  1. /*global _*/
  2. define(function (require) {
  3. var Super = require('./base'),
  4. Toastr = require('toastr'),
  5. Promise = require('bluebird'),
  6. Page = Super.extend({
  7. });
  8. Page.prototype.initialize = function (options) {
  9. //super(options)
  10. Super.prototype.initialize.call(this, options);
  11. this.app = options.app;
  12. this.layout = options.app.layout;
  13. this.router = options.app.router;
  14. this.toast = Toastr;
  15. this.config = window.app.config;
  16. this.params = options.params;
  17. this.toast.options = {
  18. "closeButton" : false,
  19. "debug" : false,
  20. "positionClass" : "toast-bottom-right",
  21. "onclick" : null,
  22. "showDuration" : "300",
  23. "hideDuration" : "1000",
  24. "timeOut" : "5000",
  25. "extendedTimeOut": "1000",
  26. "showEasing" : "swing",
  27. "hideEasing" : "linear",
  28. "showMethod" : "fadeIn",
  29. "hideMethod" : "fadeOut"
  30. };
  31. };
  32. Page.prototype.start = function () {
  33. this.trigger('start');
  34. return Promise.resolve();
  35. };
  36. Page.prototype.render = function () {
  37. this.ready();
  38. };
  39. Page.prototype.ready = function () {
  40. this.trigger('ready');
  41. };
  42. Page.prototype.cleanUp = function () {
  43. };
  44. Page.prototype.close = function () {
  45. this.cleanUp();
  46. this.undelegateEvents();
  47. this.$el.empty();
  48. };
  49. Page.prototype.reload = function (options) {
  50. var params = _.extend(this.options.params, {rand: new Date().getTime()}, options);
  51. var url = this.generateHash(this.options.controller, this.options.action, params);
  52. this.router.navigate(url, {trigger: true, replace: true});
  53. };
  54. Page.prototype.generateHash = function (controller, action, params) {
  55. var parts = [controller, action];
  56. var keys = _.keys(params);
  57. _.forEach(keys, function (index) {
  58. var value = params[index];
  59. if( value !== undefined ){
  60. parts.push(index);
  61. parts.push(encodeURIComponent(value));
  62. }
  63. });
  64. // console.log(controller, action, params, parts);
  65. return parts.join('/');
  66. };
  67. Page.prototype.backButtonClickHandler = function (event) {
  68. event.preventDefault();
  69. this.back();
  70. };
  71. Page.prototype.back = function () {
  72. // var backUrl = this.session.get('backUrl');
  73. // if( !backUrl ){
  74. window.history.back();
  75. // }else{
  76. // this.router.navigate(backUrl, {trigger: true});
  77. // this.session.unset('backUrl');
  78. // }
  79. };
  80. Page.prototype.setBackLink = function () {
  81. this.session.set('backUrl', window.location.hash);
  82. };
  83. Page.prototype.goTo = function (hash, options) {
  84. var url = hash;
  85. if (_.isObject(hash)) {
  86. // console.log(hash, _.omit(hash, 'controller', 'action'));
  87. url = this.generateHash(hash.controller || this.options.controller,
  88. hash.action || this.options.action, _.omit(hash, 'controller', 'action'));
  89. }
  90. this.router.navigate(url, options || {trigger: true});
  91. };
  92. return Page;
  93. });