codeeditor.html.php 4.3 KB

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