Code Monkey home page Code Monkey logo

availability.js's Introduction

Availability.js

The Availability Badge allows Cushion users to display their availability on their own website. The badge is tied directly to the availability data in Cushion, so it will automatically update when the user’s status changes.

Basic Usage

Ribbon

Include the following code in your HTML, and replace $YOUR_USER_ID with your user ID, which can be found in the Availability Badge’s add-on page.

<link href="//static.cushionapp.com/availability.css" rel="stylesheet">
<script src="//static.cushionapp.com/availability.js" data-user="$YOUR_USER_ID"></script>

Badge

Include the same code from the ribbon example above, but add an data-availability-badge attribute to the element you’d like the badge to be displayed within.

<link href="//static.cushionapp.com/availability.css" rel="stylesheet">
<script src="//static.cushionapp.com/availability.js" data-user="$YOUR_USER_ID"></script>

<p>I'm currently <span data-availability-badge></span>.</p>

Customizing the Badge

Styling with CSS

The appearance of both the badge and ribbon can be modified using CSS. Simply include your CSS after the code that loads the Availability Badge stylesheet and use availability.css as a reference for class names:

<style>
  .availability-ribbon__text {
    font-weight: bold;
  }

  .availability-ribbon.unavailable .availability-ribbon__banner {
    fill: red;
  }
</style>

Building a Custom Badge

If CSS is too limiting, you can also define your own function to assemble custom HTML, draw with canvas, or even play a silly video.

To start, include only the Availability Badge’s JavaScript file in your HTML:

<script src="//static.cushionapp.com/availability.js"></script>

Then, use the Availabilty.custom function to define your user ID and render function:

Availability.custom({ user: '$YOUR_USER_ID', render: function() {} })

Inside the render function, this is bound to an Availabilty object. The function should handle all logic on its own, and no return value is required.

Note: To make sure everything has loaded before executing your code, place it before the closing </body> tag, or within a DOMContentLoaded listener ($(document).ready in jQuery).

Here’s an example of a simple GIF badge:

function renderGIF () {
  var img = document.querySelector('.image')
  if (this.isAvailable())   img.src = 'http://i.giphy.com/urhcoANPxB3K8.gif'
  if (this.isUnavailable()) img.src = 'http://i.giphy.com/O4caHIyGGVTW.gif'
  if (this.isSoon())        img.src = 'http://i.giphy.com/ErLimaUL0blbW.gif'
}

Availability.custom({ user: '$YOUR_USER_ID', render: renderGIF })

Availability object

The Availability object is bound to this within a render function and has the following attributes:

  1. this.availability: A string with a value of available, soon, unavailable or private
  2. this.date: A date object, or null if the above is unavailable or private
  3. this.hours: A number ≥ 0, representing the number of hours of availability
  4. this.user: Basic user info, including nickname, color, and referral_code

There are also some helper functions:

this.month()

Returns the month name of this.date.

this.monthShort()

Returns the month name of this.date shortened to 3–5 characters.

this.isAvailable()
this.isUnvailable()
this.isSoon()
this.isPrivate()
this.referralUrl()

Returns users referral link.

API

Availability.badge({options})

Takes an options hash and display's a badge.

  • options.user: A user ID
  • options.container: An element to appended the badge too
  • options.href: Optional, if set the badge will link to this url, defaults to your referral url (to unset use null)
Availability.ribbon({options})

Takes an options hash and display's a ribbon.

  • options.user: A user ID
  • options.container: Optional, an element to appended the ribbon too, document.body by default
  • options.href: Optional, if set the badge will link to this url, defaults to your referral url (to unset use null)
Availability.custom({options})

Takes an options hash and runs the given render function.

Troubleshooting

My availability isn’t updating

Availability data is heavily cached, so if your availability changes, the badge will reflect the change within an hour.

availability.js's People

Contributors

larryfox avatar destroytoday avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.