From ad29d361c346bdc58e003514cd560733a1d00d2a Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Thu, 14 Aug 2014 10:16:42 -0400 Subject: [PATCH] Transition to mkwsref xblock (reference widget) --- mkwsref/mkwsref/__init__.py | 1 + mkwsref/mkwsref/mkwsref.py | 71 +++++++++++++++++++++++++++++ mkwsref/mkwsref/static/html/settings.html | 21 +++++++++ mkwsref/mkwsref/static/html/student.html | 3 ++ mkwsref/mkwsref/static/js/src/settings.js | 16 +++++++ mkwsref/mkwsref/static/js/src/student.js | 18 ++++++++ mkwsref/requirements.txt | 1 + mkwsref/setup.py | 37 +++++++++++++++ mkwsxb/__init__.py | 1 - mkwsxb/mkwsxb.py | 64 -------------------------- mkwsxb/static/css/mkws-widget-ru.css | 37 --------------- mkwsxb/static/html/mkwsxb.html | 3 -- mkwsxb/static/html/settings.html | 21 --------- mkwsxb/static/js/src/mkwsxb.js | 22 --------- mkwsxb/static/js/src/settings.js | 16 ------- requirements.txt | 1 - setup.py | 37 --------------- 17 files changed, 168 insertions(+), 202 deletions(-) create mode 100644 mkwsref/mkwsref/__init__.py create mode 100644 mkwsref/mkwsref/mkwsref.py create mode 100644 mkwsref/mkwsref/static/html/settings.html create mode 100644 mkwsref/mkwsref/static/html/student.html create mode 100644 mkwsref/mkwsref/static/js/src/settings.js create mode 100644 mkwsref/mkwsref/static/js/src/student.js create mode 100644 mkwsref/requirements.txt create mode 100644 mkwsref/setup.py delete mode 100644 mkwsxb/__init__.py delete mode 100644 mkwsxb/mkwsxb.py delete mode 100644 mkwsxb/static/css/mkws-widget-ru.css delete mode 100644 mkwsxb/static/html/mkwsxb.html delete mode 100644 mkwsxb/static/html/settings.html delete mode 100644 mkwsxb/static/js/src/mkwsxb.js delete mode 100644 mkwsxb/static/js/src/settings.js delete mode 100644 requirements.txt delete mode 100644 setup.py diff --git a/mkwsref/mkwsref/__init__.py b/mkwsref/mkwsref/__init__.py new file mode 100644 index 0000000..b36c626 --- /dev/null +++ b/mkwsref/mkwsref/__init__.py @@ -0,0 +1 @@ +from .mkwsref import MKWSRef diff --git a/mkwsref/mkwsref/mkwsref.py b/mkwsref/mkwsref/mkwsref.py new file mode 100644 index 0000000..dd0fce0 --- /dev/null +++ b/mkwsref/mkwsref/mkwsref.py @@ -0,0 +1,71 @@ +"""Embed reference widget from MKWS, the MasterKey Widget Set""" + +import pkg_resources +import random + +from xblock.core import XBlock +from xblock.fields import Integer, Scope, String, Any, Boolean, Dict +from xblock.fragment import Fragment + +class MKWSRef(XBlock): + """Embed reference widget from MKWS, the MasterKey Widget Set""" + + # Fields + query = String( + help="Search query", + default="water", + scope=Scope.content + ) + display_name = String( + default="MKWS Reference Widget", + scope=Scope.settings + ) + + def resource_string(self, path): + """Helper for accessing resources.""" + data = pkg_resources.resource_string(__name__, path) + return data.decode("utf8") + + def student_view(self, context=None): + """The primary view of the MKWS XBlock, shown to students when viewing courses.""" + html = self.resource_string("static/html/student.html") + frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000))) + # student.js uses require.js as it cannot guarantee mkws-complete.js has loaded + # in studio without it. We'll need to add it if we're in the LMS: + frag.add_javascript_url("/static/js/vendor/require.js"); + frag.add_javascript(self.resource_string("static/js/src/student.js")) + frag.initialize_js('MKWSRef') + return frag; + + def author_view(self, context=None): + """View of the MKWS XBlock shown when authoring courses.""" + html = self.resource_string("static/html/student.html") + frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000))) + frag.add_javascript(self.resource_string("static/js/src/student.js")) + frag.initialize_js('MKWSRef') + return frag; + + def studio_view(self, context=None): + """Studio configuration view.""" + html = self.resource_string("static/html/settings.html") + frag = Fragment(html.format(query=self.query)) + frag.add_javascript(self.resource_string("static/js/src/settings.js")) + frag.initialize_js('MKWSRefSettings') + return frag + + @XBlock.json_handler + def update_settings(self, data, suffix=''): + """Studio configuration callback.""" + self.query = data['query'] + return {"result": "success"} + + @staticmethod + def workbench_scenarios(): + """A canned scenario for display in the workbench.""" + return [ + ("MKWSRef", + """ + + + """), + ] diff --git a/mkwsref/mkwsref/static/html/settings.html b/mkwsref/mkwsref/static/html/settings.html new file mode 100644 index 0000000..2bf5f56 --- /dev/null +++ b/mkwsref/mkwsref/static/html/settings.html @@ -0,0 +1,21 @@ +
+ +
+ +
+
diff --git a/mkwsref/mkwsref/static/html/student.html b/mkwsref/mkwsref/static/html/student.html new file mode 100644 index 0000000..3a5cd7f --- /dev/null +++ b/mkwsref/mkwsref/static/html/student.html @@ -0,0 +1,3 @@ +
+
Loading reference...
+
diff --git a/mkwsref/mkwsref/static/js/src/settings.js b/mkwsref/mkwsref/static/js/src/settings.js new file mode 100644 index 0000000..c44e65d --- /dev/null +++ b/mkwsref/mkwsref/static/js/src/settings.js @@ -0,0 +1,16 @@ +function MKWSRefSettings(runtime, element) { + $(element).find('.save-button').bind('click', function() { + var handlerUrl = runtime.handlerUrl(element, 'update_settings'); + var data = { + query: $(element).find('input[name=query]').val() + }; + console.log($(element).find('input[query]')); + $.post(handlerUrl, JSON.stringify(data)).done(function(response) { + window.location.reload(false); + }); + }); + + $(element).find('.cancel-button').bind('click', function() { + runtime.notify('cancel', {}); + }); +}; diff --git a/mkwsref/mkwsref/static/js/src/student.js b/mkwsref/mkwsref/static/js/src/student.js new file mode 100644 index 0000000..fb70a1c --- /dev/null +++ b/mkwsref/mkwsref/static/js/src/student.js @@ -0,0 +1,18 @@ +/* Javascript for MKWSRef. */ +require.config({ + paths: { + mkws: "//mkws.indexdata.com/mkws-complete", + }, + shim: { + mkws: { + exports: "mkws" + }, + } +}); +function MKWSRef(runtime, element) { + require(['mkws'], function() { + console.log(mkws); + //mkws.init("XBlock initialised.", element); + mkws.init("XBlock initialised."); + } ); +} diff --git a/mkwsref/requirements.txt b/mkwsref/requirements.txt new file mode 100644 index 0000000..d6e1198 --- /dev/null +++ b/mkwsref/requirements.txt @@ -0,0 +1 @@ +-e . diff --git a/mkwsref/setup.py b/mkwsref/setup.py new file mode 100644 index 0000000..096e650 --- /dev/null +++ b/mkwsref/setup.py @@ -0,0 +1,37 @@ +"""Setup for mkwsref XBlock.""" + +import os +from setuptools import setup + +def package_data(pkg, roots): + """Generic function to find package_data. + + All of the files under each of the `roots` will be declared as package + data for package `pkg`. + + """ + data = [] + for root in roots: + for dirname, _, files in os.walk(os.path.join(pkg, root)): + for fname in files: + data.append(os.path.relpath(os.path.join(dirname, fname), pkg)) + + return {pkg: data} + +setup( + name='mkwsref', + version='0.1', + description='XBlock to embed an MKWS reference widget', + packages=[ + 'mkwsref', + ], + install_requires=[ + 'XBlock', + ], + entry_points={ + 'xblock.v1': [ + 'mkwsref = mkwsref:MKWSRef', + ] + }, + package_data=package_data("mkwsref", ["static", "public"]), +) diff --git a/mkwsxb/__init__.py b/mkwsxb/__init__.py deleted file mode 100644 index ac8aa9b..0000000 --- a/mkwsxb/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .mkwsxb import MKWSXB \ No newline at end of file diff --git a/mkwsxb/mkwsxb.py b/mkwsxb/mkwsxb.py deleted file mode 100644 index 13822ea..0000000 --- a/mkwsxb/mkwsxb.py +++ /dev/null @@ -1,64 +0,0 @@ -"""Embed widgets from MKWS, the MasterKey Widget Set""" - -import pkg_resources -import random - -from xblock.core import XBlock -from xblock.fields import Integer, Scope, String, Any, Boolean, Dict -from xblock.fragment import Fragment - -class MKWSXB(XBlock): - """Embed widgets from MKWS, the MasterKey Widget Set""" - - # Fields - query = String( - help="Search query", - default="water", - scope=Scope.content - ) - display_name = String( - default="MKWS Widget", - scope=Scope.settings - ) - - def resource_string(self, path): - """Helper for accessing resources.""" - data = pkg_resources.resource_string(__name__, path) - return data.decode("utf8") - - def student_view(self, context=None): - """The primary view of the MKWS XBlock, shown to students when viewing courses.""" - html = self.resource_string("static/html/mkwsxb.html") - frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000))) - # mkwsxb.js uses require.js as it cannot guarantee mkws-complete.js has loaded - # in studio without it - frag.add_javascript_url("/static/js/vendor/require.js"); - frag.add_javascript(self.resource_string("static/js/src/mkwsxb.js")) - frag.add_css(self.resource_string("static/css/mkws-widget-ru.css")) - frag.initialize_js('MKWSXB') - return frag; - - def studio_view(self, context=None): - """Studio configuration view.""" - html = self.resource_string("static/html/settings.html") - frag = Fragment(html.format(query=self.query)) - frag.add_javascript(self.resource_string("static/js/src/settings.js")) - frag.initialize_js('MKWSXBSettings') - return frag - - @XBlock.json_handler - def update_settings(self, data, suffix=''): - """Studio configuration callback.""" - self.query = data['query'] - return {"result": "success"} - - @staticmethod - def workbench_scenarios(): - """A canned scenario for display in the workbench.""" - return [ - ("MKWSXB", - """ - - - """), - ] diff --git a/mkwsxb/static/css/mkws-widget-ru.css b/mkwsxb/static/css/mkws-widget-ru.css deleted file mode 100644 index 74f6f48..0000000 --- a/mkwsxb/static/css/mkws-widget-ru.css +++ /dev/null @@ -1,37 +0,0 @@ -.mkwsReferenceUniverse { - font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; - background: #FCFBFA; - padding: 0.5em 1em 0.25em; - box-shadow: 0 0 2px 0 #7F8F93; - border-radius: 0 0 1.5em; - -moz-border-radius: 0 0 1.5em; - -webkit-border-radius: 0 0 1.5em; - line-height: 1.4; - color: #86979B; - background: radial-gradient(ellipse at center, #ffffff 0%,#f8f8f8 100%); -} - -.mkwsReferenceUniverse h2 { - font-size: 100%; - color: #4A5456; - padding-bottom: .5em; -} - -.mkwsReferenceUniverse ul { - margin: 0; - padding: 0; -} - -.mkwsReferenceUniverse li { - margin: .95em .25em; - padding-top: .75em; - border-top: 1px dotted #BEC8CC; - font-size: 90%; - list-style: none; -} - -.mkwsReferenceUniverse a { - text-decoration: none; - font-weight:bold; - color: #2B77AF; -} diff --git a/mkwsxb/static/html/mkwsxb.html b/mkwsxb/static/html/mkwsxb.html deleted file mode 100644 index e1107a6..0000000 --- a/mkwsxb/static/html/mkwsxb.html +++ /dev/null @@ -1,3 +0,0 @@ -
-
Searching Reference Universe...
-
diff --git a/mkwsxb/static/html/settings.html b/mkwsxb/static/html/settings.html deleted file mode 100644 index 2bf5f56..0000000 --- a/mkwsxb/static/html/settings.html +++ /dev/null @@ -1,21 +0,0 @@ -
- -
- -
-
diff --git a/mkwsxb/static/js/src/mkwsxb.js b/mkwsxb/static/js/src/mkwsxb.js deleted file mode 100644 index dcf79e6..0000000 --- a/mkwsxb/static/js/src/mkwsxb.js +++ /dev/null @@ -1,22 +0,0 @@ -/* Javascript for MKWSXB. */ -require.config({ - paths: { - mkws: "//mkws.indexdata.com/mkws-complete", - mkws_widget_ru: "//example.indexdata.com/mkws-widget-ru" - }, - shim: { - mkws: { - exports: "mkws" - }, - mkws_widget_ru: { - deps: ["mkws"] - } - } -}); -function MKWSXB(runtime, element) { - require(['mkws_widget_ru'], function() { - console.log(mkws); - //mkws.init("XBlock initialised.", element); - mkws.init("XBlock initialised."); - } ); -} diff --git a/mkwsxb/static/js/src/settings.js b/mkwsxb/static/js/src/settings.js deleted file mode 100644 index 07d5f83..0000000 --- a/mkwsxb/static/js/src/settings.js +++ /dev/null @@ -1,16 +0,0 @@ -function MKWSXBSettings(runtime, element) { - $(element).find('.save-button').bind('click', function() { - var handlerUrl = runtime.handlerUrl(element, 'update_settings'); - var data = { - query: $(element).find('input[name=query]').val() - }; - console.log($(element).find('input[query]')); - $.post(handlerUrl, JSON.stringify(data)).done(function(response) { - window.location.reload(false); - }); - }); - - $(element).find('.cancel-button').bind('click', function() { - runtime.notify('cancel', {}); - }); -}; diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index d6e1198..0000000 --- a/requirements.txt +++ /dev/null @@ -1 +0,0 @@ --e . diff --git a/setup.py b/setup.py deleted file mode 100644 index 7de2766..0000000 --- a/setup.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Setup for mkwsxb XBlock.""" - -import os -from setuptools import setup - -def package_data(pkg, roots): - """Generic function to find package_data. - - All of the files under each of the `roots` will be declared as package - data for package `pkg`. - - """ - data = [] - for root in roots: - for dirname, _, files in os.walk(os.path.join(pkg, root)): - for fname in files: - data.append(os.path.relpath(os.path.join(dirname, fname), pkg)) - - return {pkg: data} - -setup( - name='mkwsxb-xblock', - version='0.1', - description='XBlock to embed MKWS widgets', - packages=[ - 'mkwsxb', - ], - install_requires=[ - 'XBlock', - ], - entry_points={ - 'xblock.v1': [ - 'mkwsxb = mkwsxb:MKWSXB', - ] - }, - package_data=package_data("mkwsxb", ["static", "public"]), -) -- 1.7.10.4