Move TemplateField JS code to a Media class
Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
import mayan
|
||||
|
||||
from .widgets import TemplateWidget
|
||||
|
||||
@@ -12,3 +15,8 @@ class TemplateField(forms.CharField):
|
||||
self.model = kwargs.pop('model')
|
||||
self.model_variable = kwargs.pop('model_variable')
|
||||
super(TemplateField, self).__init__(*args, **kwargs)
|
||||
self.help_text = _(
|
||||
'The template string to be evaluated. '
|
||||
'Use Django\'s default templating language '
|
||||
'(https://docs.djangoproject.com/en/%(django_version)s/ref/templates/builtins/)'
|
||||
) % {'django_version': mayan.__django_version__}
|
||||
|
||||
@@ -19,12 +19,11 @@ class DocumentTemplateSandboxForm(forms.Form):
|
||||
self.model_variable = kwargs.pop('model_variable')
|
||||
super(DocumentTemplateSandboxForm, self).__init__(*args, **kwargs)
|
||||
self.fields['template'] = TemplateField(
|
||||
help_text=_(
|
||||
'The template string to be evaluated. '
|
||||
'Use Django\'s default templating language '
|
||||
'(https://docs.djangoproject.com/en/1.11/ref/templates/builtins/)'
|
||||
), label=_('Template'), model=self.model,
|
||||
label=_('Template'), model=self.model,
|
||||
model_variable=self.model_variable, required=False
|
||||
)
|
||||
self.order_fields(field_order=('template', 'result'))
|
||||
self.fields['template'].widget.attrs['model'] = self.model
|
||||
self.fields['template'].widget.attrs[
|
||||
'data-model-variable'
|
||||
] = self.model_variable
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
jQuery(document).ready(function() {
|
||||
var fieldNameID = '#id_template_model_property';
|
||||
$(fieldNameID).change(function(event) {
|
||||
var $idModelProperty = $(this);
|
||||
var $idTemplate = $('#id_template_template');
|
||||
var templateCursorPosition = $idTemplate.prop('selectionStart');
|
||||
var templateValue = $idTemplate.val();
|
||||
var modelVariable = $idTemplate.data('model-variable');
|
||||
var propertyText = '{{ ' + modelVariable + '.' + $idModelProperty.val() + ' }}';
|
||||
|
||||
templateValue = templateValue.slice(
|
||||
0, templateCursorPosition
|
||||
) + propertyText + templateValue.slice(
|
||||
templateCursorPosition
|
||||
);
|
||||
$idTemplate.val(templateValue);
|
||||
$idTemplate.focus();
|
||||
$idTemplate.prop(
|
||||
'selectionStart', templateCursorPosition + propertyText.length
|
||||
);
|
||||
$idTemplate.prop(
|
||||
'selectionEnd', templateCursorPosition + propertyText.length
|
||||
);
|
||||
$(fieldNameID + ' option')[0].selected = true;
|
||||
});
|
||||
});
|
||||
@@ -1,28 +0,0 @@
|
||||
{% load static %}
|
||||
|
||||
{% include 'appearance/generic_form.html' %}
|
||||
|
||||
{% block javascript %}
|
||||
<script>
|
||||
jQuery(document).ready(function() {
|
||||
$('#id_template_model_property').change(function(event) {
|
||||
var $idModelProperty = $(this);
|
||||
var $idTemplate = $('#id_template_template');
|
||||
var templateCursorPosition = $idTemplate.prop('selectionStart');
|
||||
var templateValue = $idTemplate.val();
|
||||
|
||||
var propertyText = '{{ ';
|
||||
propertyText = propertyText + '{{ form.model_variable }}.';
|
||||
propertyText = propertyText + $idModelProperty.val();
|
||||
propertyText = propertyText + ' }}';
|
||||
|
||||
templateValue = templateValue.slice(0, templateCursorPosition) + propertyText + templateValue.slice(templateCursorPosition);
|
||||
$idTemplate.val(templateValue);
|
||||
$idTemplate.focus();
|
||||
$idTemplate.prop('selectionStart', templateCursorPosition + propertyText.length);
|
||||
$idTemplate.prop('selectionEnd', templateCursorPosition + propertyText.length);
|
||||
$('#id_template_model_property option')[0].selected = true;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock javascript %}
|
||||
@@ -19,7 +19,6 @@ class DocumentTemplateSandboxView(ExternalObjectMixin, FormView):
|
||||
external_object_class = Document
|
||||
external_object_permission = permission_template_sandbox
|
||||
form_class = DocumentTemplateSandboxForm
|
||||
template_name = 'templating/template_form.html'
|
||||
|
||||
def form_valid(self, form):
|
||||
path = reverse(
|
||||
|
||||
@@ -3,12 +3,16 @@ from __future__ import absolute_import, unicode_literals
|
||||
from collections import OrderedDict
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mayan.apps.common.classes import ModelProperty
|
||||
from mayan.apps.common.widgets import NamedMultiWidget
|
||||
|
||||
|
||||
class TemplateWidget(NamedMultiWidget):
|
||||
class Media:
|
||||
js = ('templating/js/template_widget.js',)
|
||||
|
||||
def __init__(self, attrs=None, **kwargs):
|
||||
widgets = OrderedDict()
|
||||
|
||||
@@ -25,7 +29,7 @@ class TemplateWidget(NamedMultiWidget):
|
||||
model=self.attrs['model']
|
||||
)
|
||||
self.widgets['model_property'].choices = (
|
||||
[('', '----')] + choices
|
||||
[('', _('<Model property choices>'))] + choices
|
||||
)
|
||||
return {
|
||||
'model_property': None, 'template': value
|
||||
|
||||
Reference in New Issue
Block a user