Commit a70cf604 authored by Paul Brown's avatar Paul Brown

add file_admin modals, simplify existing modals

parent 27f9baff
......@@ -136,6 +136,19 @@ class FileAdmin(BaseView, ActionsMixin):
"""
# Modals
rename_modal = False
"""Setting this to true will display the rename view as a modal dialog."""
upload_modal = False
"""Setting this to true will display the upload view as a modal dialog."""
mkdir_modal = False
"""Setting this to true will display the mkdir view as a modal dialog."""
edit_modal = False
"""Setting this to true will display the edit view as a modal dialog."""
def __init__(self, base_path, base_url=None,
name=None, category=None, endpoint=None, url=None,
verify_path=True, menu_class_name=None, menu_icon_type=None, menu_icon_value=None):
......@@ -422,7 +435,7 @@ class FileAdmin(BaseView, ActionsMixin):
Additional arguments
"""
if not path:
return self.get_url(endpoint)
return self.get_url(endpoint, **kwargs)
else:
if self._on_windows:
path = path.replace('\\', '/')
......@@ -431,7 +444,7 @@ class FileAdmin(BaseView, ActionsMixin):
return self.get_url(endpoint, **kwargs)
def _get_file_url(self, path):
def _get_file_url(self, path, **kwargs):
"""
Return static file url
......@@ -443,7 +456,7 @@ class FileAdmin(BaseView, ActionsMixin):
else:
route = '.download'
return self.get_url(route, path=path)
return self.get_url(route, path=path, **kwargs)
def _normalize_path(self, path):
"""
......@@ -641,11 +654,14 @@ class FileAdmin(BaseView, ActionsMixin):
if self.validate_form(form):
try:
self._save_form_files(directory, path, form)
flash(gettext('Successfully saved file: %(name)s',
name=form.upload.data.filename))
return redirect(self._get_dir_url('.index', path))
except Exception as ex:
flash(gettext('Failed to save file: %(error)s', error=ex), 'error')
return self.render(self.upload_template, form=form)
return self.render(self.upload_template, form=form,
header_text=gettext('Upload File'))
@expose('/download/<path:path>')
def download(self, path=None):
......@@ -696,15 +712,16 @@ class FileAdmin(BaseView, ActionsMixin):
try:
os.mkdir(op.join(directory, form.name.data))
self.on_mkdir(directory, form.name.data)
flash(gettext('Successfully created directory: %(directory)s',
directory=form.name.data))
return redirect(dir_url)
except Exception as ex:
flash(gettext('Failed to create directory: %(error)s', error=ex), 'error')
else:
helpers.flash_errors(form, message='Failed to create directory: %(error)s')
return self.render(self.mkdir_template,
form=form,
dir_url=dir_url)
return self.render(self.mkdir_template, form=form, dir_url=dir_url,
header_text=gettext('Create Directory'))
@expose('/delete/', methods=('POST',))
def delete(self):
......@@ -789,8 +806,8 @@ class FileAdmin(BaseView, ActionsMixin):
os.rename(full_path, op.join(dir_base, filename))
self.on_rename(full_path, dir_base, filename)
flash(gettext('Successfully renamed "%(src)s" to "%(dst)s"',
src=op.basename(path),
dst=filename))
src=op.basename(path),
dst=filename))
except Exception as ex:
flash(gettext('Failed to rename: %(error)s', error=ex), 'error')
......
{% extends 'admin/master.html' %}
{%- if not request.args.get('modal') -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('You are editing %(path)s', path=path) }}</h3>
{% macro check_error(error) %}
{% if error %}
<span>This file cannot be edited for now.</span>
<span>{{ _gettext('This file cannot be edited for now.') }}</span>
{% else %}
{{ lib.render_form(form, dir_url) }}
{{ caller() }}
{% endif %}
{% endmacro %}
{% block body %}
{% call check_error(error) %}
{%- if request.args.get('modal') -%}
{# content added to modal-content #}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{%- else -%}
{% block header_text -%}
<h3>{{ _gettext('Editing %(path)s', path=path) }}<h3>
{%- endblock %}
{{ lib.render_form(form, dir_url) }}
{%- endif -%}
{% endcall %}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{{ self.header_text() }}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
$(this).removeData('modal');
});
</script>
{%- endif -%}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not request.args.get('modal') -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- if request.args.get('modal') -%}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{%- else -%}
<h3>{{ header_text }}</h3>
{{ lib.render_form(form, dir_url) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{{ header_text }}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
$(this).removeData('modal');
});
</script>
{%- endif -%}
{% endblock %}
......@@ -50,9 +50,15 @@
<td>
{% block list_row_actions scoped %}
{% if admin_view.can_rename and path and name != '..' %}
<a class="icon" href="{{ get_url('.rename', path=path) }}">
<i class="fa fa-pencil icon-pencil"></i>
</a>
{%- if admin_view.rename_modal -%}
{{ lib.add_modal_button(url=get_url('.rename', path=path, modal=True),
title=_gettext('Rename File'),
content='<i class="fa fa-pencil icon-pencil"></i>') }}
{% else %}
<a class="icon" href="{{ get_url('.rename', path=path) }}" title="{{ _gettext('Rename File') }}">
<i class="fa fa-pencil icon-pencil"></i>
</a>
{%- endif -%}
{% endif %}
{%- if admin_view.can_delete and path -%}
{% if is_dir %}
......@@ -86,9 +92,14 @@
{% else %}
<td>
{% if admin_view.can_download %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- if admin_view.edit_modal and admin_view.is_file_editable(path) -%}
{{ lib.add_modal_button(url=get_file_url(path, modal=True)|safe,
btn_class='', content=name) }}
{% else %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- endif -%}
{% else %}
{{ name }}
{{ name }}
{% endif %}
</td>
<td>
......@@ -104,12 +115,24 @@
<div class="btn-toolbar">
{% if admin_view.can_upload %}
<div class="btn-group">
<a class="btn btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- if admin_view.upload_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.upload', path=dir_path, modal=True),
btn_class="btn btn-large",
content=_gettext('Upload File')) }}
{% else %}
<a class="btn btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if admin_view.can_mkdir %}
<div class="btn-group">
<a class="btn btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- if admin_view.mkdir_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.mkdir', path=dir_path, modal=True),
btn_class="btn btn-large",
content=_gettext('Create Directory')) }}
{% else %}
<a class="btn btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if actions %}
......@@ -119,14 +142,20 @@
{% endif %}
</div>
{% endblock %}
{% block actions %}
{{ actionslib.form(actions, get_url('.action_view')) }}
{% endblock %}
{%- if admin_view.rename_modal or admin_view.mkdir_modal
or admin_view.upload_modal or admin_view.edit_modal -%}
{{ lib.add_modal_window() }}
{%- endif -%}
{% endblock %}
{% block tail %}
{{ super() }}
{{ actionslib.script(_gettext('Please select at least one file.'),
actions,
actions_confirmation) }}
actions,
actions_confirmation) }}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not request.args.get('modal') -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('Please provide new name for %(name)s', name=name) }}</h3>
{%- if request.args.get('modal') -%}
{# content added to modal-content #}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{%- else -%}
{% block header_text -%}
{{ _gettext('Rename %(name)s', name=name) }}
{%- endblock %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{{ self.header_text() }}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
$(this).removeData('modal');
});
</script>
{%- endif -%}
{% endblock %}
......@@ -113,8 +113,8 @@
</div>
{% endmacro %}
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window') %}
<a class="icon" href="#" data-toggle="modal" title="{{ title }}" data-target="#{{ modal_window_id }}" data-remote="{{ url }}">
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window', btn_class='icon') %}
<a class="{{ btn_class }}" href="#" data-toggle="modal" title="{{ title }}" data-target="#{{ modal_window_id }}" data-remote="{{ url }}">
{{ content|safe }}
</a>
{% endmacro %}
......
......@@ -17,8 +17,7 @@
{% block body %}
{%- if request.args.get('modal') -%}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
action=request.url, is_modal=True) }}
{%- else -%}
{% block navlinks %}
<ul class="nav nav-tabs">
......@@ -31,9 +30,7 @@
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts) }}
{%- endif -%}
{% endblock %}
......@@ -41,7 +38,7 @@
{%- if request.args.get('modal') -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{% block modal_header %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}');
$('.modal-header h3').html('{% block header_text %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
......
......@@ -18,12 +18,23 @@
{%- if request.args.get('modal') -%}
{# remove save and continue button for modal (it won't function properly) #}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
action=request.url, is_modal=True) }}
{%- else -%}
{{ lib.render_form(form, return_url, extra(), form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
{% block navlinks %}
<ul class="nav nav-tabs">
<li>
<a href="{{ return_url }}">{{ _gettext('List') }}</a>
</li>
<li>
<a href="{{ get_url('.create_view', url=return_url) }}">{{ _gettext('Create') }}</a>
</li>
<li class="active">
<a href="javascript:void(0)">{{ _gettext('Edit') }}</a>
</li>
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts) }}
{%- endif -%}
{% endblock %}
......@@ -31,7 +42,9 @@
{%- if request.args.get('modal') -%}
<script>
// fill the header of modal dynamically
$('.modal-header h3').html('{% block modal_header %}<h3>{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}</h3>{% endblock %}');
$('.modal-header h3').html('{% block header_text -%}
{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}
{%- endblock %}');
// fixes "remote modal shows same content every time"
$('.modal').on('hidden', function() {
......@@ -47,4 +60,4 @@
{{ super() }}
{{ lib.form_js() }}
{%- endif -%}
{% endblock %}
\ No newline at end of file
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not request.args.get('modal') -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('You are editing %(path)s', path=path) }}</h3>
{% macro check_error(error) %}
{% if error %}
<span>This file cannot be edited for now.</span>
<span>{{ _gettext('This file cannot be edited for now.') }}</span>
{% else %}
{{ lib.render_form(form, dir_url) }}
{{ caller() }}
{% endif %}
{% endmacro %}
{% block body %}
{%- if request.args.get('modal') -%}
{# content added to modal-content #}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{% block header_text %}
<h3>{{ _gettext('Editing %(path)s', path=path) }}</h3>
{% endblock %}
</div>
<div class="modal-body">
{% call check_error(error) %}
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
{% endcall %}
</div>
{%- else -%}
{{ self.header_text() }}
{% call check_error(error) %}
{{ lib.render_form(form, dir_url) }}
{% endcall %}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
<script>
// fixes "remote modal shows same content every time", avoiding the flicker
$('body').on('hidden.bs.modal', '.modal', function() {
$(this).removeData('bs.modal').find(".modal-content").empty();
});
</script>
{%- endif -%}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not request.args.get('modal') -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- if request.args.get('modal') -%}
{# content added to modal-content #}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h3>{{ header_text }}</h3>
</div>
<div class="modal-body">
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
</div>
{%- else -%}
<h3>{{ header_text }}</h3>
{{ lib.render_form(form, dir_url) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
<script>
// fixes "remote modal shows same content every time", avoiding the flicker
$('body').on('hidden.bs.modal', '.modal', function() {
$(this).removeData('bs.modal').find(".modal-content").empty();
});
</script>
{%- endif -%}
{% endblock %}
......@@ -50,9 +50,15 @@
<td>
{% block list_row_actions scoped %}
{% if admin_view.can_rename and path and name != '..' %}
<a class="icon" href="{{ get_url('.rename', path=path) }}">
<i class="fa fa-pencil glyphicon glyphicon-pencil"></i>
</a>
{%- if admin_view.rename_modal -%}
{{ lib.add_modal_button(url=get_url('.rename', path=path, modal=True),
title=_gettext('Rename File'),
content='<i class="fa fa-pencil glyphicon glyphicon-pencil"></i>') }}
{% else %}
<a class="icon" href="{{ get_url('.rename', path=path) }}" title="{{ _gettext('Rename File') }}">
<i class="fa fa-pencil glyphicon glyphicon-pencil"></i>
</a>
{%- endif -%}
{% endif %}
{%- if admin_view.can_delete and path -%}
{% if is_dir %}
......@@ -86,9 +92,14 @@
{% else %}
<td>
{% if admin_view.can_download %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- if admin_view.edit_modal and admin_view.is_file_editable(path) -%}
{{ lib.add_modal_button(url=get_file_url(path, modal=True)|safe,
btn_class='', content=name) }}
{% else %}
<a href="{{ get_file_url(path)|safe }}">{{ name }}</a>
{%- endif -%}
{% else %}
{{ name }}
{{ name }}
{% endif %}
</td>
<td>
......@@ -104,12 +115,24 @@
<div class="btn-toolbar">
{% if admin_view.can_upload %}
<div class="btn-group">
<a class="btn btn-default btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- if admin_view.upload_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.upload', path=dir_path, modal=True),
btn_class="btn btn-default btn-large",
content=_gettext('Upload File')) }}
{% else %}
<a class="btn btn-default btn-large" href="{{ get_dir_url('.upload', path=dir_path) }}">{{ _gettext('Upload File') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if admin_view.can_mkdir %}
<div class="btn-group">
<a class="btn btn-default btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- if admin_view.mkdir_modal -%}
{{ lib.add_modal_button(url=get_dir_url('.mkdir', path=dir_path, modal=True),
btn_class="btn btn-default btn-large",
content=_gettext('Create Directory')) }}
{% else %}
<a class="btn btn-default btn-large" href="{{ get_dir_url('.mkdir', path=dir_path) }}">{{ _gettext('Create Directory') }}</a>
{%- endif -%}
</div>
{% endif %}
{% if actions %}
......@@ -119,14 +142,20 @@
{% endif %}
</div>
{% endblock %}
{% block actions %}
{{ actionslib.form(actions, get_url('.action_view')) }}
{% endblock %}
{%- if admin_view.rename_modal or admin_view.mkdir_modal
or admin_view.upload_modal or admin_view.edit_modal -%}
{{ lib.add_modal_window() }}
{%- endif -%}
{% endblock %}
{% block tail %}
{{ super() }}
{{ actionslib.script(_gettext('Please select at least one file.'),
actions,
actions_confirmation) }}
actions,
actions_confirmation) }}
{% endblock %}
{% extends 'admin/master.html' %}
{%- if not request.args.get('modal') -%}
{% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
<h3>{{ _gettext('Please provide new name for %(name)s', name=name) }}</h3>
{{ lib.render_form(form, dir_url) }}
{% endblock %}
\ No newline at end of file
{%- if request.args.get('modal') -%}
{# content added to modal-content #}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{% block header_text %}
<h3>{{ _gettext('Rename %(name)s', name=name) }}</h3>
{% endblock %}
</div>
<div class="modal-body">
{{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
</div>
{%- else -%}
<h3>{{ self.header_text() }}</h3>
{{ lib.render_form(form, dir_url) }}
{%- endif -%}
{% endblock %}
{% block tail %}
{%- if request.args.get('modal') -%}
<script>
// fixes "remote modal shows same content every time", avoiding the flicker
$('body').on('hidden.bs.modal', '.modal', function() {
$(this).removeData('bs.modal').find(".modal-content").empty();
});
</script>
{%- endif -%}
{% endblock %}
......@@ -108,8 +108,8 @@
</div>
{% endmacro %}
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window') %}
<a class="icon" data-target="#{{ modal_window_id }}" title="{{ title }}" href="{{ url }}" data-toggle="modal">
{% macro add_modal_button(url='', title='', content='', modal_window_id='fa_modal_window', btn_class='icon') %}
<a class="{{ btn_class }}" data-target="#{{ modal_window_id }}" title="{{ title }}" href="{{ url }}" data-toggle="modal">
{{ content|safe }}
</a>
{% endmacro %}
......
......@@ -18,13 +18,12 @@
{%- if request.args.get('modal') -%}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{% block modal_header %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}
{% block header_text %}<h3>{{ _gettext('Create New Record') }}</h3>{% endblock %}
</div>
<div class="modal-body">
{# remove save and continue button for modal (it won't function properly) #}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts,
action=request.url, is_modal=True) }}
</div>
{%- else -%}
{% block navlinks %}
......@@ -38,9 +37,7 @@
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts,
action=url_for('.create_view', url=return_url),
is_modal=request.args.get('modal')) }}
{{ lib.render_form(form, return_url, extra(), form_opts=form_opts) }}
{%- endif -%}
{% endblock %}
......
......@@ -19,18 +19,31 @@
{# content added to modal-content #}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{% block modal_header %}<h3>{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}</h3>{% endblock %}
{% block header_text %}
<h3>{{ _gettext('Edit Record') + ' #' + request.args.get('id') }}</h3>
{% endblock %}
</div>
<div class="modal-body">
{# remove save and continue button for modal (it won't function properly) #}
{{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
action=request.url, is_modal=True) }}
</div>
{%- else -%}
{{ lib.render_form(form, return_url, extra(), form_opts,
action=url_for('.edit_view', id=request.args.get('id'), url=return_url),
is_modal=request.args.get('modal')) }}
{% block navlinks %}
<ul class="nav nav-tabs">
<li>
<a href="{{ return_url }}">{{ _gettext('List') }}</a>
</li>
<li>
<a href="{{ get_url('.create_view', url=return_url) }}">{{ _gettext('Create') }}</a>
</li>
<li class="active">
<a href="javascript:void(0)">{{ _gettext('Edit') }}</a>
</li>
</ul>
{% endblock %}
{{ lib.render_form(form, return_url, extra(), form_opts) }}
{%- endif -%}
{% endblock %}
......
......@@ -168,7 +168,9 @@
{% endblock %}
{% endblock %}
{% block actions %}
{{ actionlib.form(actions, get_url('.action_view')) }}
{% endblock %}
{%- if admin_view.edit_modal or admin_view.create_modal -%}
{{ lib.add_modal_window() }}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment