You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2018/11/08 21:53:51 UTC

[10/10] allura git commit: [#8253] Refactor logic and cleanup codes

[#8253] Refactor logic and cleanup codes


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/c54ede38
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/c54ede38
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/c54ede38

Branch: refs/heads/master
Commit: c54ede38405979855a8cc779a92713942d2994c0
Parents: 8d3b43b
Author: Shalitha Suranga <sh...@gmail.com>
Authored: Mon Nov 5 13:37:19 2018 +0530
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Thu Nov 8 21:53:25 2018 +0000

----------------------------------------------------------------------
 Allura/allura/lib/utils.py                      |  9 +++
 Allura/allura/lib/widgets/discuss.py            | 74 ++----------------
 .../lib/widgets/resources/js/reactions.js       | 81 ++++++++++++++++++++
 3 files changed, 95 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/c54ede38/Allura/allura/lib/utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 07fd2a1..7a13f17 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -36,6 +36,7 @@ import tg
 import pylons
 import json
 from formencode import Invalid
+from collections import OrderedDict
 from tg.decorators import before_validate
 from pylons.controllers.util import etag_cache
 from paste.deploy.converters import asbool, asint
@@ -774,3 +775,11 @@ def get_reaction_emoji_list():
     else:
         emo_list = emo_list.split(',')
     return emo_list
+
+
+def get_reactions_json():
+    """ Returns global reactions json """
+    j = OrderedDict()
+    for em in get_reaction_emoji_list():
+        j[em] =  pylons.app_globals.emojize(em)
+    return json.dumps(j)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/allura/blob/c54ede38/Allura/allura/lib/widgets/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/discuss.py b/Allura/allura/lib/widgets/discuss.py
index 9ff25f9..1a38101 100644
--- a/Allura/allura/lib/widgets/discuss.py
+++ b/Allura/allura/lib/widgets/discuss.py
@@ -17,6 +17,7 @@
 
 from formencode import validators as fev
 
+import json
 import ew as ew_core
 import ew.jinja2_ew as ew
 from pylons import app_globals as g
@@ -303,15 +304,6 @@ class Post(HierWidget):
         attach_post=AttachPost(submit_text='Attach'),
         attachment=Attachment())
 
-    def get_reaction_tooltip_content(self):
-        """Construct html content for the reaction tooltip"""
-        html = ''
-        for em in utils.get_reaction_emoji_list():
-            emoji_u = g.emojize(em)
-            html += '<span onclick="reactComment(\'` + btnId +  `\', \'%s\')">` + twemoji.parse(\'%s\') + `</span>' % \
-            (em, emoji_u)
-        return html
-
     def resources(self):
         for r in super(Post, self).resources():
             yield r
@@ -439,66 +431,6 @@ class Post(HierWidget):
             });
         }());
         ''')
-        
-        yield ew.JSScript('''
-        // Reaction support
-        $(function(){
-
-        $('.reaction-button').tooltipster({
-            animation: 'fade',
-            delay: 200,
-            theme: 'tooltipster-default',
-            trigger: 'click',
-            position: 'top',
-            iconCloning: false,
-            maxWidth: 400,
-            contentAsHTML: true,
-            interactive: true
-        });
-
-        $('.reaction-button').each(function() {
-            var btnId = $(this).attr('id');
-            $(this).click(function(e) {
-                e.preventDefault();
-                var tooltiptext = `
-                <div class="post-reactions-list">
-                    %s
-                </div>
-                `;
-                $(this).tooltipster('content', tooltiptext);
-                $(this).tooltipster('show', function() {
-                    twemoji.parse($('.tooltipster-content')[0]);
-                });
-            });
-        });
-
-
-        });
-
-        function reactComment(btnId ,r) {
-        var btn = $('#' + btnId);
-        var reacts_list = btn.closest('.post-content').find('.reactions');
-        $.ajax({
-            type: 'post',
-            url: btn.data('commentlink') + 'post_reaction',
-            data: {
-                'r' : r,
-                '_session_id' : $.cookie('_session_id')
-            },
-            success: function(res) {
-                var react_html = '';
-
-                for (var i in res.counts) {
-                    react_html += '<span>' + res.emoji_unicode[i] + '</span> ' + res.counts[i];
-                }
-                
-                reacts_list.html(react_html);
-                twemoji.parse(reacts_list[0]);
-                btn.tooltipster('hide');
-            }
-        });
-        }
-        ''' % self.get_reaction_tooltip_content() ) 
 
 
 class PostThread(ew_core.Widget):
@@ -580,3 +512,7 @@ class Thread(HierWidget):
             }
         });
         ''')
+        yield ew.JSScript('''
+            var global_reactions = %s;
+        ''' % utils.get_reactions_json())
+        yield ew.JSLink('js/reactions.js')

http://git-wip-us.apache.org/repos/asf/allura/blob/c54ede38/Allura/allura/lib/widgets/resources/js/reactions.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/reactions.js b/Allura/allura/lib/widgets/resources/js/reactions.js
new file mode 100644
index 0000000..f8df816
--- /dev/null
+++ b/Allura/allura/lib/widgets/resources/js/reactions.js
@@ -0,0 +1,81 @@
+/*
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+ */
+
+// Reaction support
+
+$(function(){
+
+$('.reaction-button').tooltipster({
+    animation: 'fade',
+    delay: 200,
+    theme: 'tooltipster-default',
+    trigger: 'click',
+    position: 'top',
+    iconCloning: false,
+    maxWidth: 400,
+    contentAsHTML: true,
+    interactive: true,
+    functionReady: function(instance, helper) {
+        $(helper).find('.emoji_button').click(function() {
+            reactComment(instance, $(this).data('emoji'));
+        });
+    }
+});
+
+$('.reaction-button').each(function() {
+    var btnId = $(this).attr('id');
+    $(this).click(function(e) {
+        e.preventDefault();
+
+        var emohtml = '';
+        for(var emo in global_reactions) {
+            emohtml += '<span class=\'emoji_button\' data-emoji=\'' + emo + '\'>' + 
+            twemoji.parse(global_reactions[emo]) + '</span>';
+        }
+        var tooltiptext = '<div class="post-reactions-list">' + emohtml + '</div>';
+        $(this).tooltipster('content', tooltiptext);
+        $(this).tooltipster('show');
+    });
+});
+
+
+});
+
+function reactComment(btn ,r) {
+var reacts_list = btn.closest('.post-content').find('.reactions');
+$.ajax({
+    type: 'post',
+    url: btn.data('commentlink') + 'post_reaction',
+    data: {
+        'r' : r,
+        '_session_id' : $.cookie('_session_id')
+    },
+    success: function(res) {
+        var react_html = '';
+
+        for (var i in res.counts) {
+            react_html += '<span>' + res.emoji_unicode[i] + '</span> ' + res.counts[i];
+        }
+        
+        reacts_list.html(react_html);
+        twemoji.parse(reacts_list[0]);
+        btn.tooltipster('hide');
+    }
+});
+}
\ No newline at end of file