Html Module can now be editing, saved, and re-opened
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
from mitxmako.shortcuts import render_to_response
|
||||
from keystore.django import keystore
|
||||
from django_future.csrf import ensure_csrf_cookie
|
||||
from django.http import HttpResponse
|
||||
import json
|
||||
|
||||
|
||||
@ensure_csrf_cookie
|
||||
def index(request):
|
||||
# TODO (cpennington): These need to be read in from the active user
|
||||
org = 'mit.edu'
|
||||
@@ -20,3 +24,10 @@ def edit_item(request):
|
||||
'type': item.type,
|
||||
'name': item.name,
|
||||
})
|
||||
|
||||
|
||||
def save_item(request):
|
||||
item_id = request.POST['id']
|
||||
data = json.loads(request.POST['data'])
|
||||
keystore().update_item(item_id, data)
|
||||
return HttpResponse(json.dumps({}))
|
||||
|
||||
@@ -22,6 +22,8 @@ Longer TODO:
|
||||
import sys
|
||||
import tempfile
|
||||
import os.path
|
||||
import os
|
||||
import errno
|
||||
from path import path
|
||||
|
||||
############################ FEATURE CONFIGURATION #############################
|
||||
@@ -156,7 +158,15 @@ PIPELINE_CSS = {
|
||||
PIPELINE_ALWAYS_RECOMPILE = ['sass/base-style.scss']
|
||||
|
||||
from x_module import XModuleDescriptor
|
||||
js_file_dir = tempfile.mkdtemp('js', dir=PROJECT_ROOT / "static")
|
||||
js_file_dir = PROJECT_ROOT / "static" / "coffee" / "module"
|
||||
try:
|
||||
os.makedirs(js_file_dir)
|
||||
except OSError as exc:
|
||||
if exc.errno == errno.EEXIST:
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
|
||||
module_js_sources = []
|
||||
for xmodule in XModuleDescriptor.load_classes():
|
||||
js = xmodule.get_javascript()
|
||||
@@ -172,7 +182,7 @@ for xmodule in XModuleDescriptor.load_classes():
|
||||
|
||||
PIPELINE_JS = {
|
||||
'main': {
|
||||
'source_filenames': ['coffee/main.coffee'],
|
||||
'source_filenames': ['coffee/main.coffee', 'coffee/unit.coffee'],
|
||||
'output_filename': 'js/main.js',
|
||||
},
|
||||
'module-js': {
|
||||
|
||||
1
cms/static/.gitignore
vendored
1
cms/static/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
tmp*js
|
||||
1
cms/static/coffee/.gitignore
vendored
1
cms/static/coffee/.gitignore
vendored
@@ -1 +1,2 @@
|
||||
*.js
|
||||
module
|
||||
|
||||
@@ -3,16 +3,18 @@ bind_edit_links = ->
|
||||
edit_item($(this).attr('id'))
|
||||
return false
|
||||
|
||||
edit_item = (id) ->
|
||||
$.get('/edit_item', {id: id}, (data) ->
|
||||
edit_item = (id) =>
|
||||
$.get('/edit_item', {id: id}, (data) =>
|
||||
$('#module-html').empty().append(data)
|
||||
bind_edit_links()
|
||||
$('section.edit-pane').show()
|
||||
$('body').addClass('content')
|
||||
new window[$('#unit-wrapper').attr('class')] 'module-html'
|
||||
new @Unit('unit-wrapper', id)
|
||||
)
|
||||
|
||||
$ ->
|
||||
$.ajaxSetup
|
||||
headers : { 'X-CSRFToken': $.cookie 'csrftoken' }
|
||||
$('section.main-content').children().hide()
|
||||
$('.editable').inlineEdit()
|
||||
$('.editable-textarea').inlineEdit({control: 'textarea'})
|
||||
|
||||
13
cms/static/coffee/unit.coffee
Normal file
13
cms/static/coffee/unit.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
class @Unit
|
||||
constructor: (@element_id, @module_id) ->
|
||||
@module = new window[$("##{@element_id}").attr('class')] 'module-html'
|
||||
|
||||
$("##{@element_id} .save-update").click( (event) =>
|
||||
event.preventDefault()
|
||||
$.post("save_item", {
|
||||
id: @module_id
|
||||
data: JSON.stringify(@module.save())
|
||||
})
|
||||
|
||||
)
|
||||
|
||||
47
cms/static/js/jquery.cookie.js
Normal file
47
cms/static/js/jquery.cookie.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/*!
|
||||
* jQuery Cookie Plugin
|
||||
* https://github.com/carhartl/jquery-cookie
|
||||
*
|
||||
* Copyright 2011, Klaus Hartl
|
||||
* Dual licensed under the MIT or GPL Version 2 licenses.
|
||||
* http://www.opensource.org/licenses/mit-license.php
|
||||
* http://www.opensource.org/licenses/GPL-2.0
|
||||
*/
|
||||
(function($) {
|
||||
$.cookie = function(key, value, options) {
|
||||
|
||||
// key and at least value given, set cookie...
|
||||
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
|
||||
options = $.extend({}, options);
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
options.expires = -1;
|
||||
}
|
||||
|
||||
if (typeof options.expires === 'number') {
|
||||
var days = options.expires, t = options.expires = new Date();
|
||||
t.setDate(t.getDate() + days);
|
||||
}
|
||||
|
||||
value = String(value);
|
||||
|
||||
return (document.cookie = [
|
||||
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
|
||||
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
|
||||
options.path ? '; path=' + options.path : '',
|
||||
options.domain ? '; domain=' + options.domain : '',
|
||||
options.secure ? '; secure' : ''
|
||||
].join(''));
|
||||
}
|
||||
|
||||
// key and possibly options given, get cookie...
|
||||
options = value || {};
|
||||
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
|
||||
|
||||
var pairs = document.cookie.split('; ');
|
||||
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
|
||||
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
|
||||
}
|
||||
return null;
|
||||
};
|
||||
})(jQuery);
|
||||
@@ -34,6 +34,7 @@
|
||||
|
||||
<%static:js group='module-js'/>
|
||||
<script src="${ STATIC_URL }/js/jquery.inlineedit.js"></script>
|
||||
<script src="${ STATIC_URL }/js/jquery.cookie.js"></script>
|
||||
<script src="${ STATIC_URL }/js/jquery.leanModal.min.js"></script>
|
||||
<script src="${ STATIC_URL }/js/jquery.tablednd.js"></script>
|
||||
</body>
|
||||
|
||||
@@ -7,4 +7,5 @@ from django.conf.urls.defaults import patterns, url
|
||||
urlpatterns = patterns('',
|
||||
url(r'^$', 'contentstore.views.index', name='index'),
|
||||
url(r'^edit_item$', 'contentstore.views.edit_item', name='edit_item'),
|
||||
url(r'^save_item$', 'contentstore.views.save_item', name='save_item'),
|
||||
)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
class @HTML
|
||||
constructor: (@id) ->
|
||||
id = @id
|
||||
$("##{id} .edit-box").on('input', ->
|
||||
$("##{id} .preview").empty().append($(this).val())
|
||||
@edit_box = $("##{@id} .edit-box")
|
||||
@preview = $("##{@id} .preview")
|
||||
@edit_box.on('input', =>
|
||||
@preview.empty().append(@edit_box.val())
|
||||
)
|
||||
|
||||
save: -> $("##{@id} .edit-box").val()
|
||||
save: -> {text: @edit_box.val()}
|
||||
|
||||
Reference in New Issue
Block a user