breadcrumb.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = '';
  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 += '<div class="breadcrumb-item breadcrumb-current" breadcrumb-entry="'+iEntry+'" title="'+sTitle+'">'+sIconSpec+'<span class="truncate">'+oEntry['label']+'</span></div>';
  70. }
  71. else
  72. {
  73. sBreadCrumbHtml += '<div class="breadcrumb-item"><a class="breadcrumb-link" breadcrumb-entry="'+iEntry+'" href="'+oEntry['url']+'" title="'+sTitle+'">'+sIconSpec+'<span class="truncate">'+oEntry['label']+'</span></a></div>';
  74. }
  75. }
  76. }
  77. $('#itop-breadcrumb').html(sBreadCrumbHtml);
  78. }
  79. else
  80. {
  81. // Sorry! No Web Storage support..
  82. //$('#itop-breadcrumb').html('<span style="display:none;">Session storage not available for the current browser</span>');
  83. }
  84. },
  85. // called when created, and later when changing options
  86. _refresh: function()
  87. {
  88. },
  89. // events bound via _bind are removed automatically
  90. // revert other modifications here
  91. _destroy: function()
  92. {
  93. this.element
  94. .removeClass('breadcrumb');
  95. },
  96. // _setOptions is called with a hash of all options that are changing
  97. // always refresh when changing options
  98. _setOptions: function()
  99. {
  100. this._superApply(arguments);
  101. },
  102. // _setOption is called for each individual option that is changing
  103. _setOption: function( key, value )
  104. {
  105. this._super( key, value );
  106. },
  107. _Read: function()
  108. {
  109. var sBreadCrumbStorageKey = this.options.itop_instance_id + 'breadcrumb-v1';
  110. var aBreadCrumb = [];
  111. var sBreadCrumbData = sessionStorage.getItem(sBreadCrumbStorageKey);
  112. if (sBreadCrumbData !== null)
  113. {
  114. aBreadCrumb = JSON.parse(sBreadCrumbData);
  115. }
  116. return aBreadCrumb;
  117. },
  118. _Write: function(aBreadCrumb)
  119. {
  120. var sBreadCrumbStorageKey = this.options.itop_instance_id + 'breadcrumb-v1';
  121. sBreadCrumbData = JSON.stringify(aBreadCrumb);
  122. sessionStorage.setItem(sBreadCrumbStorageKey, sBreadCrumbData);
  123. },
  124. // Refresh the latest entry (navigating to a tab)
  125. _RefreshLatestEntry: function()
  126. {
  127. aBreadCrumb = this._Read();
  128. var iDisplayableItems = aBreadCrumb.length;
  129. if (this.options.new_entry !== null) {
  130. // The current page is the last entry in the breadcrumb, let's refresh it
  131. aBreadCrumb[aBreadCrumb.length - 1].url = window.location.href;
  132. $('#itop-breadcrumb .breadcrumb-current:last-of-type a').attr('href', window.location.href);
  133. }
  134. this._Write(aBreadCrumb);
  135. }
  136. });
  137. });