Skip to content

Commit

Permalink
A bit of a cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jordij committed Jan 22, 2015
1 parent d8122e9 commit 48a2499
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 150 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,24 @@ wagtailembedder

Snippets embedder for Wagtail richtext fields.

UNDER CONSTRUCTION
# Quickstart

``` $ pip install wagtailembedder [GITHUB SSH URI]```

add wagtailembedder to your settings.py in the INSTALLED_APPS section:

```
...
'modelcluster',
'wagtailembedder',
'core',
...
```

For each of your models registered as a wagtail.wagtailsnippets create an html file to render the template inside a RichText field.
Example: if we have a ```SocialMediaLink``` snippet in our ```core``` app we need to create a template in ```core/templates/snippets/social_media_link.html```

If no template is defined then nothing will be outputted in the frontend when rendering a RichText field, don't be a dick and write some nice templates.

Templates names will match snippets models names replacing capital letters with underscores, Wagtail style.

3 changes: 3 additions & 0 deletions wagtailembedder/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ def expand_db_attributes(attrs, for_editor):


def add_embed_handler():
"""
Add our own SnippetEmbedHandler into the wagtailcore EMBED_HANDLERS var
"""
EMBED_HANDLERS['snippet'] = SnippetEmbedHandler
return True
163 changes: 47 additions & 116 deletions wagtailembedder/static/wagtailembedder/js/hallo-embedder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,134 +9,65 @@
var button, widget;

widget = this;

getEnclosingEmbed = function() {
var node;

node = widget.options.editable.getSelection().commonAncestorContainer;
return $(node).parents('div[data-embedtype="snippet"]').get(0);
};

button = $('<span></span>');
button.hallobutton({
uuid: this.options.uuid,
editable: this.options.editable,
label: 'Snippets',
icon: 'icon-snippet',
command: null
queryState: function(event) {
var refreshedButton = button.hallobutton('checked', getEnclosingEmbed());
if ($(refreshedButton).hasClass('ui-state-active')) {
$(toolbar).find('button').not($(button).children()[0]).removeClass('ui-state-active');
}
return refreshedButton;
}
});
toolbar.append(button);
return button.on("click", function(event) {
var insertionPoint, lastSelection;

lastSelection = widget.options.editable.getSelection();
insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();
return ModalWorkflow({
url: window.embedderChooserUrls.embedderChooser,
responses: {
snippetChosen: function(embedData) {
var elem;

elem = $(embedData).get(0);
lastSelection.insertNode(elem);
// if (elem.getAttribute('contenteditable') === 'false') {
// insertRichTextDeleteControl(elem);
// }
return widget.options.editable.element.trigger('change');
var enclosingLink, insertionPoint, lastSelection;

enclosingLink = getEnclosingEmbed();
if (!enclosingLink) {

lastSelection = widget.options.editable.getSelection();
insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();
return ModalWorkflow({
url: window.embedderChooserUrls.embedderChooser,
responses: {
snippetChosen: function(embedData) {
var elem = $(embedData).get(0);
lastSelection.insertNode(elem);
if (elem.getAttribute('contenteditable') === 'false') {
insertRichTextDeleteControl(elem);
}

var parentElement = $(lastSelection.endContainer);
var eol;
if ($(parentElement).hasClass('richtext')) {
eol = $(elem).parent().append("<br/>");
} else {
parentElement = $(parentElement).parent();
eol = $( "<br/>" ).insertAfter(parentElement);
}
placeCaretAtEnd(eol[0]);

return widget.options.editable.element.trigger('change');
}
}
}
});
});
}
});
}
});
})(jQuery);

}).call(this);






// (function() {
// (function($) {
// return $.widget("IKS.halloembedder", {
// options: {
// uuid: '',
// editable: null
// },
// populateToolbar: function(toolbar) {
// var button, getEnclosingLink, widget;

// widget = this;
// getEnclosingLink = function() {
// var node;

// node = widget.options.editable.getSelection().commonAncestorContainer;
// return $(node).parents('a').get(0);
// };
// button = $('<span></span>');
// button.hallobutton({
// uuid: this.options.uuid,
// editable: this.options.editable,
// label: 'Snippets',
// icon: 'icon-snippet',
// command: null
// /*queryState: function(event) {
// return button.hallobutton('checked', !!getEnclosingLink());
// }*/
// });
// toolbar.append(button);
// return button.on("click", function(event) {
// var insertionPoint, lastSelection;

// lastSelection = widget.options.editable.getSelection();
// insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();

