breadcrumb.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //iTop Form field
  2. ;
  3. $(function()
  4. {
  5. // the widget definition, where 'itop' is the namespace,
  6. // 'breadcrumb' the widget name
  7. $.widget( 'itop.breadcrumb',
  8. {
  9. // default options
  10. options:
  11. {
  12. itop_instance_id: '',
  13. new_entry: null,
  14. max_count: 8
  15. },
  16. // the constructor
  17. _create: function()
  18. {
  19. var me = this;
  20. this.element
  21. .addClass('breadcrumb');
  22. if(typeof(Storage) !== "undefined")
  23. {
  24. $(window).bind( 'hashchange', function(e)
  25. {
  26. me._RefreshLatestEntry();
  27. });
  28. aBreadCrumb = this._Read();
  29. if (this.options.new_entry !== null) {
  30. var sUrl = this.options.new_entry.url;
  31. if (sUrl.length == 0) {
  32. sUrl = window.location.href;
  33. }
  34. // Eliminate items having the same id, before appending the new item
  35. var aBreadCrumb = $.grep(aBreadCrumb, function(item, ipos){
  36. if (item.id == me.options.new_entry.id) return false;
  37. else return true;
  38. });
  39. aBreadCrumb.push({
  40. id: this.options.new_entry.id,
  41. label: this.options.new_entry.label,
  42. description: this.options.new_entry.description,
  43. icon: this.options.new_entry.icon,
  44. url: sUrl
  45. });
  46. // Keep only the last <max_count> items
  47. aBreadCrumb = aBreadCrumb.slice(-(this.options.max_count));
  48. }
  49. this._Write(aBreadCrumb);
  50. var sBreadCrumbHtml = '<ul>';
  51. for (iEntry in aBreadCrumb)
  52. {
  53. //if (iEntry >= iDisplayableItems) break; // skip the current page
  54. var oEntry = aBreadCrumb[iEntry];
  55. if (oEntry['label'].length > 0)
  56. {
  57. var sIconSpec = '';
  58. if (oEntry['icon'].length > 0)
  59. {
  60. sIconSpec = '<span class="icon"><img src="'+oEntry['icon']+'"/></span>';
  61. }
  62. var sTitle = oEntry['description'];
  63. if (sTitle.length == 0) {
  64. sTitle = oEntry['label'];
  65. }
  66. if ((this.options.new_entry !== null) && (iEntry == aBreadCrumb.length - 1))
  67. {
  68. // Last entry is the current page
  69. sBreadCrumbHtml += '<li><div class="itop-breadcrumb-current" breadcrumb-entry="'+iEntry+'" title="'+sTitle+'">'+sIconSpec+'<span class="truncate">'+oEntry['label']+'</span></div></li>';
  70. }
  71. else
  72. {
  73. sBreadCrumbHtml += '<li><a class="itop-breadcrumb-link" breadcrumb-entry="'+iEntry+'" href="'+oEntry['url']+'" title="'+sTitle+'">'+sIconSpec+'<span class="truncate">'+oEntry['label']+'</span></a></li>';
  74. }
  75. }
  76. }
  77. sBreadCrumbHtml += '</ul>';
  78. $('#itop-breadcrumb').html(sBreadCrumbHtml);
  79. }
  80. else
  81. {
  82. // Sorry! No Web Storage support..
  83. //$('#itop-breadcrumb').html('<span style="display:none;">Session storage not available for the current browser</span>');
  84. }
  85. },
  86. // called when created, and later when changing options
  87. _refresh: function()
  88. {
  89. },
  90. // events bound via _bind are removed automatically
  91. // revert other modifications here
  92. _destroy: function()
  93. {
  94. this.element
  95. .removeClass('breadcrumb');
  96. },
  97. // _setOptions is called with a hash of all options that are changing
  98. // always refresh when changing options
  99. _setOptions: function()
  100. {
  101. this._superApply(arguments);
  102. },
  103. // _setOption is called for each individual option that is changing
  104. _setOption: function( key, value )
  105. {
  106. this._super( key, value );
  107. },
  108. _Read: function()
  109. {
  110. var sBreadCrumbStorageKey = this.options.itop_instance_id + 'breadcrumb-v1';
  111. var aBreadCrumb = [];
  112. var sBreadCrumbData = sessionStorage.getItem(sBreadCrumbStorageKey);
  113. if (sBreadCrumbData !== null)
  114. {
  115. aBreadCrumb = JSON.parse(sBreadCrumbData);
  116. }
  117. return aBreadCrumb;
  118. },
  119. _Write: function(aBreadCrumb)
  120. {
  121. var sBreadCrumbStorageKey = this.options.itop_instance_id + 'breadcrumb-v1';
  122. sBreadCrumbData = JSON.stringify(aBreadCrumb);
  123. sessionStorage.setItem(sBreadCrumbStorageKey, sBreadCrumbData);
  124. },
  125. // Refresh the latest entry (navigating to a tab)
  126. _RefreshLatestEntry: function()
  127. {
  128. aBreadCrumb = this._Read();
  129. var iDisplayableItems = aBreadCrumb.length;
  130. if (this.options.new_entry !== null) {
  131. // The current page is the last entry in the breadcrumb, let's refresh it
  132. aBreadCrumb[aBreadCrumb.length - 1].url = window.location.href;
  133. $('#itop-breadcrumb li:last-of-type a').attr('href', window.location.href);
  134. }
  135. this._Write(aBreadCrumb);
  136. }
  137. });
  138. });