userBookmarks.js 4.5 KB

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