-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from jaap3/modernize-js
Modernize js
- Loading branch information
Showing
15 changed files
with
171 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,34 @@ | ||
(function ($) { | ||
(function (d) { | ||
var default_settings = {{ default_settings }}; | ||
|
||
function initField($e) { | ||
if ($e.parents('.empty-form').length == 0) { // Don't do empty inlines | ||
var id = $e.attr('id'); | ||
var custom_settings = {}; | ||
if ($e.attr('data-field-settings')) { | ||
custom_settings = $.parseJSON($e.attr('data-field-settings')); | ||
} | ||
var settings = $.extend({}, default_settings, custom_settings); | ||
{% include init_template %} | ||
function initField(field) { | ||
if (field.closest('.empty-form')) return; // Don't do empty inlines | ||
var id = field.id; | ||
var custom_settings = {}; | ||
if (field.dataset.fieldSettings) { | ||
custom_settings = JSON.parse(field.dataset.fieldSettings); | ||
} | ||
var settings = Object.assign({}, default_settings, custom_settings); | ||
{% include init_template %} | ||
} | ||
|
||
$(function () { | ||
// initialize the editors on load | ||
$('textarea.djrichtextfield').each(function () { | ||
initField($(this)); | ||
}); | ||
function initFields(parent) { | ||
var richTextFields = parent.querySelectorAll('textarea.djrichtextfield'); | ||
for (var i = 0; i < richTextFields.length; i++) { | ||
initField(richTextFields[i]) | ||
} | ||
} | ||
|
||
addEventListener('DOMContentLoaded', function () { | ||
initFields(d); | ||
|
||
// initialize the editor after adding an inline | ||
// XXX: We don't use jQuery's click event as it won't work in Django 1.4 | ||
document.body.addEventListener("click", function(ev) { | ||
if(!ev.target.parentNode || ev.target.parentNode.className.indexOf("add-row") === -1) { | ||
return; | ||
} | ||
var $addRow = $(ev.target.parentNode); | ||
setTimeout(function() { // We have to wait until the inline is added | ||
$('textarea.djrichtextfield', $addRow.parent()).each(function () { | ||
initField($(this)); | ||
}); | ||
d.body.addEventListener('click', function(evt) { | ||
// initialize the editor after adding an inline | ||
var addRow = evt.target.closest('.add-row'); | ||
if(!addRow) return; | ||
setTimeout(function() { | ||
initFields(addRow.parentNode); | ||
}, 0); | ||
}, true); | ||
}); | ||
}(django.jQuery)); | ||
})(document); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from django.conf.urls import url | ||
from django.urls import path | ||
|
||
from djrichtextfield.views import InitView | ||
|
||
urlpatterns = [ | ||
url('^init.js$', InitView.as_view(), name='djrichtextfield_init') | ||
path('init.js', InitView.as_view(), name='djrichtextfield_init') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Comment on {{ post.title }}</title> | ||
{{ form.media }} | ||
</head> | ||
<body> | ||
<article> | ||
<h1>Comment on {{ post.title }}</h1> | ||
|
||
<form method="post"> | ||
<div>{% csrf_token %}</div> | ||
{{ form.as_p }} | ||
|
||
<p><input type="submit" value="Comment"></p> | ||
</form> | ||
</article> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>{{ post.title }}</title> | ||
</head> | ||
<body> | ||
<article> | ||
<h1>{{ post.title }}</h1> | ||
|
||
<div>{{ post.lead|safe }}</div> | ||
|
||
<hr> | ||
|
||
<div>{{ post.content|safe }}</div> | ||
</article> | ||
|
||
<aside> | ||
<h2>Comments</h2> | ||
|
||
<ul> | ||
{% for comment in post.comment_set.all %} | ||
<li id="c{{ comment.pk }}"> | ||
{{ comment.content|safe }} | ||
</li> | ||
{% empty %} | ||
<li> | ||
<p>No comments yet</p> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
<p><a href="{{ post.get_add_comment_url }}">Add a comment</a></p> | ||
</aside> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from django.views.generic import UpdateView, DetailView | ||
|
||
from .forms import CommentForm | ||
from .models import Comment, Post | ||
|
||
|
||
class PostDetail(DetailView): | ||
model = Post | ||
|
||
|
||
class CommentCreateView(UpdateView): | ||
model = Comment | ||
form_class = CommentForm | ||
|
||
def get_object(self): | ||
post = get_object_or_404(Post, pk=self.kwargs['pk']) | ||
return Comment(post=post) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
from django.conf.urls import include, url | ||
from django.urls import include, path | ||
from django.contrib import admin | ||
|
||
from .testapp.views import CommentCreateView, PostDetail | ||
|
||
|
||
admin.autodiscover() | ||
|
||
urlpatterns = [ | ||
url(r'^admin/', admin.site.urls), | ||
url(r'^djrichtextfield/', include('djrichtextfield.urls')) | ||
path('admin/', admin.site.urls), | ||
path('djrichtextfield/', include('djrichtextfield.urls')), | ||
path('post/<int:pk>/', PostDetail.as_view(), name='post_detail'), | ||
path('post/<int:pk>/comment/', CommentCreateView.as_view(), name='post_add_comment') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters