Commits

Takafumi Arakaki  committed 1627187

Rewrite neorg.js using coffee script

  • Participants
  • Parent commits 6fa3e86

Comments (0)

Files changed (5)

 syntax: glob
 build
+neorg/static/*.js
 neorg/static/help
 neorg/static/jslib
 doc/source/_templates

File neorg/static/neorg.coffee

+neorgCBOpen = (arrow) ->
+  ctd = $.colorbox.element().parent()
+  if arrow == "left"
+    otd = ctd.prev()
+  else if arrow == "right"
+    otd = ctd.next()
+  else
+    ctr = ctd.parent()
+    idx = ctr.children().index(ctd[0])
+
+    return if idx == undefined
+    if arrow == "up"
+      otr = ctr.prev()
+    else if arrow == "down"
+        otr = ctr.next()
+    otd = otr.children().eq(idx)
+
+  oa = otd.children("a.neorg-gene-image-link").first()
+  oa.click() if oa.length
+
+bindKeydown = (key, func) ->
+  $(document).bind "keydown.neorg." + key, key, func
+
+bindKeydownToNeorgCBOpen = (key) ->
+  bindKeydown key, (e) ->
+    e.preventDefault()
+    neorgCBOpen key
+
+unBindKeydown = (key) ->
+  $(document).unbind "keydown.neorg." + key
+
+arrowKeyArray = [ "left", "right", "up", "down" ]
+
+neorgCBSetting =
+  title: ->
+    $(this).attr "title", $(this).prev("p").text()
+    return  # return nothing
+
+  transition: "none"
+  speed: 0
+  maxWidth: "100%"
+  maxHeight: "100%"
+  fixed: true
+  onOpen: ->
+    for i of arrowKeyArray
+      bindKeydownToNeorgCBOpen arrowKeyArray[i]
+
+  onClosed: ->
+    for i of arrowKeyArray
+      unBindKeydown arrowKeyArray[i]
+
+neorgInit = ->
+  $(".neorg-gene-image-link").colorbox neorgCBSetting
+
+
+# export functions as global function
+# see: http://stackoverflow.com/questions/4214731/
+root = exports ? this
+root.neorgInit = neorgInit

File neorg/static/neorg.js

-function neorgCBOpen(arrow) {
-    var otd, oa;
-    var ctd = $.colorbox.element().parent();
-    if (arrow == 'left') {
-        otd = ctd.prev();
-    } else if (arrow == 'right') {
-        otd = ctd.next();
-    } else {
-        var ctr = ctd.parent();
-        var idx = ctr.children().index(ctd[0]);
-        var otr;
-        if (idx == undefined) {
-            return;
-        }
-        if (arrow == 'up') {
-            otr = ctr.prev();
-        } else if (arrow == 'down') {
-            otr = ctr.next();
-        }
-        otd = otr.children().eq(idx);
-        // alert(idx + arrow + otd.text());
-    }
-    oa = otd.children('a.neorg-gene-image-link').first();
-    if (oa.length) {
-        oa.click();
-    }
-}
-
-function bindKeydown(key, func) {
-    $(document).bind('keydown.neorg.' + key, key, func);
-}
-
-function bindKeydownToNeorgCBOpen(key) {
-    bindKeydown(key, function(e) {
-        e.preventDefault();
-        neorgCBOpen(key);
-    });
-}
-
-function unBindKeydown(key) {
-    $(document).unbind('keydown.neorg.' + key);
-}
-
-
-$(document).ready(function() {
-    $('.neorg-gene-image-link').colorbox(neorgCBSetting);
-});
-
-
-var arrowKeyArray = ['left', 'right', 'up', 'down'];
-
-var neorgCBSetting = {
-    title: function() {
-        $(this).attr('title', $(this).prev('p').text());
-    },
-    transition: 'none',
-    speed: 0,
-    maxWidth: '100%',
-    maxHeight: '100%',
-    fixed: true,
-    onOpen: function() {
-        // alert('onOpen');
-        var i;
-        for (i in arrowKeyArray) {
-            bindKeydownToNeorgCBOpen(arrowKeyArray[i]);
-        }
-    },
-    onClosed: function() {
-        // alert('onClosed');
-        var i;
-        for (i in arrowKeyArray) {
-            unBindKeydown(arrowKeyArray[i]);
-        }
-    }
-};

File neorg/templates/layout.html

       </div>
       {{ menu() }}
     </div>
+  <script type='text/javascript'>
+    neorgInit();
+  </script>
   </body>
 </html>
 
     sub_commands = [  # run update_help/js before any other sub_commands
         ('update_help', has_help_doc),
-        ('update_js', None),  # always call update_js before build
+        ('download_js', None),  # always call download_js before build
+        ('compile_js', None),   # always call compile_js before build
         ] + build.sub_commands
 
 
         ]
 
 
-class update_js(Command):
+class compile_js(Command):
+    description = 'Compile neorg.js'
+    user_options = []
+
+    def initialize_options(self):
+        pass
+
+    def finalize_options(self):
+        pass
+
+    def run(self):
+        import subprocess
+
+        commands = ['coffee', '--compile', os.path.join('neorg', 'static')]
+        commands_str = " ".join(commands)
+        log.info(commands_str)
+        proc = subprocess.Popen(
+            commands, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        ret = proc.wait()
+        if ret != 0:
+            log.info(proc.stdout.read())
+            raise RuntimeError("Command '{0}' failed".format(commands_str))
+
+
+class download_js(Command):
     description = 'Download and extract javascripts to neorg/static/js'
     user_options = []
 
 cmdclass = {
     'build': my_build,
     'update_help': update_help,
-    'update_js': update_js,
+    'download_js': download_js,
+    'compile_js': compile_js,
     }
 if BUILD_SPHINX_AVAILABLE:
     cmdclass.update(build_sphinx=BuildDoc)