codeeditor.html.php 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php if($extView = $this->getExtViewFile(__FILE__)){include $extView; return helper::cd();}?>
  2. <?php js::import($jsRoot . 'ace/ace.js');?>
  3. <style>
  4. .form-actions {margin-top: 0;}
  5. body.codeeditor-fullscreen .form-actions {position: fixed; bottom: 15px; left: 30px; z-index: 1105; width: 600px}
  6. .editor-wrapper {position: relative; z-index: 1;}
  7. .editor-wrapper pre {z-index: 2; margin-bottom: 0;}
  8. .editor-wrapper .actions {position: absolute; right: 0; bottom: 15px; z-index: 3;}
  9. .editor-wrapper .actions > a {opacity: .8; color: #808080; border: 1px solid #ccc; min-width: 14px; height: 16px; line-height: 16px; text-align: center; display: block; width: 16px; text-align: center;}
  10. .editor-wrapper .actions > a:hover {color: #fff; background-color: #3280fc; border-color: #3280fc}
  11. .editor-wrapper.fullscreen {position: fixed; left: 30px; top:80px; bottom: 40px; right: 30px; z-index: 10}
  12. .editor-wrapper.fullscreen .pre {height: 100%; width: 100%}
  13. .editor-wrapper.fullscreen .actions > a {background-color: #ea644a; color: #fff; border-color: #ea644a; opacity: 1}
  14. .modal-dialog.editor-fullscreen {position: absolute; bottom: 0; right: 0; top: 0 !important; left: 0; margin: 0!important; width: 100%!important; max-width: unset; height: auto!important; border-radius: 0}
  15. .modal-dialog.editor-fullscreen .editor-actions {position: fixed; bottom: 15px; left: 20px;}
  16. .modal-dialog.editor-fullscreen .editor-wrapper.fullscreen {bottom: 80px;}
  17. .modal-dialog.editor-fullscreen .editor-wrapper.fullscreen .actions {bottom: -20px;}
  18. .modal-dialog.editor-fullscreen .editor-wrapper.fullscreen .editor-resizer {display: none;}
  19. .editor-resizer {position: absolute; bottom: 0; width: 16px; right: 0; text-align: center; cursor: s-resize; z-index: 4; opacity: .8; transition: opacity .2s; border: 1px solid #ccc; line-height: 16px; height: 16px; background: #f1f1f1; color: #808080; background: rgba(255,255,255,.8);}
  20. .editor-resizer:hover {opacity: 1}
  21. </style>
  22. <script>
  23. jQuery.fn.codeeditor = function(options)
  24. {
  25. return this.each(function()
  26. {
  27. var $this = $(this);
  28. var setting = $.extend({mode: $this.data('mode') || 'html', theme: 'textmate'}, $this.data(), options);
  29. if(setting.height) $this.css('height', setting.height);
  30. $this.css('display', 'none');
  31. var id = $this.attr('id') + '-editor';
  32. $this.before('<div class="editor-wrapper"><div class="actions"><a href="javascript:;" class="btn-fullscreen"><i class="icon icon-expand-full"></i></a></div><pre id="{0}"></pre><div class="editor-resizer"><i class="icon icon-sort"></i></div></div>'.format(id));
  33. var $editor = $('#' + id).addClass('ace-editor').height($this.height()),
  34. editor = ace.edit(id);
  35. var $wrapper = $editor.closest('.editor-wrapper'),
  36. session = editor.getSession();
  37. editor.setOptions({fontSize: '15px'});
  38. editor.setValue($this.val());
  39. editor.setShowPrintMargin(false);
  40. editor.clearSelection();
  41. session.setMode("ace/mode/" + setting.mode);
  42. session.setUseWorker(false);
  43. session.on('change', function(e)
  44. {
  45. $this.val(editor.getValue());
  46. });
  47. $this.data('editor', editor).data('editorId', id);
  48. $wrapper.on('click', '.btn-fullscreen', function()
  49. {
  50. $wrapper.toggleClass('fullscreen');
  51. $wrapper.closest('.modal-dialog').toggleClass('editor-fullscreen');
  52. $('body').toggleClass('codeeditor-fullscreen');
  53. $(this).find('i').toggleClass('icon-collapse-full');
  54. if($wrapper.hasClass('fullscreen'))
  55. {
  56. $editor.data('origin-height', $editor.height()).height($wrapper.height());
  57. }
  58. else
  59. {
  60. $editor.height($editor.data('origin-height'));
  61. }
  62. editor.resize();
  63. }).on('mousedown', '.editor-resizer', function(e)
  64. {
  65. var dragStartData = {y: e.screenY, height: $editor.outerHeight()};
  66. $wrapper.data('dragStartData', dragStartData);
  67. e.preventDefault();
  68. });
  69. $(document).on('mousemove', function(e)
  70. {
  71. var dragStartData = $wrapper.data('dragStartData');
  72. if(dragStartData) {
  73. var newHeight = dragStartData.height + e.screenY - dragStartData.y - 21;
  74. $editor.height(newHeight);
  75. e.preventDefault();
  76. editor.resize();
  77. }
  78. }).on('mouseup', function()
  79. {
  80. $wrapper.data('dragStartData', null);
  81. });
  82. });
  83. };
  84. $(function()
  85. {
  86. $('.codeeditor').codeeditor();
  87. });
  88. </script>