/usr/lib/python2.7/dist-packages/sagenb/notebook/js.py is in python-sagenb 1.0.1+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | # -*- coding: utf-8 -*
r"""nodoctest
JavaScript (AJAX) Components of the Notebook
AUTHORS:
- William Stein
- Tom Boothby
- Alex Clemesha
This file contains some minimal code to generate the Javascript code
which is inserted to the head of the notebook web page. All of the
interesting Javascript code is contained under
``data/sage/js/notebook_lib.js``.
"""
###########################################################################
# Copyright (C) 2006 William Stein <wstein@gmail.com>
# 2006 Tom Boothby <boothby@u.washington.edu>
#
# Released under the *modified* BSD license.
# Tom wrote in email to me at wstein@gmail.com on March 2, 2008:
# "You have my permission to change the license on anything I've
# contributed to the notebook, to whatever suits you."
#
###########################################################################
import os
from . import keyboards
from .template import template
from sagenb.misc.misc import SAGE_URL
from .compress.JavaScriptCompressor import JavaScriptCompressor
from hashlib import sha1
# Debug mode? If sagenb lives under SAGE_ROOT/, we minify/pack and cache
# the Notebook JS library.
debug_mode = False
try:
from sage.env import SAGE_ROOT
from pkg_resources import Requirement, working_set
sagenb_path = working_set.find(Requirement.parse('sagenb')).location
debug_mode = SAGE_ROOT not in os.path.realpath(sagenb_path)
except Exception:
pass
_cache_javascript = None
def javascript():
"""
Return javascript library for the Sage Notebook. This is done by
reading the template ``notebook_lib.js`` where all of the
javascript code is contained and replacing a few of the values
specific to the running session.
Before the code is returned (as a string), it is run through a
JavascriptCompressor to minimize the amount of data needed to be
sent to the browser.
The code and a hash of the code is returned.
.. note::
This the output of this function is cached so that it only
needs to be generated once.
EXAMPLES::
sage: from sagenb.notebook.js import javascript
sage: s = javascript()
sage: s[0][:30]
'/* JavaScriptCompressor 0.1 [w'
"""
global _cache_javascript, debug_mode
if _cache_javascript is not None:
return _cache_javascript
s = template(os.path.join('js', 'notebook_dynamic.js'),
SAGE_URL=SAGE_URL,
KEY_CODES=keyhandler.all_tests(),
debug_mode=debug_mode)
if debug_mode:
# TODO: maybe we should return a random hash so that the code
# is reloaded on every refresh
return s, sha1(s).hexdigest()
# TODO: use minify here, which is more standard (and usually safer
# and with gzip compression, smaller); But first inquire about the
# propriety of the "This software shall be used for Good, not
# Evil" clause in the license. Does that prevent us from
# distributing it (i.e., it adds an extra condition to the
# software)? See http://www.crockford.com/javascript/jsmin.py.txt
s = JavaScriptCompressor().getPacked(s.encode('utf-8'))
_cache_javascript = (s, sha1(s).hexdigest())
return _cache_javascript
class JSKeyHandler:
"""
This class is used to make javascript functions to check
for specific keyevents.
"""
def __init__(self):
self.key_codes = {};
def set(self, name, key='', alt=False, ctrl=False, shift=False):
"""
Add a named keycode to the handler. When built by
``all_tests()``, it can be called in javascript by
``key_<key_name>(event_object)``. The function returns
true if the keycode numbered by the ``key`` parameter was
pressed with the appropriate modifier keys, false otherwise.
"""
self.key_codes.setdefault(name,[])
self.key_codes[name] = [JSKeyCode(key, alt, ctrl, shift)]
def add(self, name, key='', alt=False, ctrl=False, shift=False):
"""
Similar to ``set_key(...)``, but this instead checks if
there is an existing keycode by the specified name, and
associates the specified key combination to that name in
addition. This way, if different browsers don't catch one
keycode, multiple keycodes can be assigned to the same test.
"""
try:
self.key_codes[name]
except KeyError:
self.key_codes.setdefault(name,[])
self.key_codes[name].append(JSKeyCode(key,alt,ctrl,shift))
def all_tests(self):
"""
Builds all tests currently in the handler. Returns a string
of javascript code which defines all functions.
"""
tests = ''
for name, keys in self.key_codes.items():
value = "\n||".join([k.js_test() for k in keys])
tests += " function key_%s(e) {\n return %s;\n}"%(name, value)
return tests
class JSKeyCode:
def __init__(self, key, alt, ctrl, shift):
global key_codes
self.key = key
self.alt = alt
self.ctrl = ctrl
self.shift = shift
def js_test(self):
v = 0
if self.alt:
v+=1
if self.ctrl:
v+=2
if self.shift:
v+=4
t = "((e.m==%s)&&(e.v==%s))"%(self.key,v)
return t
keyhandler = JSKeyHandler()
|