userBookmarks.js 4.3 KB

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