Skip to content

Commit

Permalink
object to iframe move
Browse files Browse the repository at this point in the history
  • Loading branch information
nunpa committed Mar 14, 2023
1 parent ea8ef5f commit faee749
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 0 deletions.
1 change: 1 addition & 0 deletions pdf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .pdf import pdfXBlock
77 changes: 77 additions & 0 deletions pdf/pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""TO-DO: Write a description of what this XBlock is."""

import pkg_resources

from xblock.core import XBlock
from xblock.fields import Scope, Integer, String
from xblock.fragment import Fragment

class pdfXBlock(XBlock):
"""
TO-DO: document what your XBlock does.
"""
# Fields are defined on the class. You can access them in your code as
# self.<fieldname>.
# TO-DO: change the default href so it is included as a resource in the xblock, not an url
href = String(display_name="href",
default="http://www.upv.es/plano/directorio-es.pdf",
scope=Scope.content,
help="PDF file that will be shown in the XBlock")

display_name = String(display_name="Display Name",
default="PDF File",
scope=Scope.settings,
help="Name of the component in the edxplatform")

def resource_string(self, path):
"""Handy helper for getting resources from our kit."""
data = pkg_resources.resource_string(__name__, path)
return data.decode("utf8")


def student_view(self, context=None):
"""
The primary view of the pdfXBlock, shown to students
when viewing courses.
"""
html = self.resource_string("static/html/pdf.html")
frag = Fragment(html.format(self=self))
frag.add_css(self.resource_string("static/css/pdf.css"))
return frag


def studio_view(self, context=None):
"""
The primary view of the paellaXBlock, shown to students
when viewing courses.
"""
html = self.resource_string("static/html/pdf_edit.html")
frag = Fragment(html.format(self=self))
frag.add_javascript(self.resource_string("static/js/src/pdf_edit.js"))
frag.initialize_js('pdfXBlock')
return frag

@XBlock.json_handler
def save_pdf(self, data, suffix=''):
"""
An example handler, which increments the data.
"""
self.href = data['href']
self.display_name = data['display_name']

return {
'result': 'success',
}

# TO-DO: change this to create the scenarios you'd like to see in the
# workbench while developing your XBlock.
@staticmethod
def workbench_scenarios():
"""A canned scenario for display in the workbench."""
return [
("pdfXBlock",
"""<vertical_demo>
<pdf/>
</vertical_demo>
"""),
]
9 changes: 9 additions & 0 deletions pdf/static/css/pdf.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* CSS for pdfXBlock */

.pdf_block .count {
font-weight: bold;
}

.pdf_block p {
cursor: pointer;
}
7 changes: 7 additions & 0 deletions pdf/static/html/pdf.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="pdf_block">
<iframe src="{self.href}" width="100%" height="500" frameBorder="0">
<p>It appears you don't have a PDF plugin for this browser.
You can <a href="{self.href}">click here to
download the PDF file.</a></p>
</iframe>
</div>
31 changes: 31 additions & 0 deletions pdf/static/html/pdf_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="wrapper-comp-settings is-active editor-with-buttons " id="settings-tab">
<ul class="list-input settings-list">
<li class="field comp-setting-entry is-set">
<div class="wrapper-videolist-url videolist-settings-item">
<label class="label setting-label">Title</label>
<input class="input setting-input edit-display-name" id="edit_display_name" value="{self.display_name}" type="text">
<span class="tip setting-help">The title of the PDF file that is displayed to the user</span>
</div>
</li>
<li class="field comp-setting-entry is-set">
<div class="wrapper-videolist-url videolist-settings-item">
<label class="label setting-label">PDF url</label>
<input class="input setting-input edit-display-name" id="edit_href" value="{self.href}" type="text">
<span class="tip setting-help">The url of the pdf file</span>
</div>
</li>
</ul>
<div class="xblock-actions">
<span class="xblock-editor-error-message"></span>
<ul>
<li class="action-item">
<a href="#" class="save-button action-primary action">Save</a>
</li>
<li class="action-item">
<a href="#" class="button cancel-button">Cancel</a>
</li>
</ul>
</div>
</div>


36 changes: 36 additions & 0 deletions pdf/static/js/src/pdf_edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Javascript for pdfXBlock. */
function pdfXBlock(runtime, element) {

function paellaSaved(result) {
$('.server', element).text();
$('.video_id', element).text(result.video_id);
$('.display_name', element).text(result.display_name);
}

$(element).find('.cancel-button').bind('click', function() {
runtime.notify('cancel', {});
});

$(element).find('.save-button').bind('click', function() {
var data = {
'display_name': $(edit_display_name).context.value,
'href':$(edit_href).context.value
};

$('.xblock-editor-error-message', element).html();
$('.xblock-editor-error-message', element).css('display', 'none');
var handlerUrl = runtime.handlerUrl(element, 'save_pdf');
$.post(handlerUrl, JSON.stringify(data)).done(function(response) {
if (response.result === 'success') {
window.location.reload(false);
} else {
$('.xblock-editor-error-message', element).html('Error: '+response.message);
$('.xblock-editor-error-message', element).css('display', 'block');
}
});
});

$(function ($) {
/* Here's where you'd do things on page load. */
});
}
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Setup for pdf XBlock."""

import os
from setuptools import setup


def package_data(pkg, root):
"""Generic function to find package_data for `pkg` under `root`."""
data = []
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='pdf-xblock',
version='0.1',
description='pdf XBlock', # TODO: write a better description.
packages=[
'pdf',
],
install_requires=[
'XBlock',
],
entry_points={
'xblock.v1': [
'pdf = pdf:pdfXBlock',
]
},
package_data=package_data("pdf", "static"),
)

0 comments on commit faee749

Please sign in to comment.