Created
November 27, 2012 20:53
-
-
Save jwerle/4156937 to your computer and use it in GitHub Desktop.
Get Satisfaction quick implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The Daily Voice | |
* Copyright 2012 Daily Voice (www.DailyVoice.com) | |
* | |
* Licensed 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. | |
*/ | |
/** | |
@namespace daily | |
**/ | |
var daily = window.daily || {}; | |
/** | |
@module GetSatisfaction | |
**/ | |
;(function(root, $){ | |
// js-lint | |
"use strict"; | |
/** | |
@namespace GetSatisfaction namespace | |
**/ | |
root.GetSatisfaction = new EventObject(); | |
/** | |
GSFN Widget ID | |
@property GS_ID | |
@public | |
@type {Number} | |
**/ | |
root.GetSatisfaction.GS_ID = 1234; | |
/** | |
GSFN Widget JavaScript Source URL | |
@property GS_SOURCE | |
@public | |
@type {String} | |
**/ | |
root.GetSatisfaction.GS_SOURCE = "https://loader.engage.gsfn.us/loader.js"; | |
/** | |
GSFN Widget DOM Element Container ID | |
@property GS_CONTAINER_ID | |
@public | |
@type {String} | |
**/ | |
root.GetSatisfaction.GS_CONTAINER_ID = ['getsat', 'widget', root.GetSatisfaction.GS_ID].join('-'); | |
/** | |
Loads the GetSatisfaction namespace | |
@name GetSatisfaction.load | |
@public | |
@function | |
@param {Boolean} deferred A flag to defer the request object to the return for chainability within the jQuery.Ajax object. | |
@param {Boolean} preventRender If true then the widget will not be loaded and rendered to the DOM. | |
@event gfsn.parse.before - Emitted when the before parsing the load request | |
@event gfsn.parse.before - Emitted when the after parsing the load request | |
@event gfsn.load.before - Emitted when before the request to load GSFN is made | |
@event gfsn.load.complete - Emitted when the request made for GSFN is complete | |
@event gfsn.load.success - Emitted when the GFSN namespace has been loaded | |
@event gfsn.load.error - Emitted when an error occurs during the load of GFSN | |
@event gfsn.container.render - Emitted when the GSFN container is rendered | |
@event gfsn.widget.load - Emitted when a widget has been loaded | |
@return {Object} Either a reference to this or the jQuery.Ajax jQuery.Deferred Object. | |
**/ | |
root.GetSatisfaction.load = function(deferred, preventRender){ | |
var self = this, $body, $container, request | |
, id, source, containerId, GSError, trace | |
trace = function(msg) { | |
msg = (typeof msg === 'object' ? | |
(msg.length? | |
msg.join('') : | |
msg.toString()): | |
msg); | |
return log(['[GSFN] ->', msg].join(' ')) | |
}; | |
GSError = this.GetSatisfactionError | |
deferred = deferred || {}; | |
id = (typeof deferred === 'object' && deferred.id? | |
deferred.id : | |
root.GetSatisfaction.GS_ID); | |
source = (typeof deferred === 'object' && deferred.source? | |
deferred.source : | |
root.GetSatisfaction.GS_SOURCE); | |
containerId = (typeof deferred === 'object' && deferred.containerId? | |
deferred.containerId : | |
root.GetSatisfaction.GS_CONTAINER_ID); | |
if (! id) { | |
throw new GSError("Coldn't determine GetSatisfaction ID"); | |
} | |
if (! source) { | |
throw new GSError("Coldn't determine GetSatisfaction source URL"); | |
} | |
if (! containerId) { | |
throw new GSError("Coldn't determine GetSatisfaction container ID"); | |
} | |
if (! mobileUser) { | |
$body = $('body'); | |
$container = $('<div class="button anchor-right helpfeedback"/>'); | |
$container.attr({ | |
'id' : id, | |
'title' : "Help & Feedback" | |
}); | |
// Trigger parse.before event | |
self.trigger('gsfn.parse.before'); | |
// Trace some stuff | |
trace("Loading..."); | |
trace(['id: ', '"'+ id + '"']); | |
trace(['source: ', '"'+ source + '"']); | |
trace(['containerId: ', '"'+ containerId + '"']); | |
// Make and cache the request object | |
request = $.ajax({ | |
url : source, | |
async : true, | |
cache : false, | |
dataType : 'script', | |
beforeSend: function(){ | |
trace("Preparing to load GetSatisfaction.."); | |
// Trigger load.before event | |
self.trigger('gsfn.load.before'); | |
}, | |
complete : function(){ | |
trace("GetSatisfaction loaded.."); | |
// Trigger load.complete event | |
self.trigger('gsfn.load.complete'); | |
if (! $('#' + containerId).length) { | |
trace("GetSatisfaction container not found.. Creating.."); | |
$body.append($container); | |
// Trigger container.render event | |
self.trigger('gsfn.container.render'); | |
} | |
}, | |
success : function(){ | |
trace("GetSatisfaction namespace loaded.."); | |
// Attach the GSFN namespace | |
root.GetSatisfaction.ns = GSFN; | |
// Trigger container.render event | |
self.trigger('gsfn.load.success'); | |
// Grab the widget | |
GSFN.loadWidget(id, { | |
containerId : containerId | |
}); | |
setTimeout(function(){ | |
trace("GetSatisfaction widget loaded.."); | |
// Trigger widget.load event | |
self.trigger('gsfn.widget.load'); | |
}, 100); // Artificial timing.. | |
}, | |
error : function(){ | |
trace("GetSatisfaction error.."); | |
// Trigger beforeLoad event | |
self.trigger('gsfn.load.error'); | |
throw new GSError("Something went wrong loading GetSatisfaction!"); | |
} | |
}); | |
// Trigger parse.after event | |
self.trigger('gsfn.parse.after'); | |
if (deferred === true) { | |
// Trigger container.render event | |
self.trigger('gsfn.load.deferred'); | |
// Deferred for possible overrides | |
return request; | |
} | |
} | |
// Keep piping the chain | |
return this; | |
}; | |
/** | |
Constructs a new instance of a GetSatisfactionError | |
@name GetSatisfaction.GetSatisfactionError | |
@public | |
@constructor | |
@param {String} message A message to output with the error object throw to the buffer. | |
**/ | |
function GetSatisfactionError(message) { | |
Error.call(this) | |
this.message = message; | |
} | |
// Inherit the Error prototype | |
GetSatisfactionError = Base.inherits(Error); | |
// Override the name property | |
GetSatisfactionError.prototype.name = "GetSatisfactionError"; | |
// Attach for global reference | |
root.GetSatisfaction.GetSatisfactionError = GetSatisfactionError; | |
if ( typeof define === "function" && define.amd && define.amd ) { | |
define("gsfn", function () { return root.GetSatisfaction; } ); | |
} | |
})(daily, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Many deps are needed, but you should get the gist of it