app.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* global -Misago */
  2. /* exported Misago */
  3. (function () {
  4. 'use strict';
  5. window.Misago = function() {
  6. var ns = Object.getPrototypeOf(this);
  7. var self = this;
  8. // Services init/destroy
  9. this._initServices = function(services) {
  10. var orderedServices = new ns.OrderedList(services).order(false);
  11. orderedServices.forEach(function (item) {
  12. var factory = null;
  13. if (item.item.factory !== undefined) {
  14. factory = item.item.factory;
  15. } else {
  16. factory = item.item;
  17. }
  18. var serviceInstance = factory(self);
  19. if (serviceInstance) {
  20. self[item.key] = serviceInstance;
  21. }
  22. });
  23. };
  24. this._destroyServices = function(services) {
  25. var orderedServices = new ns.OrderedList(services).order();
  26. orderedServices.reverse();
  27. orderedServices.forEach(function (item) {
  28. if (item.destroy !== undefined) {
  29. item.destroy(self);
  30. }
  31. });
  32. };
  33. // Context data
  34. this.context = {
  35. // Empty settings
  36. SETTINGS: {}
  37. };
  38. // App init/destory
  39. this.setup = false;
  40. this.init = function(setup, context) {
  41. this.setup = {
  42. test: ns.get(setup, 'test', false),
  43. api: ns.get(setup, 'api', '/api/')
  44. };
  45. if (context) {
  46. this.context = context;
  47. }
  48. this._initServices(ns._services);
  49. };
  50. this.destroy = function() {
  51. this._destroyServices(ns._services);
  52. };
  53. };
  54. // Services
  55. var proto = window.Misago.prototype;
  56. proto._services = [];
  57. proto.addService = function(name, factory, order) {
  58. proto._services.push({
  59. key: name,
  60. item: factory,
  61. after: proto.get(order, 'after'),
  62. before: proto.get(order, 'before')
  63. });
  64. };
  65. // Exceptions
  66. proto.PermissionDenied = function(message) {
  67. this.detail = message;
  68. this.status = 403;
  69. this.toString = function() {
  70. return this.detail || 'Permission denied';
  71. };
  72. };
  73. }());