bootstrap.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. use Dcat\Admin\Admin;
  3. use Dcat\Admin\Grid;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid\Filter;
  6. use Dcat\Admin\Show;
  7. /**
  8. * Dcat-admin - admin builder based on Laravel.
  9. * @author jqh <https://github.com/jqhph>
  10. *
  11. * Bootstraper for Admin.
  12. *
  13. * Here you can remove builtin form field:
  14. *
  15. * extend custom field:
  16. * Dcat\Admin\Form::extend('php', PHPEditor::class);
  17. * Dcat\Admin\Grid\Column::extend('php', PHPEditor::class);
  18. * Dcat\Admin\Grid\Filter::extend('php', PHPEditor::class);
  19. *
  20. * Or require js and css assets:
  21. * Admin::css('/packages/prettydocs/css/styles.css');
  22. * Admin::js('/packages/prettydocs/js/main.js');
  23. *
  24. */
  25. Form\Field\Editor::resolving(function (Form\Field\Editor $editor) {
  26. $editor->options([
  27. 'file_picker_types' => 'media',
  28. 'file_picker_callback' => \Dcat\Admin\Support\JavaScript::make(<<<JS
  29. function file_picker_callback (callback, value, meta) {
  30. // 设置上传地址为原富文本框图片文件上传地址
  31. var upurl = opts.images_upload_url;
  32. var filetype = '';
  33. // 处理媒体类型文件能选择的文件类型
  34. if (meta.filetype == 'media') {
  35. filetype = '.mp4,.webm,.ogg'
  36. }
  37. //模拟出一个input用于添加本地文件
  38. var input = document.createElement('input');
  39. input.setAttribute('type', 'file');
  40. input.setAttribute('accept', filetype);
  41. // 模拟点击file input
  42. input.click();
  43. input.onchange = function() {
  44. // 文件选择后进行上传
  45. var file = this.files[0];
  46. var xhr, formData;
  47. console.log(file.name);
  48. Dcat.loading()
  49. xhr = new XMLHttpRequest();
  50. xhr.withCredentials = false;
  51. xhr.open('POST', upurl);
  52. xhr.onload = function() {
  53. var json;
  54. if (xhr.status != 200) {
  55. failure('HTTP Error: ' + xhr.status);
  56. return;
  57. }
  58. json = JSON.parse(xhr.responseText);
  59. if (!json || typeof json.location != 'string') {
  60. failure('Invalid JSON: ' + xhr.responseText);
  61. return;
  62. }
  63. json.location = json.location.replace('http://','https://')
  64. json.location = json.location.replace('-internal','')
  65. Dcat.loading(false)
  66. callback(json.location);
  67. };
  68. formData = new FormData();
  69. formData.append('file', file, file.name );
  70. xhr.send(formData);
  71. }
  72. }
  73. JS)
  74. ]);
  75. });