0

I am using one emoji jQuery plugin to rate for some article. There in emoji rating plugin file one function is used for showing count based on emoji clicked. I need to call that specific function of plugin from outside. The idea over here is to fill emoji based on given count so that without hover. Below is the plugin file:

/**
 *********************************
 * Emotions Rating - Yanci Nerio *
 *********************************
 * Emotions Rating
 * Version: 1.0.0
 * URL: https://github.com/YanNerio/emotion-ratings
 * Description: This plugin allows you to create ratings using emojis
 * Requires: >= 1.9
 * Author: Yanci Nerio (www.yancinerio.com)
 * License: MIT
 */

;(function($, document, window, undefined) {

    "use strict";

    var pluginName = 'emotionsRating';
    var $element;
    // Default options for the plugin
    var defaults = {
        bgEmotion: "happy",
        emotionsCollection: ['angry','disappointed','meh', 'happy', 'inLove'],
        count: 5,
        color: "red",
        emotionSize: 30,
        inputName: "rating",
        emotionOnUpdate: null
    };
    //the collection of emotions to show on the ratings
    var emotionsArray = {
          angry: "😠",
          disappointed: "😞",
          meh: "😐", 
          happy: "😊",
          smile: "😃",
          wink: "😉",
          laughing: "😆",
          inLove: "😍",
          heart: "❤",
          crying: "😢",
          star: "⭐",
        };
    //the collection of emotions to show on the ratings
    var colorsArray = {
            gold: "#d0a658;",
            red: "#cb2a2a;",
            blue: "#337ab7;", 
            green: "#26bf78;",
            black: "#00000;",
            brown: "#916a3a;",
            pink: "#f21f6d;",
            purple: "#ba27bd",
            orange: "#f89e5e;"
    };
    var clicked = false;

    // Plugin constructor
    function Plugin(element, options) {
        this.element = $("#element");//element;
        // Merge the options given by the user with the defaults
        this.settings = $.extend( {}, defaults, options );
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
    }
    
    //Avoiding conflicts with prototype
    $.extend(Plugin.prototype = {
        // Public functions accessible to users
        // Prototype methods are shared across all elements
        // You have access to this.settings and this.element
        init: function() {
            $element = $(this.element);
            this.count = 0;
            this.emotionStyle();
            this.renderEmotion();            
            this.manageHover();
            this.manageClick();
        },
        emotionStyle: function() {
            var styles = ".emotion-style{margin-right:3px;border-radius: 50%;cursor:pointer;opacity:0.3;display: inline-block;font-size:"
                 + this.settings.emotionSize +"px; text-decoration:none;line-height:0.9;text-align: center;color:"+colorsArray[this.settings.color]+"}";
            $element.append("<style>" + styles + "</style>");
        },
        renderEmotion: function () {
            var count = this.settings.count;
            var bgEmotion = emotionsArray[this.settings.bgEmotion];
            var container = "<div class='emotion-container'>";
            var emotionDiv;

            for (var i = 0; i < count; i++) {
                emotionDiv = "<div class='emotion-style' data-index='" + i + "'>"+bgEmotion+"</div>";
                container += emotionDiv;
            }
            container += "</div>";

            $element.append(container);
        },
        clearEmotion: function(content) {
            $element.find(".emotion-style").each( function() {
                $(this).css("opacity", 0.3);
                var bgEmotion = emotionsArray[content];
                $(this).html(bgEmotion);
            });
        },
        showEmotion: function(count) {
            this.clearEmotion(this.settings.bgEmotion);
            var emotion = getEmotion(this.settings.emotions,count);
            for (var i = 0; i < count; i++) {                
                $element.find(".emotion-style").eq(i).css("opacity", 1);
                $element.find(".emotion-style").eq(i).html(emotion);
            }
        },
        manageHover: function() {
            var self = this;

            $element.on({
                mouseenter: function() {
                    var count = parseInt($(this).data("index"), 10) + 1;

                    if (clicked) {
                       // return;
                    }
                    self.showEmotion(count);
                },
                mouseleave: function() {
                    if (!clicked) {
                        self.clearEmotion();
                    }
                }
            }, ".emotion-style" );
        },
        manageClick: function() {
            var self = this;
            $element.on("click", ".emotion-style", function() {
            var index = $(this).data("index"),
                count = parseInt(index, 10) + 1;

                self.showEmotion(count);
                self.count = count;

                if (!clicked) {
                    self.appendInput(count);
                    clicked = true;
                } else {
                    self.updateInput(count);
                }
                if ($.isFunction(self.settings.onUpdate)) {
                    self.settings.onUpdate.call(self, count);
                }
            });
        },        
        appendInput: function(count) {
            var _input = "<input type='hidden' class='emoji-rating'" + 
                    " name='" + this.settings.inputName + 
                    "' value='" + count + "' />";
   alert(count);
            $element.append(_input);
        },
        updateInput: function(count) {
            var _input = $element.find("input.emoji-rating");

            _input.val(count);
   alert(count);
        }
    });

    $.fn[pluginName] = function(options) {
        return this.each(function() {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
            }
        });
    };

    var getEmotion = function(_emotions,count) {
        var emotion;
        emotion = emotionsArray[_emotions[count-1]];
        return emotion;
    }

})(jQuery, document, window);

Then in my html page i am trying to access manageclick function like below:

<div class="container">
    <h1>jQuery Emotion Ratings Plugin Demo</h1>
    
    <div id="element1"></div>
</div>
<script src="@Url.Content("~/Content/Ratings/emotion-ratings.js")"></script>
<script src="@Url.Content("~/Content/Ratings/emoji_ga.js")"></script>
<script>

 
  var test = $("#element1").emotionsRating({});
  $.test.manageClick();
  
</script>

Here in above emotion-rating.js file plugin code is used.

But getting error manageClick of undefined. How could I access this function externally?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Prity Kumari
  • 85
  • 2
  • 10

1 Answers1

1

I read the source code of this plugin,noticing that it has no option or public method to fill emoji based on given count,but there is still an alternative that can fulfill your requirement(Be aware that the source code you used above has been modified from the original code )

  1. Simply but recommended one.You can trigger the click event manually by doing this

$('#element .emotion-style:eq('+count+')').click()

'element' is the id attr of dom container in which you initialize the plugin

  1. Or you can modify the source code to provide an open option.When the option is set,trigger the click event in the init method
neo
  • 604
  • 3
  • 10
  • Hi Neo, Yes This suggested solution is what i was expecting from. Thank you very much. Upvoting your answer. – Prity Kumari Dec 03 '17 at 15:40
  • Hi Neo just one query, Can i use some flag in mouse hover function so when i send some value externally to get emoji then can i stop hover so that in mouse hover it should not change emoji selection should not change. – Prity Kumari Dec 09 '17 at 14:20
  • sorry,I didn't get what you meant. In my version of the plugin, the emoji selection doesn't change on mouse-hover event if there is already an emoji that has been selected. – neo Dec 12 '17 at 04:22
  • Hi Neo, Yes you are right Mouse hover event is not firing after emoji selected. Here i have one query like i need to use emoji ratings on every comment. So when i am getting saved html comment in jquery response then there i have one div which id is element. so every html comment i am taking one loop. Now how could i set the count for each div element so for every comment i should see emoji rating? – Prity Kumari Dec 25 '17 at 13:50
  • I think I'm little late answering to this question, I'm glad somebody else did it, well I just added the option to set an initial rating value to the plugin hope this helps someone else https://github.com/YanNerio/emotion-ratings/ And thank you for using it – Yanci Nerio Mar 19 '18 at 18:11