Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 7896f86

Browse files
committed
Create and Use Factory pattern for making ActiveCodes.
1 parent aa4760b commit 7896f86

File tree

2 files changed

+95
-87
lines changed

2 files changed

+95
-87
lines changed

runestone/activecode/js/activecode.js

Lines changed: 94 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -659,81 +659,7 @@ ActiveCode.prototype.runProg = function() {
659659

660660
};
661661

662-
ActiveCode.addActiveCodeToDiv = function(outerdiv, acdiv, sid, initialcode, language) {
663-
var thepre, newac;
664-
$("#"+acdiv).empty();
665-
thepre = document.createElement("pre");
666-
thepre['data-component'] = "activecode";
667-
thepre.id = acdiv;
668-
thepre['data-lang'] = language;
669-
$("#"+acdiv).append(thepre);
670-
var opts = {'orig' : thepre, 'useRunestoneServices': true };
671-
// todo: look at lang to decide what kind of ActiveCode to make
672-
newac = new ActiveCode(opts);
673-
savediv = newac.divid;
674-
newac.divid = outerdiv;
675-
newac.loadEditor();
676-
newac.divid = savediv;
677-
newac.editor.setSize(500,300);
678-
};
679662

680-
ActiveCode.createScratchActivecode = function() {
681-
/* set up the scratch Activecode editor in the search menu */
682-
// use the URL to assign a divid - each page should have a unique Activecode block id.
683-
// Remove everything from the URL but the course and page name
684-
// todo: this could probably be eliminated and simply moved to the template file
685-
var divid = document.URL.split('#')[0];
686-
if (divid.indexOf('static') > -1) {
687-
divid = divid.split('static')[1];
688-
} else {
689-
divid = divid.split('/');
690-
divid = divid.slice(-2).join("");
691-
}
692-
divid = divid.split('?')[0]; // remove any query string (e.g ?lastPosition)
693-
divid = divid.replaceAll('/', '').replace('.html', '');
694-
eBookConfig.scratchDiv = divid;
695-
// generate the HTML
696-
var html = '<div id="ac_modal_' + divid + '" class="modal fade">' +
697-
' <div class="modal-dialog scratch-ac-modal">' +
698-
' <div class="modal-content">' +
699-
' <div class="modal-header">' +
700-
' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
701-
' <h4 class="modal-title">Scratch ActiveCode</h4>' +
702-
' </div> ' +
703-
' <div class="modal-body">' +
704-
' <pre data-component="activecode" id="' + divid + '">' +
705-
'<br /> ' +
706-
'<br /> ' +
707-
'<br /> ' +
708-
' </pre>' +
709-
' </div>' +
710-
' </div>' +
711-
' </div>' +
712-
'</div>';
713-
el = $(html);
714-
$('body').append(el);
715-
716-
el.on('shown.bs.modal show.bs.modal', function () {
717-
el.find('.CodeMirror').each(function (i, e) {
718-
e.CodeMirror.refresh();
719-
e.CodeMirror.focus();
720-
});
721-
});
722-
723-
$(document).bind('keypress', '\\', function(evt) {
724-
toggleScratchActivecode();
725-
return false;
726-
});
727-
};
728-
729-
730-
ActiveCode.toggleScratchActivecode = function () {
731-
var divid = "ac_modal_" + eBookConfig.scratchDiv;
732-
var div = $("#" + divid);
733-
734-
div.modal('toggle');
735-
736-
};
737663

738664

739665
JSActiveCode.prototype = new ActiveCode();
@@ -1518,21 +1444,103 @@ LiveCode.prototype.pushDataFile = function (datadiv) {
15181444
}
15191445
};
15201446

