Skip to content

Commit

Permalink
Making js widget configurable per instance
Browse files Browse the repository at this point in the history
  • Loading branch information
syavorsky committed Mar 11, 2011
1 parent 98a94c6 commit 8b84c4d
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 64 deletions.
30 changes: 15 additions & 15 deletions django_markdown/static/django_markdown/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,53 @@
// markItUp!
// By Jay Salvat - http://markitup.jaysalvat.com/
// ------------------------------------------------------------------*/
.markItUp .markItUpButton1 a {
.miu-icon-h1 a {
background-image:url(images/h1.png);
}
.markItUp .markItUpButton2 a {
.miu-icon-h2 a {
background-image:url(images/h2.png);
}
.markItUp .markItUpButton3 a {
.miu-icon-h3 a {
background-image:url(images/h3.png);
}
.markItUp .markItUpButton4 a {
.miu-icon-h4 a {
background-image:url(images/h4.png);
}
.markItUp .markItUpButton5 a {
.miu-icon-h5 a {
background-image:url(images/h5.png);
}
.markItUp .markItUpButton6 a {
.miu-icon-h6 a {
background-image:url(images/h6.png);
}

.markItUp .markItUpButton7 a {
.miu-icon-bold a {
background-image:url(images/bold.png);
}
.markItUp .markItUpButton8 a {
.miu-icon-italic a {
background-image:url(images/italic.png);
}

.markItUp .markItUpButton9 a {
.miu-icon-list-bullet a {
background-image:url(images/list-bullet.png);
}
.markItUp .markItUpButton10 a {
.miu-icon-list-numeric a {
background-image:url(images/list-numeric.png);
}

.markItUp .markItUpButton11 a {
.miu-icon-picture a {
background-image:url(images/picture.png);
}
.markItUp .markItUpButton12 a {
.miu-icon-link a {
background-image:url(images/link.png);
}

.markItUp .markItUpButton13 a {
.miu-icon-quotes a {
background-image:url(images/quotes.png);
}
.markItUp .markItUpButton14 a {
.miu-icon-code a {
background-image:url(images/code.png);
}

.markItUp .preview a {
.miu-icon-preview a {
background-image:url(images/preview.png);
}
177 changes: 130 additions & 47 deletions django_markdown/static/django_markdown/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,12 @@
// Copyright (C) 2008 Jay Salvat
// http://markitup.jaysalvat.com/
// -------------------------------------------------------------------
// MarkDown tags example
// http://en.wikipedia.org/wiki/Markdown
// http://daringfireball.net/projects/markdown/
// -------------------------------------------------------------------
// Feel free to add more tags
// -------------------------------------------------------------------


// mIu nameSpace to avoid conflict.
miu = (function($){

var settings = {
onShiftEnter: {keepDefault:false, openWith:'\n\n'},
markupSet: [
{name:'First Level Heading', key:'1', placeHolder:'Your title here...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '=') } },
{name:'Second Level Heading', key:'2', placeHolder:'Your title here...', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '-') } },
{name:'Heading 3', key:'3', openWith:'### ', placeHolder:'Your title here...' },
{name:'Heading 4', key:'4', openWith:'#### ', placeHolder:'Your title here...' },
{name:'Heading 5', key:'5', openWith:'##### ', placeHolder:'Your title here...' },
{name:'Heading 6', key:'6', openWith:'###### ', placeHolder:'Your title here...' },
{separator:'---------------' },
{name:'Bold', key:'B', openWith:'**', closeWith:'**'},
{name:'Italic', key:'I', openWith:'_', closeWith:'_'},
{separator:'---------------' },
{name:'Bulleted List', openWith:'- ' },
{name:'Numeric List', openWith:function(markItUp) {
return markItUp.line+'. ';
}},
{separator:'---------------' },
{name:'Picture', key:'P', replaceWith:'![[![Alternative text]!]]([![Url:!:http://]!] "[![Title]!]")'},
{name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...' },
{separator:'---------------'},
{name:'Quotes', openWith:'> '},
{name:'Code Block / Code', openWith:'(!(\t|!|`)!)', closeWith:'(!(`)!)'},
{separator:'---------------'},
{name:'Preview', call:'preview', className:"preview"}
]
};
var editors = {};

function getCookie(name) {
var value = null;
Expand All @@ -60,6 +29,58 @@ miu = (function($){
return value;
}

/*
* Work around bug in python-markdown where header underlines must be at least 3 chars
* */
function markdownTitle(markItUp, char) {
heading = '';
n = $.trim(markItUp.selection || markItUp.placeHolder).length;
if (n < 3) { n = 3; }
for(i = 0; i < n; i++) {
heading += char;
}
return '\n'+heading;
}

/*
* Default editors configuration
* */
var settings = {
onShiftEnter: {keepDefault:false, openWith:'\n\n'},
nameSpace: 'markdown',

/*
* MarkDown tags
* http://en.wikipedia.org/wiki/Markdown
* http://daringfireball.net/projects/markdown/
* */

markupSet: [
{name:'First Level Heading', key:'1', placeHolder:'Your title here...', closeWith:function(markItUp) { return markdownTitle(markItUp, '=') }, className: 'miu-icon-h1' },
{name:'Second Level Heading', key:'2', placeHolder:'Your title here...', closeWith:function(markItUp) { return markdownTitle(markItUp, '-') }, className: 'miu-icon-h2' },
{name:'Heading 3', key:'3', openWith:'### ', placeHolder:'Your title here...', className: 'miu-icon-h3' },
{name:'Heading 4', key:'4', openWith:'#### ', placeHolder:'Your title here...', className: 'miu-icon-h4' },
{name:'Heading 5', key:'5', openWith:'##### ', placeHolder:'Your title here...', className: 'miu-icon-h5' },
{name:'Heading 6', key:'6', openWith:'###### ', placeHolder:'Your title here...', className: 'miu-icon-h6' },
{separator:'---------------' },
{name:'Bold', key:'B', openWith:'**', closeWith:'**', className: 'miu-icon-bold'},
{name:'Italic', key:'I', openWith:'_', closeWith:'_', className: 'miu-icon-italic'},
{separator:'---------------' },
{name:'Bulleted List', openWith:'- ', className: 'miu-icon-list-bullet' },
{name:'Numeric List', openWith:function(markItUp) {
return markItUp.line+'. ';
}, className: 'miu-icon-list-numeric'},
{separator:'---------------' },
{name:'Picture', key:'P', replaceWith:'![[![Alternative text]!]]([![Url:!:http://]!] "[![Title]!]")', className: 'miu-icon-picture'},
{name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...', className: 'miu-icon-link' },
{separator:'---------------'},
{name:'Quotes', openWith:'> ', className: 'miu-icon-quotes'},
{name:'Code Block / Code', openWith:'(!(\t|!|`)!)', closeWith:'(!(`)!)', className: 'miu-icon-code'},
{separator:'---------------'},
{name:'Preview', call:'preview', className:"preview", className: 'miu-icon-preview'}
]
};

/*
* Add "X-CSRFToken" header to every AJAX request within current domain
* */
Expand All @@ -69,31 +90,93 @@ miu = (function($){
if(isLocal || RegExp.$1 == document.location.host){
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
})
});

/*
* Single markItUp'ed textarea class
* */
function Editor(textareId, extraSettings){

/*
* Editor instance: default mIu settings extended with what you passed to miu.init(..., extraSettings)
* */
var editorSettings = $.extend(settings, extraSettings);

/*
* Dynamicaly adds button to the editor at index position
* */
function addButton(conf, index){
var index = (!index || index > editorSettings.markupSet.length ? editorSettings.markupSet.length : index);
editorSettings.markupSet = $.merge(editorSettings.markupSet.slice(0, index),
$.merge([conf],
editorSettings.markupSet.slice(index)));
init();
}

/*
* Dynamicaly removess button from the editor at index position
* */
function removeButton(index){
editorSettings.markupSet = $.merge(editorSettings.markupSet.slice(0, index),
editorSettings.markupSet.slice(index + 1));
init();
}

/*
* Get/set editor settings
* */
function config(newSettings){
if(newSettings){
editorSettings = newSettings;
}
return editorSettings;
}

/*
* Initialize/re-initialize the editor
* */
function init(){
with($('#' + textareId)){
markItUpRemove();
markItUp(editorSettings);
}
}

/* ----- initializing ------ */

this.addButton = addButton;
this.removeButton = removeButton;
this.config = config;
this.init = init;

init();
}

return {
settings: settings,

/*
* work around bug in python-markdown where header underlines must be at least 3 chars
* Get/set default mIu settings
* */
markdownTitle: function(markItUp, char) {
heading = '';
n = $.trim(markItUp.selection || markItUp.placeHolder).length;
if (n < 3) { n = 3; }
for(i = 0; i < n; i++) {
heading += char;
}
return '\n'+heading;
},
config: function(newSettings){
if(newSettings){
settings = newSettings;
}
return settings;
},

/*
* Returns editor instance by textarea id,
* if no textareId passed then all instances will be returned
* */
editors: function(textareId){
return textareId ? editors[textareId] : editors;
},

/*
* Shortcut for initializing editor
* */
init: function(textareId, extraSettings){
$(document).ready(function() {
$('#' + textareId).markItUp($.extend(settings, extraSettings));
editors[textareId] = new Editor(textareId, extraSettings);
});
}

Expand Down
1 change: 0 additions & 1 deletion django_markdown/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from django.views.generic.simple import direct_to_template

def preview(request):
Expand Down
2 changes: 1 addition & 1 deletion django_markdown/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.simplejson import simplejson
from django.utils import simplejson


class MarkdownWidget(forms.Textarea):
Expand Down

0 comments on commit 8b84c4d

Please sign in to comment.