trumbowyg.mention.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* ===========================================================
  2. * trumbowyg.mention.js v0.1
  3. * Mention plugin for Trumbowyg
  4. * http://alex-d.github.com/Trumbowyg
  5. * ===========================================================
  6. * Author : Viper
  7. * Github: https://github.com/Globulopolis
  8. * Website: http://киноархив.com
  9. */
  10. (function ($) {
  11. 'use strict';
  12. var defaultOptions = {
  13. source: '',
  14. formatDropdownItem: formatDropdownItem,
  15. formatResult: formatResult
  16. };
  17. $.extend(true, $.trumbowyg, {
  18. langs: {
  19. en: {
  20. mention: 'Mention'
  21. },
  22. da: {
  23. mention: 'Nævn'
  24. },
  25. fr: {
  26. mention: 'Mentioner'
  27. },
  28. ru: {
  29. mention: 'Упомянуть'
  30. },
  31. tr: {
  32. mention: 'Bahset'
  33. },
  34. zh_tw: {
  35. mention: '標記'
  36. },
  37. },
  38. plugins: {
  39. mention: {
  40. init: function (trumbowyg) {
  41. trumbowyg.o.plugins.mention = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.mention || {});
  42. var btnDef = {
  43. dropdown: buildDropdown(trumbowyg.o.plugins.mention.source, trumbowyg)
  44. };
  45. trumbowyg.addBtnDef('mention', btnDef);
  46. }
  47. }
  48. }
  49. });
  50. /**
  51. * Build dropdown list
  52. *
  53. * @param {Array} items Items
  54. * @param {object} trumbowyg Editor
  55. *
  56. * @return {Array}
  57. */
  58. function buildDropdown(items, trumbowyg) {
  59. var dropdown = [];
  60. // Check if source is an array
  61. if (items.constructor === Array) {
  62. $.each(items, function (i, item) {
  63. var btn = 'mention-' + i,
  64. btnDef = {
  65. hasIcon: false,
  66. text: trumbowyg.o.plugins.mention.formatDropdownItem(item),
  67. fn: function () {
  68. trumbowyg.execCmd('insertHTML', trumbowyg.o.plugins.mention.formatResult(item));
  69. return true;
  70. }
  71. };
  72. trumbowyg.addBtnDef(btn, btnDef);
  73. dropdown.push(btn);
  74. });
  75. }
  76. return dropdown;
  77. }
  78. /**
  79. * Format item in dropdown.
  80. *
  81. * @param {object} item Item object.
  82. *
  83. * @return {string}
  84. */
  85. function formatDropdownItem(item) {
  86. return item.login;
  87. }
  88. /**
  89. * Format result pasted in editor.
  90. *
  91. * @param {object} item Item object.
  92. *
  93. * @return {string}
  94. */
  95. function formatResult(item) {
  96. return '@' + item.login + ' ';
  97. }
  98. })(jQuery);