All Projects → janosgyerik → Upvotejs

janosgyerik / Upvotejs

Licence: other
UpvoteJS generates a voting widget like the one used on Stack Exchange sites

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Upvotejs

rater-js
Star rating widget for the browser. Unlimited number of stars. No dependencies. No Jquery required.
Stars: ✭ 66 (-51.82%)
Mutual labels:  widget, voting
Flutter hooks
React hooks for Flutter. Hooks are a new kind of object that manages a Widget life-cycles. They are used to increase code sharing between widgets and as a complete replacement for StatefulWidget.
Stars: ✭ 1,973 (+1340.15%)
Mutual labels:  widget
Vue2 Bootstrap Table
A sortable and searchable table, as a Vue2 component, using bootstrap styling.
Stars: ✭ 120 (-12.41%)
Mutual labels:  widget
Widgetexamples
A demo project showcasing different types of Widgets created with SwiftUI and WidgetKit.
Stars: ✭ 125 (-8.76%)
Mutual labels:  widget
Django Jsoneditor
Django JSONEditor input widget to provide javascript online JSON Editor
Stars: ✭ 124 (-9.49%)
Mutual labels:  widget
Spperspective
Widgets iOS 14 animation with 3D and dynamic shadow. Customisable transform and duration.
Stars: ✭ 127 (-7.3%)
Mutual labels:  widget
Todayflights
TodayFlights is a Notification Center widget that allows you to track flights using the 'FlightUtilities' macOS system framework.
Stars: ✭ 118 (-13.87%)
Mutual labels:  widget
Tagger
Zero Dependency, Vanilla JavaScript Tag Editor
Stars: ✭ 135 (-1.46%)
Mutual labels:  widget
Solang
First fully featured programming language for Stack Overflow Driven Development
Stars: ✭ 133 (-2.92%)
Mutual labels:  stackoverflow
Yii2 Date Picker Widget
Bootstrap DatePicker Widget for Yii2
Stars: ✭ 128 (-6.57%)
Mutual labels:  widget
Conversable For Scriptable
Conversable is a simple contacts widget for Scriptable.
Stars: ✭ 126 (-8.03%)
Mutual labels:  widget
Uppload
📁 JavaScript image uploader and editor, no backend required
Stars: ✭ 1,673 (+1121.17%)
Mutual labels:  widget
Flutterweekview
Displays a highly customizable week view (or day view) which is able to display events, to be scrolled, to be zoomed-in & out and a lot more !
Stars: ✭ 130 (-5.11%)
Mutual labels:  widget
Stackoverflow python
stackoverflow上关于python的翻译
Stars: ✭ 119 (-13.14%)
Mutual labels:  stackoverflow
Jeelizglassesvtowidget
JavaScript/WebGL glasses virtual try on widget. Real time webcam experience, robust to all lighting conditions, high end 3D PBR rendering, easy to integrate, fallback to server-side rendering
Stars: ✭ 134 (-2.19%)
Mutual labels:  widget
Ngresizable
Simple, tree-shakable, AoT, Universal and Web Worker friendly resizable component for Angular (2 and beyond).
Stars: ✭ 118 (-13.87%)
Mutual labels:  widget
Widget
A set of widgets based on jQuery&&javascript. 一套基于jquery或javascript的插件库 :轮播、标签页、滚动条、下拉框、对话框、搜索提示、城市选择(城市三级联动)、日历等
Stars: ✭ 1,579 (+1052.55%)
Mutual labels:  widget
Tabtoolbar
A small library for creating tabbed toolbars
Stars: ✭ 129 (-5.84%)
Mutual labels:  widget
Overflow Pager Indicator
Simple paging indicator widget with pager dataset ovewflow effect à la Instagram behavior
Stars: ✭ 136 (-0.73%)
Mutual labels:  widget
Ci Buildstats
Little widget to display AppVeyor, TravisCI, CircleCI, GitHub Actions or Azure Pipelines build history charts and other SVG badges.
Stars: ✭ 134 (-2.19%)
Mutual labels:  widget

UpvoteJS

Build Status

UpvoteJS is a JavaScript package to create a voting widget that looks like the one used on Stack Exchange sites. For example, like this:

Screenshot featuring stackoverflow.com, superuser.com, serverfault.com

Using the package

There are different ways to use the package, depending on your use case.

Create a "read-only" widget in HTML

That is, have a nicely rendered widget based on HTML markup alone, to simply represent some state, that cannot be modified by user action. In this mode, no JavaScript is needed, and the counter, the upvote/downvote arrows and the star are rendered based on the HTML markup alone.

Include the stylesheet in your page's <head> element, for example:

<link rel="stylesheet" href="dist/upvotejs/upvotejs.css">

Make sure the image upvotejs.svg is present in the same directory as the upvotejs.css file.

Include this basic HTML boilerplate for each vote:

<div id="the-id" class="upvotejs">
    <a class="upvote"></a>
    <span class="count">0</span>
    <a class="downvote"></a>
    <a class="star"></a>
</div>

Customize the boilerplate with values appropriate for the vote you want to display:

  • Set the correct value for .count
  • For upvoted state, add class upvote-on for .upvote
  • For downvoted state, add class downvote-on for .downvote
  • For starred state, add class star-on for .star

