Skip to content

Commit

Permalink
use bootstrap for file upload, remove iframe for ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectPatatoe committed Mar 23, 2021
1 parent b21e786 commit 49511a8
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 279 deletions.
2 changes: 0 additions & 2 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ license, including:

Password Compat - (c) Anthony Ferrara <https://github.com/ircmaxell>

jQuery.AjaxFileUpload.js - (c) Jordan Feldstein <https://github.com/jfeldstein>

URLify for PHP by jbroadway <https://github.com/jbroadway> based on URLify.js - (c) Django Software Foundation and individual contributors

Twenty Fifteen & Twenty Sixteen theme - (c) WordPress.org & Automattic.com
Expand Down
19 changes: 18 additions & 1 deletion system/admin/editor/js/Markdown.Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@
var imageDefaultText = "http://";
var linkDefaultText = "http://";

//Polyfill for node.remove() from MDN
// from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('remove')) {
return;
}
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
// -------------------------------------------------------------------
// END OF YOUR CHANGES
// -------------------------------------------------------------------
Expand Down Expand Up @@ -1757,7 +1774,7 @@
// Marks up the link and adds the ref.
var linkEnteredCallback = function (link) {

background.parentNode.removeChild(background);
background.remove();

if (link !== null) {
// ( $1
Expand Down
110 changes: 56 additions & 54 deletions system/admin/editor/js/editor.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,69 @@
(function () {

var converter = new Markdown.Converter();
Markdown.Extra.init(converter);
var editor = new Markdown.Editor(converter);

var $dialog = $('#insertImageDialog').dialog({
autoOpen: false,
closeOnEscape: false,
open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
//======Image Uploader=====
var callbackFunc;
var dialogClose = function() {
$('#insertImageDialog').modal('hide');
$('#insertImageDialogURL').val('');
$('#insertImageDialogFile').val('');
};
$('#insertImageDialogInsert').click( function() {
callbackFunc( $('#insertImageDialogURL').val().length > 0 ? $('#insertImageDialogURL').val() : null );
dialogClose();
});

var $url = $('input[type=text]', $dialog);
var $file = $('input[type=file]', $dialog);

editor.hooks.set('insertImageDialog', function(callback) {

var dialogClose = function() {
$url.val('');
$file.val('');
$dialog.dialog('close');
};

$dialog.dialog({
buttons : {
"Insert" : {
text: "Insert",
id: "insert",
click: function(){
callback($url.val().length > 0 ? $url.val(): null);
dialogClose();
}
},
"Cancel" : {
text: "Cancel",
id: "cancel",
click: function(){
dialogClose();
callback(null);
}
}
}
});

var uploadComplete = function(response) {
if (response.error == '0') {
$url.val(base_path + response.path);
$("#insert").trigger('click');
} else {
alert(response.error);
$file.val('');
$('#insertImageDialogClose').click( function() {
callbackFunc(null);
dialogClose();
});
$('#insertImageDialogCancel').click( function() {
callbackFunc(null);
dialogClose();
});
$('#insertImageDialogFile').on('input', function(){
var file = $("#insertImageDialogFile").prop("files");
var formData = new FormData();
formData.append('file', file[0], file[0].name);
// Set up the request.
$.ajax({
type: "POST",
url: base_path + 'upload.php',
data: formData,
processData: false,
contentType: false,
success: function (response) {
if (response.error == '0')
{
callbackFunc(base_path + response.path);
dialogClose();
}
else
{
if (response.error !== '') alert(response.error);
else alert("An unknown error has occurred");
console.error("Bad Response");
console.error(response);
$('#insertImageDialogFile').val('');
}
},
failure: function (response) {
if (response.error !== '') alert(response.error);
else alert("An unknown error has occurred");
console.error("Unable to Upload");
console.error(response);
$('#insertImageDialogFile').val('');
}
};

$file.ajaxfileupload({
'action': base_path + 'upload.php',
'onComplete': uploadComplete,
});

$dialog.dialog('open');
});//ajax
});//oninput
editor.hooks.set('insertImageDialog', function(callback) {
$('#insertImageDialog').modal('show');
callbackFunc = callback;

return true; // tell the editor that we'll take care of getting the image url
});

//=====end image uploader=====
editor.run();

})();
165 changes: 0 additions & 165 deletions system/admin/editor/js/jquery.ajaxfileupload.js

This file was deleted.

40 changes: 26 additions & 14 deletions system/admin/views/add-content.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/Markdown.Editor.js"></script>
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/Markdown.Extra.js"></script>
<link rel="stylesheet" href="<?php echo site_url() ?>system/resources/css/jquery-ui.css">
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/jquery.ajaxfileupload.js"></script>
<script>
$( function() {
var availableTags = [
Expand Down Expand Up @@ -173,23 +172,36 @@ function extractLast( term ) {
</div>

<style>
#insertImageDialog { display:none; padding: 10px; font-size:12px;}
.wmd-prompt-background {z-index:10!important;}
#wmd-preview img {max-width:100%;}
</style>

<div id="insertImageDialog" title="<?php echo i18n('Insert_Image');?>">
<label>URL</label>
<input type="text" size="48" placeholder="<?php echo i18n('Enter_image_URL');?>" />

<hr>

<form method="post" action="" enctype="multipart/form-data">
<label><?php echo i18n('Upload');?></label>
<input type="file" name="file" id="file" />
</form>
<div class="modal fade" id="insertImageDialog" tabindex="-1" role="dialog" aria-labelledby="insertImageDialogTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="insertImageDialogTitle"><?php echo i18n('Insert_Image');?></h5>
<button type="button" class="close" id="insertImageDialogClose" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="insertImageDialogURL">URL</label>
<input type="text" class="form-control" id="insertImageDialogURL" size="48" placeholder="<?php echo i18n('Enter_image_URL');?>" />
</div>
<hr>
<div class="form-group">
<label for="insertImageDialogFile"><?php echo i18n('Upload');?></label>
<input type="file" class="form-control-file" name="file" id="insertImageDialogFile" accept="image/png,image/jpeg,image/gif" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="insertImageDialogInsert"><?php echo i18n('Insert_Image');?></button>
<button type="button" class="btn btn-secondary" id="insertImageDialogCancel" data-dismiss="modal"><?php echo i18n('Cancel');?></button>
</div>
</div>
</div>
</div>

</div>

<!-- Declare the base path. Important -->
Expand Down
Loading

0 comments on commit 49511a8

Please sign in to comment.