// return ModalWorkflow({
// url: window.embedderChooserUrls.embedderChooser,
// responses: {
// embedChosen: function(embedData) {
// var elem;
// elem = $(embedData).get(0);
// lastSelection.insertNode(elem);
// // if (elem.getAttribute('contenteditable') === 'false') {
// // insertRichTextDeleteControl(elem);
// // }
// return widget.options.editable.element.trigger('change');
// }
// }
// });
// // enclosingLink = getEnclosingLink();
// /*if (enclosingLink) {
// $(enclosingLink).replaceWith(enclosingLink.innerHTML);
// button.hallobutton('checked', false);
// return widget.options.editable.element.trigger('change');
// } else {
// lastSelection = widget.options.editable.getSelection();
// if (lastSelection.collapsed) {
// url = window.chooserUrls.pageChooser + '?allow_external_link=true&allow_email_link=true&prompt_for_link_text=true';
// } else {
// url = window.chooserUrls.pageChooser + '?allow_external_link=true&allow_email_link=true';
// }
// return ModalWorkflow({
// url: url,
// responses: {
// pageChosen: function(pageData) {
// var a;

// a = document.createElement('a');
// a.setAttribute('href', pageData.url);
// if (pageData.id) {
// a.setAttribute('data-id', pageData.id);
// a.setAttribute('data-linktype', 'page');
// }
// if ((!lastSelection.collapsed) && lastSelection.canSurroundContents()) {
// lastSelection.surroundContents(a);
// } else {
// a.appendChild(document.createTextNode(pageData.title));
// lastSelection.insertNode(a);
// }
// return widget.options.editable.element.trigger('change');
// }
// }
// });
// }*/
// });
// }
// });
// })(jQuery);

// }).call(this);
}).call(this);
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ <h2><a class="snippet-choice" href="{% url 'wagtailembedder_choose_snippet' snip
</li>
{% endfor %}
{% else %}
{% url 'wagtailsnippets_create' content_type.app_label content_type.model as wagtailsnippets_create_url %}
<li>
<p>No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one first?</a>?</p>
<p>No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one first</a>?</p>
</li>
{% endif %}
</ul>
Expand Down
11 changes: 0 additions & 11 deletions wagtailembedder/templates/wagtailembedder/snippets/type_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ function(modal) {
$('div.snippet-list h2 a.snippet-choice').click(function(event){
event.preventDefault();
modal.loadUrl(this.href);
// $.get($(this).attr('href'), function(response) {

// })
// .done(function() {
// console.log( "second success" );
// })
// .fail(function(error) {
// console.log(error);
// console.log( "error" );
// });

return false;
});
}
11 changes: 0 additions & 11 deletions wagtailembedder/templates/wagtailembedder/snippets/types_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ function(modal) {
$('div.snippet-list h2 a.snippet-embedder').click(function(event){
event.preventDefault();
modal.loadUrl(this.href);
// $.get($(this).attr('href'), function(response) {

// })
// .done(function() {
// console.log( "second success" );
// })
// .fail(function(error) {
// console.log(error);
// console.log( "error" );
// });

return false;
});
}
22 changes: 13 additions & 9 deletions wagtailembedder/views/chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

@permission_required('wagtailadmin.access_admin')
def index(request):
snippet_types = [
(
get_snippet_type_name(content_type)[1],
get_snippet_type_description(content_type),
content_type
)
snippet_types = [(
get_snippet_type_name(content_type)[1],
get_snippet_type_description(content_type),
content_type)
for content_type in get_snippet_content_types()
if user_can_edit_snippet_type(request.user, content_type)
]
Expand All @@ -41,6 +39,7 @@ def index_objects(request, content_type_app_name, content_type_model_name):
model = content_type.model_class()
items = model.objects.all()
snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)

return render_modal_workflow(
request,
'wagtailembedder/snippets/type_index.html',
Expand All @@ -62,14 +61,19 @@ def choose_snippet(request, id, content_type_app_name, content_type_model_name):
raise PermissionDenied
model = content_type.model_class()
try:
instance = model.objects.get(id=id)
model.objects.get(id=id)
except ObjectDoesNotExist:
return render_modal_workflow(
request, None, None,
request,
None,
None,
{'error': 'Sorry, an error occurred. Contact support or try again later.'}
)
embed_html = embed_to_editor_html(id, content_type_app_name, content_type_model_name)

return render_modal_workflow(
request, None, 'wagtailembedder/editor/snippet_chosen.js',
request,
None,
'wagtailembedder/editor/snippet_chosen.js',
{'embed_html': embed_html}
)
5 changes: 4 additions & 1 deletion wagtailembedder/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def register_admin_urls():
@hooks.register('insert_editor_js')
def editor_js():
"""
Add extra JS files to the admin
Add extra JS files to the admin to register the plugin and set the reversed URL in JS vars
"""
js_files = [
'wagtailembedder/js/hallo-embedder.js',
Expand All @@ -38,4 +38,7 @@ def editor_js():

@hooks.register('before_serve_page')
def add_handler(page, request, serve_args, serve_kwargs):
"""
Call add_embed_handler() to set a custom handler for embedded snippets
"""
add_embed_handler()

0 comments on commit 48a2499

Please sign in to comment.