userBookmarks.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var $ = global.jQuery
  2. var UserBookmarks = function() {
  3. var priv = {}; //private api
  4. var publ = {}; //public api
  5. priv.options = {};
  6. var defaults = {
  7. url: '', //webservice url
  8. store: null, // @required
  9. debug: false
  10. };
  11. var _cont; // container holding table
  12. var _stateEdit = false;
  13. /*
  14. initialize the plugin.
  15. */
  16. priv.init = function() {
  17. _cont = $(priv.options.id);
  18. priv.options.store.subscribe(
  19. (function (global, priv) {
  20. return function renderUserBookmarks(data) {
  21. priv.setData(data.bookmarks)
  22. }
  23. })(global, priv)
  24. )
  25. _cont.sortable();
  26. _cont.on('sortupdate', priv.sort);
  27. };
  28. priv.setData = function(data) {
  29. _cont.empty();
  30. data.forEach(function(item) {
  31. if (!item) return;
  32. if ('type' in item) {
  33. var l = $('<a></a>');
  34. l.data('id', item.id);
  35. l.addClass('btn');
  36. l.addClass('btn-xs');
  37. var label = item.name, title = '';
  38. if (item.hasOwnProperty('class') && item['class'] != '') {
  39. l.addClass(item.class);
  40. } else {
  41. l.addClass('btn-default');
  42. }
  43. if (item.type == 'menu') {
  44. // l.attr('href', 'index.php?_route=ViewTableAjax&typeName=' + 'p5_default_db:' + item.name);
  45. l.attr('href', 'index.php?_route=ViewTableAjax&namespace=' + item.namespace);
  46. if ('label' in item && item.label.length > 0) {
  47. label = item.label;
  48. title = item.label + ' (' + item.name + ')';
  49. }
  50. else if ('opis' in item && item.opis.length > 0) {
  51. label = item.opis;
  52. title = item.opis + ' (' + item.name + ')';
  53. }
  54. } else if (item.type == 'url') {
  55. l.attr('href', 'index.php?FUNCTION_INIT=URL_INIT&ZASOB_ID=' + item.id);
  56. l.attr('target', '_blank');
  57. }
  58. if (label.length > 20) {
  59. var pos = label.indexOf(' - ');
  60. if (pos > 20 || pos < 5) {
  61. pos = 20;
  62. l.text(label.substring(0, pos) + ' ...');
  63. } else {
  64. l.text(label.substring(0, pos));
  65. }
  66. } else {
  67. l.text(label);
  68. }
  69. if (title == '') title = label;
  70. l.attr('title', title);
  71. l.appendTo(_cont);
  72. if (_stateEdit) {
  73. priv.addEditBtns(l);
  74. }
  75. }
  76. });
  77. if (data.length > 0) {
  78. var editBtn = $('<button class="btn btn-xs" style="float:right" title="Edit Bookmarks"><i class="glyphicon glyphicon-cog"></i></button>')
  79. editBtn.on('click', priv.edit);
  80. editBtn.prependTo(_cont);
  81. }
  82. };
  83. priv.changed = function(e) {
  84. global.p5UI__MenuStore.remoteUpdate({
  85. '_postTask': 'changeBookmark',
  86. '_zasobID': e.data.id,
  87. btnCls: e.data.cls
  88. })
  89. return false;
  90. };
  91. priv.removed = function(e) {
  92. global.p5UI__MenuStore.remoteUpdate({
  93. '_postTask': 'removeBookmark',
  94. '_zasobID': e.data.id,
  95. })
  96. return false;
  97. };
  98. priv.sort = function(e, ui) {
  99. var idsOrder = _cont.find('a').map(function(ind, n){
  100. return $(n).data('id');
  101. });
  102. global.p5UI__MenuStore.remoteUpdate({
  103. '_postTask': 'sortBookmarks',
  104. '_zasobID': 0,
  105. ids: idsOrder,
  106. })
  107. return true;
  108. };
  109. priv.addEditBtns = function(el) {
  110. var next, btn;
  111. el.wrap('<div></div>');
  112. next = $('<span><em> Change color:</em> </span>');
  113. $.each(['btn-default', 'btn-primary', 'btn-info', 'btn-success', 'btn-warning', 'btn-danger'], function(btnInd, btnClass){
  114. btn = $('<button class="btn btn-xs ' + btnClass + '"> &nbsp; </button>');
  115. btn.on('click', {id: el.data('id'), cls: btnClass}, priv.changed);
  116. btn.appendTo(next);
  117. });
  118. btn = $('<button class="btn btn-xs"> remove </button>');
  119. btn.on('click', {id: el.data('id')}, priv.removed);
  120. btn.appendTo(next);
  121. next.insertAfter(el);
  122. };
  123. priv.edit = function(e) {
  124. _stateEdit = !_stateEdit;
  125. var el;
  126. _cont.find('a').each(function(ind, n){
  127. if (priv.options.debug) console.log(n);
  128. el = $(n);
  129. if (_stateEdit) {
  130. priv.addEditBtns(el);
  131. } else {
  132. el.next().remove();
  133. el.unwrap();
  134. }
  135. });
  136. }
  137. publ.init = function(options) {
  138. if (priv.options.debug) console.log('UserBookmarks initialization...');
  139. //merge supplied options with defaults
  140. $.extend(priv.options, defaults, options);
  141. priv.init();
  142. return publ;
  143. };
  144. return publ;
  145. };
  146. $.fn.UserBookmarks = function(options) {
  147. options = options || {};
  148. return this.each(function() {
  149. options.id = this;
  150. $(this).data('UserBookmarks', new UserBookmarks().init(options));
  151. });
  152. };