forked from ohmage/ohmage.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ohmage.min.js
1 lines (1 loc) · 6.2 KB
/
ohmage.min.js
1
!function(e,n){"function"==typeof define&&define.amd?define(["jquery","jquery.cookie"],n):"object"==typeof exports?module.exports=n(require("jquery","jquery.cookie")):e.returnExports=n(jQuery)}(this,function(e){Ohmage=function(n,r){function t(e,n,r){var t,a,u,c,o=0;r||(r={});var s=function(){o=r.leading===!1?0:_.now(),t=null,c=e.apply(a,u),t||(a=u=null)},i=function(){var i=_.now();o||r.leading!==!1||(o=i);var l=n-(i-o);return a=this,u=arguments,0>=l||l>n?(t&&(clearTimeout(t),t=null),o=i,c=e.apply(a,u),t||(a=u=null)):t||r.trailing===!1||(t=setTimeout(s,l)),c};return i.cancel=function(){clearTimeout(t),o=0,t=a=u=null},i}function a(e){var n,r=!1;return function(){return r?n:(r=!0,n=e.apply(this,arguments),e=null,n)}}if(!n||!r)throw"app and client are required parameters.";n=n.replace(/\/+$/,"");var u=[],c={};return c.callback=function(e,n){return u.push({name:e,fun:n}),c},c.call=function(t,a,c){function o(n,r,t){t.status&&e.each(s,function(e,a){a(n,r,t)})}var s=[],c=c||function(e){return e.data},a=a||{};a.client=r,e.cookie("auth_token")&&(a.auth_token=e.cookie("auth_token"));var i={type:"POST",url:n+t,data:a,dataType:"text",xhrFields:{withCredentials:!0}};a instanceof FormData&&(i.contentType=!1,i.cache=!1,i.processData=!1);var l=e.ajax(i).then(function(n,r,a){var u=e.Deferred();if("application/json"==a.getResponseHeader("content-type")){if(!n||""==n){var s="Fail: "+t+". Ohmage returned undefined error.";o(s,-1,a),u.reject(a,r,s)}var i=e.parseJSON(n);if("success"==i.result)u.resolve(c(i),r,a);else if("failure"==i.result){var s=i.errors[0].text;o(i.errors[0].text,i.errors[0].code,a),u.reject(a,r,s)}else{var s="JSON response did not contain result attribute.";o(s,-2,a),u.reject(a,r,s)}}else u.resolve(n,r,a);return u.promise()},function(n,r,t){var a=e.Deferred();return n.status&&o("HTTP "+n.status+": "+n.responseText,-3,n),a.reject(n,r,t),a.promise()});return l.error=function(e){return s.push(function(n,r,t){return e(n,r,t),l}),l},e.each(u,function(e,n){l[n.name](n.fun)}),l},c.callform=function(n,t,a){var u=new FormData;return u.append("client",r),u.append("auth_token",e.cookie("auth_token")),e.each(t,function(e,n){u.append(e,n)}),c.call(n,u,a)},c.config={},c.user={},c["class"]={},c.campaign={},c.document={},c.registration={},c.audit={},c.survey={},c.response={},c.config.read=function(){return c.call("/config/read")},c.user.whoami=function(){return c.call("/user/whoami",{},function(e){return e.username})},c.user.auth_token=function(e){return c.call("/user/auth_token",e,function(e){return e.token})},c.login=function(e,n){return c.user.auth_token({user:e,password:n})},c.user.logout=function(){return c.call("/user/logout")},c.user.info=function(){return c.call("/user_info/read")},c.user.read=function(e){return c.call("/user/read",e)},c.user.setup=function(e){return c.call("/user/setup",e)},c.user.change_password=function(e){return c.call("/user/change_password",e)},c.user.reset_password=function(e){return c.call("/user/reset_password",e)},c.user.activate=function(e){return c.call("/user/activate",e)},c.user["delete"]=function(e){return c.call("/user/delete",e)},c.user.search=function(e){return c.call("/user/search",e)},c.user.create=function(e){return c.call("/user/create",e)},c.user.update=function(e){return c.call("/user/update",e)},c["class"].read=function(e){return c.call("/class/read",e)},c["class"].create=function(e){return c.call("/class/create",e)},c["class"]["delete"]=function(e){return c.call("/class/delete",e)},c["class"].update=function(e){return c.call("/class/update",e)},c["class"].adduser=function(e,n){return c["class"].update({class_urn:e,user_role_list_add:n})},c["class"].removeuser=function(e,n){return c["class"].update({class_urn:e,user_list_remove:n})},c["class"].search=function(e){return c.call("/class/search",e)},c.campaign.read=function(e){return e=e||{},e.output_format=e.output_format||"short",c.call("/campaign/read",e,function(e){return e.metadata.items})},c.campaign.create=function(e){return c.call("/campaign/create",e)},c.campaign.update=function(e){return c.call("/campaign/update",e)},c.campaign.addclass=function(e,n){return c.campaign.update({campaign_urn:e,class_list_add:n})},c.campaign["delete"]=function(e){return c.call("/campaign/delete",e)},c.campaign.search=function(e){return c.call("/campaign/search",e)},c.campaign.readall=function(e){return e=e||{},e.output_format=e.output_format||"short",c.call("/campaign/read",e)},c.document.create=function(e){return c.call("/document/create",e,function(e){return e.document_id})},c.document.read=function(e){return c.call("/document/read",e)},c.document.search=function(e){return c.document.read({document_name_search:e})},c.document.contents=function(e){return c.call("/document/read/contents",e)},c.document["delete"]=function(e){return c.call("/document/delete",e)},c.document.update=function(e){return c.call("/document/update",e)},c.registration.read=function(){return c.call("/registration/read")},c.user.register=function(e){return c.call("/user/register",e)},c.audit.read=function(e){return c.call("/audit/read",e,function(e){return e.audits})},c.survey.count=function(e){return data={campaign_urn:e,id:"privacy_state"},c.call("/survey_response/function/read",data)},c.response.read=function(e){return c.call("/survey_response/read",{campaign_urn:e,column_list:"urn:ohmage:special:all",output_format:"json-rows",survey_id_list:"urn:ohmage:special:all",user_list:"urn:ohmage:special:all"})},c.response.read_custom=function(e){return e=e||{},e.column_list=e.column_list||"urn:ohmage:special:all",e.output_format=e.output_format||"json-rows",e.survey_id_list=e.survey_id_list||"urn:ohmage:special:all",e.user_list=e.user_list||"urn:ohmage:special:all",c.call("/survey_response/read",e)},c.response["delete"]=function(e,n){return c.call("/survey_response/delete",{campaign_urn:e,survey_key:n})},c.response.update=function(e,n,r){return c.call("/survey_response/update",{campaign_urn:e,survey_key:n,privacy_state:r?"shared":"private"})},c.ping=t(c.user.whoami,59e3),c.keepalive=a(function(e){e=e||60,setInterval(c.ping,1e3*e)}),c.keepactive=a(function(){e("html").click(function(){c.ping()})}),c.config.read().done(function(e){console.log("This is Ohmage/"+e.application_name+" "+e.application_version+" ("+e.application_build+")")}).error(function(e){console.log("Ohmage seems offline: "+e)}),c}});