1521-
//
1447+
ACFactory = {};
1448+
1449+
ACFactory.createActiveCode = function (orig, lang) {
1450+
var opts = {'orig' : orig, 'useRunestoneServices': eBookConfig.useRunestoneServices, 'python3' : eBookConfig.python3 };
1451+
if (lang === "javascript") {
1452+
edList[orig.id] = new JSActiveCode(opts);
1453+
} else if (lang === 'htmlmixed') {
1454+
edList[orig.id] = new HTMLActiveCode(opts);
1455+
} else if (['java', 'cpp', 'c', 'python3', 'python2'].indexOf(lang) > -1) {
1456+
edList[orig.id] = new LiveCode(opts);
1457+
} else { // default is python
1458+
edList[orig.id] = new ActiveCode(opts);
1459+
}
1460+
1461+
}
1462+
1463+
// used by web2py controller(s)
1464+
ACFactory.addActiveCodeToDiv = function(outerdiv, acdiv, sid, initialcode, language) {
1465+
var thepre, newac;
1466+
$("#"+acdiv).empty();
1467+
thepre = document.createElement("pre");
1468+
thepre['data-component'] = "activecode";
1469+
thepre.id = acdiv;
1470+
thepre['data-lang'] = language;
1471+
$("#"+acdiv).append(thepre);
1472+
var opts = {'orig' : thepre, 'useRunestoneServices': true };
1473+
// todo: look at lang to decide what kind of ActiveCode to make
1474+
newac = ACFactory.createActiveCode(thepre,language);
1475+
savediv = newac.divid;
1476+
newac.divid = outerdiv;
1477+
newac.loadEditor();
1478+
newac.divid = savediv;
1479+
newac.editor.setSize(500,300);
1480+
};
1481+
1482+
ACFactory.createScratchActivecode = function() {
1483+
/* set up the scratch Activecode editor in the search menu */
1484+
// use the URL to assign a divid - each page should have a unique Activecode block id.
1485+
// Remove everything from the URL but the course and page name
1486+
// todo: this could probably be eliminated and simply moved to the template file
1487+
var divid = document.URL.split('#')[0];
1488+
if (divid.indexOf('static') > -1) {
1489+
divid = divid.split('static')[1];
1490+
} else {
1491+
divid = divid.split('/');
1492+
divid = divid.slice(-2).join("");
1493+
}
1494+
divid = divid.split('?')[0]; // remove any query string (e.g ?lastPosition)
1495+
divid = divid.replaceAll('/', '').replace('.html', '');
1496+
eBookConfig.scratchDiv = divid;
1497+
// generate the HTML
1498+
var html = '<div id="ac_modal_' + divid + '" class="modal fade">' +
1499+
' <div class="modal-dialog scratch-ac-modal">' +
1500+
' <div class="modal-content">' +
1501+
' <div class="modal-header">' +
1502+
' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
1503+
' <h4 class="modal-title">Scratch ActiveCode</h4>' +
1504+
' </div> ' +
1505+
' <div class="modal-body">' +
1506+
' <pre data-component="activecode" id="' + divid + '">' +
1507+
'<br /> ' +
1508+
'<br /> ' +
1509+
'<br /> ' +
1510+
' </pre>' +
1511+
' </div>' +
1512+
' </div>' +
1513+
' </div>' +
1514+
'</div>';
1515+
el = $(html);
1516+
$('body').append(el);
1517+
1518+
el.on('shown.bs.modal show.bs.modal', function () {
1519+
el.find('.CodeMirror').each(function (i, e) {
1520+
e.CodeMirror.refresh();
1521+
e.CodeMirror.focus();
1522+
});
1523+
});
1524+
1525+
$(document).bind('keypress', '\\', function(evt) {
1526+
ACFactory.toggleScratchActivecode();
1527+
return false;
1528+
});
1529+
};
1530+
1531+
1532+
ACFactory.toggleScratchActivecode = function () {
1533+
var divid = "ac_modal_" + eBookConfig.scratchDiv;
1534+
var div = $("#" + divid);
1535+
1536+
div.modal('toggle');
1537+
1538+
};
15221539

15231540
$(document).ready(function() {
1524-
ActiveCode.createScratchActivecode();
1541+
ACFactory.createScratchActivecode();
15251542
$('[data-component=activecode]').each( function(index ) {
1526-
var opts = {'orig' : this, 'useRunestoneServices': eBookConfig.useRunestoneServices, 'python3' : eBookConfig.python3 };
1527-
if ($(this).data('lang') === "javascript") {
1528-
edList[this.id] = new JSActiveCode(opts);
1529-
} else if ($(this).data('lang') === 'htmlmixed') {
1530-
edList[this.id] = new HTMLActiveCode(opts);
1531-
} else if (['java', 'cpp', 'c', 'python3', 'python2'].indexOf($(this).data('lang')) > -1) {
1532-
edList[this.id] = new LiveCode(opts);
1533-
} else { // default is python
1534-
edList[this.id] = new ActiveCode(opts);
1535-
}
1543+
ACFactory.createActiveCode(this, $(this).data('lang'));
15361544
});
15371545
if (loggedout) {
15381546
for (k in edList) {

runestone/common/project_template/_templates/plugin_layouts/sphinx_bootstrap/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
{% endif %}
120120
<li><a href='/{{appname}}/static/{{course_id}}/genindex.html'>Book Index</a></li>
121121
<li class="divider"></li>
122-
<li id="scratch_ac_link"><a href="javascript:ActiveCode.toggleScratchActivecode()">Scratch ActiveCode</a></li>
122+
<li id="scratch_ac_link"><a href="javascript:ACFactory.toggleScratchActivecode()">Scratch ActiveCode</a></li>
123123
<li class="divider"></li>
124124
<li style="width: 240px;">
125125
<form class="navbar-search" style="margin:10px;" action="{{ pathto('search') }}" method="get">

0 commit comments

Comments
 (0)