With only HTML code, the widget is read-only, the voting and star buttons are not clickable.

Create an interactive widget

That is, enable user interactions to upvote, downvote, or star, modifying the state of the counter. However, storing the state on some backend is still out of scope in this mode.

Include the JavaScript sources in your page's <head> element, for example:

<script src="dist/upvotejs/upvotejs.vanilla.js"></script>

Create the Upvote widget controller:

Upvote.create('id');

Where id is the ID of the widget in the DOM.

With this step, the controls of the widget will become clickable, upvoting and downvoting will update the count and indicate toggled state, so will the star, and consistent state will be enforced.

With HTML and JavaScript code alone, the state of the widget will not be persisted in any storage.

Save the state of the widget to some backend

In order to save the state of the widget in response to user action, pass a callback handler when creating the widget, for example:

Upvote.create('id', {callback: your_callback_handler});

On any change to the state of the widget (vote up, vote down, or star), the callback handler will be called, with a JSON object as parameter, with fields:

  • id: the id of the DOM object, the same value that was used when creating with Upvote.create

  • action: the user action that triggered the state change. Possible values: upvote, unupvote, downvote, undownvote, star, unstar

  • newState: a JSON object with fields:

    • count: the current vote count
    • upvoted: true if the widget is in upvoted state
    • downvoted: true if the widget is in downvoted state
    • starred: true if the widget is in starred state

Note that upvoted and downvoted will never be true at the same time.

An example payload:

{
  id: 'my-vote',
  action: 'upvote',
  newState: {
    count: 123,
    upvoted: true,
    downvoted: false,
    starred: true
  }
}

Using this data object, it is up to your implementation of your_callback_handler to actually implement writing the new state on some backend, for example with a POST or PATCH call to a storage service to update the user's vote.

Using with jQuery

It's possible to use the package through jQuery, if you prefer (though not clear why).

Include the following in <head>:

<link rel="stylesheet" href="dist/upvotejs/upvotejs.css">
<script src="dist/upvotejs/upvotejs.vanilla.js"></script>
<script src="dist/upvotejs/upvotejs.jquery.js"></script>

Make sure the image upvotejs.svg is present in the same directory as the upvotejs.css file.

Initialization examples:

$('#topic').upvote();
$('#topic').upvote({count: 5, upvoted: true});
$('#topic').upvote({count: 5, downvoted: true});
$('#topic').upvote({count: 5, upvoted: true, starred: true});

var callback = function(data) {
    $.ajax({
        url: '/vote',
        type: 'post',
        data: data
    });
};
$('#topic-123').upvote({id: 123, callback: callback});

Methods:

// Create, pick up initial values from HTML markup
$('#topic').upvote();

// Mutators
$('#topic').upvote('upvote');       // Upvote!
$('#topic').upvote('downvote');     // Downvote!
$('#topic').upvote('star');         // Star!

// Getters
$('#topic').upvote('count');        // Get the current vote count
$('#topic').upvote('upvoted');      // Get the upvoted state -> boolean
$('#topic').upvote('downvoted');    // Get the downvoted state -> boolean
$('#topic').upvote('starred');      // Get the starred state -> boolean

API reference

Files:

  • upvotejs.css and upvotejs.svg are required for styling, and both must be in the same directory
  • upvotejs.vanilla.js is required for interactive use

Create an Upvote widget controller using Upvote.create:

const widget = Upvote.create(id, params = {});

An element in the DOM must exist with the specified id, and have a basic markup like this:

<div id="the-id" class="upvotejs">
    <a class="upvote"></a>
    <span class="count">0</span>
    <a class="downvote"></a>
    <a class="star"></a>
</div>

The widget will be parameterized based on the markup of the DOM element:

  • The count is read from the value of .count
  • The state is considered upvoted if .upvote has upvote-on class.
  • The state is considered downvoted if .downvote has downvote-on class.
  • The state is considered starred if .star has star-on class.

An exception will be thrown in case of invalid DOM markup:

  • If a DOM element with the specified id doesn't exist
  • If the DOM element with id is already used by another Upvote widget controller
  • If the DOM element doesn't have all required components
  • If both upvoted and downvoted state are detected at the same time

The state values can be overridden explicitly using the param object, with fields:

  • count - the vote count to set, must be an integer
  • upvoted - a boolean to set upvoted state
  • downvoted - a boolean to set downvoted state
  • starred - a boolean to set starred state
  • callback - a function that will be called on any state change

An exception will be thrown in case of invalid parameters:

  • If the parameter types don't match expected values
  • If upvoted and downvoted are both true

Properties

An instance of an Upvote widget controller has the following properties:

  • id: the id of the controller
  • count(): get the current count
  • upvote(): toggle upvoted state
  • upvoted(): get the upvoted state
  • downvote(): toggle downvoted state
  • downvoted(): get the downvoted state
  • star(): toggle starred state
  • starred(): get the starred state

Event handling

The widget controller, on its creation, overwrites the onclick property of the selected DOM.

On destroy, the widget controller resets onclick to null.

Sponsors

Contributions from users and sponsors help make UpvoteJS possible. We accept donations via PayPal. Thanks! :)

License

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].