Browse Source

use bootstrap for file upload, remove iframe for ajax

pull/453/head
ProjectPatatoe 4 years ago
parent
commit
49511a8244
9 changed files with 181 additions and 279 deletions
  1. +0
    -2
      COPYRIGHT.txt
  2. +18
    -1
      system/admin/editor/js/Markdown.Editor.js
  3. +56
    -54
      system/admin/editor/js/editor.js
  4. +0
    -165
      system/admin/editor/js/jquery.ajaxfileupload.js
  5. +26
    -14
      system/admin/views/add-content.html.php
  6. +27
    -14
      system/admin/views/add-page.html.php
  7. +26
    -14
      system/admin/views/edit-content.html.php
  8. +27
    -14
      system/admin/views/edit-page.html.php
  9. +1
    -1
      upload.php

+ 0
- 2
COPYRIGHT.txt View File

@ -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


+ 18
- 1
system/admin/editor/js/Markdown.Editor.js View File

@ -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
// -------------------------------------------------------------------
@ -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


+ 56
- 54
system/admin/editor/js/editor.js View File

@ -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();
})();

+ 0
- 165
system/admin/editor/js/jquery.ajaxfileupload.js View File

@ -1,165 +0,0 @@
/*
// jQuery Ajax File Uploader
//
// @author: Jordan Feldstein <jfeldstein.com>
//
// - Ajaxifies an individual <input type="file">
// - Files are sandboxed. Doesn't matter how many, or where they are, on the page.
// - Allows for extra parameters to be included with the file
// - onStart callback can cancel the upload by returning false
*/
(function($) {
$.fn.ajaxfileupload = function(options) {
var settings = {
params: {},
action: '',
onStart: function() { },
onComplete: function(response) { },
onCancel: function() { },
validate_extensions : true,
valid_extensions : ['gif','png','jpg','jpeg'],
submit_button : null
};
var uploading_file = false;
if ( options ) {
$.extend( settings, options );
}
// 'this' is a jQuery collection of one or more (hopefully)
// file elements, but doesn't check for this yet
return this.each(function() {
var $element = $(this);
// Skip elements that are already setup. May replace this
// with uninit() later, to allow updating that settings
if($element.data('ajaxUploader-setup') === true) return;
$element.change(function()
{
// since a new image was selected, reset the marker
uploading_file = false;
// only update the file from here if we haven't assigned a submit button
if (settings.submit_button == null)
{
upload_file();
}
});
if (settings.submit_button == null)
{
// do nothing
} else
{
settings.submit_button.click(function(e)
{
// Prevent non-AJAXy submit
e.preventDefault();
// only attempt to upload file if we're not uploading
if (!uploading_file)
{
upload_file();
}
});
}
var upload_file = function()
{
if($element.val() == '') return settings.onCancel.apply($element, [settings.params]);
// make sure extension is valid
var ext = $element.val().split('.').pop().toLowerCase();
if(true === settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1)
{
// Pass back to the user
settings.onComplete.apply($element, [{status: false, message: 'The select file type is invalid. File must be ' + settings.valid_extensions.join(', ') + '.'}, settings.params]);
} else
{
uploading_file = true;
// Creates the form, extra inputs and iframe used to
// submit / upload the file
wrapElement($element);
// Call user-supplied (or default) onStart(), setting
// it's this context to the file DOM element
var ret = settings.onStart.apply($element, [settings.params]);
// let onStart have the option to cancel the upload
if(ret !== false)
{
$element.parent('form').submit(function(e) { e.stopPropagation(); }).submit();
}
}
};
// Mark this element as setup
$element.data('ajaxUploader-setup', true);
/*
// Internal handler that tries to parse the response
// and clean up after ourselves.
*/
var handleResponse = function(loadedFrame, element) {
var response, responseStr = $(loadedFrame).contents()[0].body.textContent;
try {
//response = $.parseJSON($.trim(responseStr));
response = JSON.parse(responseStr);
} catch(e) {
console.error(e);
response = responseStr;
}
// Tear-down the wrapper form
element.siblings().remove();
element.unwrap();
uploading_file = false;
// Pass back to the user
settings.onComplete.apply(element, [response, settings.params]);
};
/*
// Wraps element in a <form> tag, and inserts hidden inputs for each
// key:value pair in settings.params so they can be sent along with
// the upload. Then, creates an iframe that the whole thing is
// uploaded through.
*/
var wrapElement = function(element) {
// Create an iframe to submit through, using a semi-unique ID
var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000)
$('body').after('<iframe width="0" height="0" style="display:none;" name="'+frame_id+'" id="'+frame_id+'"/>');
$('#'+frame_id).get(0).onload = function() {
handleResponse(this, element);
};
// Wrap it in a form
element.wrap(function() {
return '<form action="' + settings.action + '" method="POST" enctype="multipart/form-data" target="'+frame_id+'" />'
})
// Insert <input type='hidden'>'s for each param
.before(function() {
var key, html = '';
for(key in settings.params) {
var paramVal = settings.params[key];
if (typeof paramVal === 'function') {
paramVal = paramVal();
}
html += '<input type="hidden" name="' + key + '" value="' + paramVal + '" />';
}
return html;
});
}
});
}
})( jQuery )

+ 26
- 14
system/admin/views/add-content.html.php View File

@ -29,7 +29,6 @@ if (file_exists($tagslang)) {
<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 = [
@ -173,23 +172,36 @@ $( function() {
</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 -->


+ 27
- 14
system/admin/views/add-page.html.php View File

@ -7,7 +7,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>
<?php if (isset($error)) { ?>
<div class="error-message"><?php echo $error ?></div>
@ -61,22 +60,36 @@
</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>
<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 -->
<script type="text/javascript">var base_path = '<?php echo site_url() ?>';</script>

+ 26
- 14
system/admin/views/edit-content.html.php View File

@ -74,7 +74,6 @@ if (file_exists($tagslang)) {
<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 = [
@ -234,23 +233,36 @@ $( function() {
</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 -->
<script type="text/javascript">var base_path = '<?php echo site_url() ?>';</script>


+ 27
- 14
system/admin/views/edit-page.html.php View File

@ -72,7 +72,6 @@ if ($type == 'is_frontpage') {
<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>
<?php if (isset($error)) { ?>
<div class="error-message"><?php echo $error ?></div>
@ -130,22 +129,36 @@ if ($type == 'is_frontpage') {
</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>
<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 -->
<script type="text/javascript">var base_path = '<?php echo site_url() ?>';</script>

+ 1
- 1
upload.php View File

@ -12,7 +12,7 @@ if (config('timezone')) {
date_default_timezone_set('Asia/Jakarta');
}
$whitelist = array('jpg', 'jpeg', 'png', 'gif');
$whitelist = array('jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', 'gif');
$name = null;
$dir = 'content/images/';
$error = null;


Loading…
Cancel
Save