plugin.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function getCollapsibleItem() {
  2. var collapsibleItem =
  3. '<div class="panel panel-default collapsible-item" id="collapbsible-1">' +
  4. ' <div class="panel-heading collapsible-item-heading" role="tab" id="headingCollapbsible">' +
  5. ' <h4 class="panel-title collapsible-item-title">' +
  6. ' <a class="collapsed collapsible-item-title-link-icon pull-right" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseCollapbsible" aria-expanded="false" aria-controls="collapseCollapbsible"><span class="glyphicon glyphicon-chevron-down">&nbsp;</span></a>' +
  7. ' <a class="collapsed collapsible-item-title-link" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseCollapbsible" aria-expanded="false" aria-controls="collapseCollapbsible"><span class="collapsible-item-title-link-text">Title Text</span></a>' +
  8. ' </h4>' +
  9. ' </div>' +
  10. ' <div id="collapseCollapbsible" class="panel-collapse collapse collapsible-item-collapse" role="tabpanel" aria-labelledby="headingCollapbsible">' +
  11. ' <div class="panel-body collapsible-item-body">Body Text</div>' +
  12. ' </div>' +
  13. '</div>';
  14. return collapsibleItem;
  15. }
  16. CKEDITOR.dtd.$editable.a = 1;
  17. CKEDITOR.plugins.add('collapsibleItem', {
  18. requires: 'widget',
  19. icons: 'collapsibleitem',
  20. hidpi: true,
  21. init: function (editor) {
  22. editor.widgets.add('collapsibleItem', {
  23. button: 'Insert Collapsible Item',
  24. template: getCollapsibleItem(),
  25. editables: {
  26. title: {
  27. selector: '.collapsible-item-title-link',
  28. allowedContent: 'span strong em u;*{color}'
  29. },
  30. content: {
  31. selector: '.collapsible-item-body',
  32. allowedContent: 'p;br;span(*)[*];ul;ol;li;strong;em;u;table(*)[*];tbody;thead;tr;td;th;hr;a;a[*];a(*)[*];img(*)[*];'
  33. }
  34. },
  35. allowedContent: 'div(!collapsible-item*,panel*,collapse)[*];h4(!collapsible-item*,panel*)[*];a(!collapsible-item*,collapsed,panel*)[*];span(!glyphicon*)[*];',
  36. requiredContent: 'div(collapsible-item);',
  37. upcast: function (element) {
  38. return element.name == 'div' && element.hasClass('collapsible-item');
  39. },
  40. init: function () {
  41. //called when widget instance is created
  42. var accordionid = "";
  43. if(editor.elementPath() != null) {
  44. var parents = editor.elementPath().elements;
  45. for (i = 0; i < parents.length; i++) {
  46. if (parents[i].hasClass('accordion-list-group')) {
  47. accordionid = parents[i].getId();
  48. break;
  49. }
  50. }
  51. }
  52. if(accordionid == ""){
  53. var parents = this.element.getParents();
  54. for(i = 0 ; i < parents.length ; i++){
  55. if(parents[i].hasClass('accordion-list-group')){
  56. accordionid = parents[i].getId();
  57. break;
  58. }
  59. }
  60. }
  61. this.setData('accordionId', accordionid);
  62. this.setData('itemId', 'Collapsible' + (new Date()).getTime());
  63. },
  64. data: function () {
  65. //called whenever the data is updated
  66. this.element.setAttribute('id', this.data.itemId);
  67. var heading = this.element.find('.collapsible-item-heading').$[0];
  68. heading.setAttribute('id', 'heading' + this.data.itemId);
  69. var itemLink = this.element.find('.collapsible-item-title-link').$[0];
  70. var itemIconLink = this.element.find('.collapsible-item-title-link-icon').$[0];
  71. var newLink = '#collapse' + this.data.itemId;
  72. itemLink.setAttribute('aria-controls', 'collapse' + this.data.itemId);
  73. itemLink.setAttribute('href', newLink);
  74. itemLink.setAttribute('data-cke-saved-href', newLink); //this must be updated as well, otherwise editor will always fallback to old value
  75. itemIconLink.setAttribute('aria-controls', 'collapse' + this.data.itemId);
  76. itemIconLink.setAttribute('href', newLink);
  77. itemIconLink.setAttribute('data-cke-saved-href', newLink);
  78. if(this.data.accordionId != "") {
  79. this.element.find('.collapsible-item-title-link').$[0].setAttribute('data-parent', "#" + this.data.accordionId);
  80. this.element.find('.collapsible-item-title-link-icon').$[0].setAttribute('data-parent', "#" + this.data.accordionId);
  81. } else {
  82. this.element.find('.collapsible-item-title-link').$[0].removeAttribute('data-parent');
  83. this.element.find('.collapsible-item-title-link-icon').$[0].removeAttribute('data-parent');
  84. }
  85. this.element.find('.collapsible-item-collapse').$[0].setAttribute('id', 'collapse' + this.data.itemId);
  86. this.element.find('.collapsible-item-collapse').$[0].setAttribute('aria-labelledby', 'heading' + this.data.itemId);
  87. },
  88. });
  89. },
  90. onLoad: function () {
  91. CKEDITOR.addCss(
  92. 'a.collapsible-item-title-link { display: block; }' +
  93. '.collapsible-item::before {font-size:10px;color:#000;content: "Bootstrap collapsible element"}' +
  94. '.collapsible-item-heading {background-color:#f4f8ef;color:#72b73a;text-decoration:none;font-size:20px;} ' +
  95. '.collapsible-item-collapse {display:block;background-color:#ddd;min-height:10px;} ' +
  96. '.collapsible-item {padding: 8px;margin: 10px;background: #eee;border-radius: 8px;border: 1px solid #ddd;box-shadow: 0 1px 1px #fff inset, 0 -1px 0px #ccc inset;}' +
  97. '.collapsible-item-title, .collapsible-item-body {box-shadow: 0 1px 1px #ddd inset;border: 1px solid #cccccc;border-radius: 5px;background: #fff;}' +
  98. '.collapsible-item-title {margin: 0 0 8px;padding: 5px 8px;}' +
  99. '.collapsible-item-body {padding: 0 8px;}' +
  100. '.collapsible-item-title-link-text {min-width:50px;display:inline-block;min-height:20px;height:100%;}' +
  101. '.collapsible-item-title-link-icon {display:inline-block;float:right;}'
  102. );
  103. }
  104. });