Snippet.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Snippet.js defines snippet base class
  3. *
  4. * Snippet is a single piece of dynamic content on the view
  5. * Snippets are used to keep view constructors simpler - and
  6. * to separate content from application logics as much as
  7. * possible
  8. *
  9. * Snippet can have a built in feature of rendering different
  10. * content with and without data. This is - again - to help
  11. * everyday tasks with Ajax-based applications
  12. *
  13. * Snippets are used i.e. in PlaceView.js
  14. *
  15. */
  16. /**
  17. * @param {String} id Id of the DOM element to attach this Snippet to. (optional)
  18. * @param {Object} params Hash of parameters for the Snippet. (optional)
  19. */
  20. var Snippet = Class.create({
  21. initialize: function(id, params) {
  22. params = params || {};
  23. this.dom = null;
  24. this.id = null;
  25. this.data = params.data || {};
  26. this.events = {};
  27. this.onData = {
  28. template : [],
  29. doUse: undefined
  30. };
  31. this.onDefault = {
  32. template : []
  33. };
  34. if(id) {
  35. this.setDom(id);
  36. }
  37. },
  38. setDom: function(id) {
  39. if(!id) {
  40. throw "ID is not set for setDom -function";
  41. return;
  42. }
  43. var tempDom = document.getElementById(id);
  44. if(tempDom != null) {
  45. this.dom = tempDom;
  46. this.id = id;
  47. } else {
  48. throw "No id: " + id + " found";
  49. return;
  50. }
  51. },
  52. updateDom: function () {
  53. this.setDom(this.id);
  54. },
  55. setData: function (data) {
  56. if (!data) {
  57. throw "No data parameter set for setData -function";
  58. return;
  59. }
  60. this.data = data;
  61. },
  62. clearData: function () {
  63. this.data = {};
  64. },
  65. output: function (source) {
  66. var output = "";
  67. for (var i = 0, l = source.template.length; i < l; i++) {
  68. var item = source.template[i];
  69. switch (typeof item) {
  70. case 'string':
  71. case 'number':
  72. output += item;
  73. break;
  74. case 'object':
  75. try {
  76. output += item.fn.apply( item.base, item.params );
  77. } catch (e) {
  78. util.log("{Snippet.prototype.output} exception when trying to handle object");
  79. util.log(item);
  80. util.log(e);
  81. }
  82. break;
  83. }
  84. }
  85. return output;
  86. },
  87. handler: function () {
  88. if (this.data &&
  89. ((typeof this.onData.doUse == 'function' && this.onData.doUse()) ||
  90. (typeof this.onData.doUse != 'function' && this.onData.doUse))) {
  91. return this.output(this.onData);
  92. }
  93. return this.output(this.onDefault);
  94. },
  95. html: function () {
  96. return this.handler();
  97. },
  98. rewrite: function () {
  99. if (this.dom != null) {
  100. this.dom.innerHTML = this.html();
  101. } else {
  102. throw "DOM element is not set, use setDom(id) first";
  103. return;
  104. }
  105. },
  106. registerEvents: function (events) {
  107. for(var event in events) {
  108. this.dom.addEventListener(event, events[event], true)
  109. }
  110. }
  111. });