From 70eae5f1cff9ddc753d0189e43fab59a8bf41905 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 13 Jan 2015 15:59:31 -0300 Subject: [PATCH] Added a plugin to present a site tour --- plugins/site_tour/controllers/public/site_tour_plugin_public_controller.rb | 11 +++++++++++ plugins/site_tour/lib/ext/person.rb | 5 +++++ plugins/site_tour/lib/site_tour_plugin.rb | 36 ++++++++++++++++++++++++++++++++++++ plugins/site_tour/public/intro.min.js | 33 +++++++++++++++++++++++++++++++++ plugins/site_tour/public/introjs.min.css | 1 + plugins/site_tour/public/main.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/site_tour/public/style.css | 1 + plugins/site_tour/public/tour/en/tour.js.dist | 6 ++++++ plugins/site_tour/test/functional/site_tour_plugin_public_controller_test.rb | 30 ++++++++++++++++++++++++++++++ plugins/site_tour/test/test_helper.rb | 1 + plugins/site_tour/test/unit/site_tour_plugin_test.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 231 insertions(+), 0 deletions(-) create mode 100644 plugins/site_tour/controllers/public/site_tour_plugin_public_controller.rb create mode 100644 plugins/site_tour/lib/ext/person.rb create mode 100644 plugins/site_tour/lib/site_tour_plugin.rb create mode 100644 plugins/site_tour/public/intro.min.js create mode 100644 plugins/site_tour/public/introjs.min.css create mode 100644 plugins/site_tour/public/main.js create mode 120000 plugins/site_tour/public/style.css create mode 100644 plugins/site_tour/public/tour/en/tour.js.dist create mode 100644 plugins/site_tour/test/functional/site_tour_plugin_public_controller_test.rb create mode 100644 plugins/site_tour/test/test_helper.rb create mode 100644 plugins/site_tour/test/unit/site_tour_plugin_test.rb diff --git a/plugins/site_tour/controllers/public/site_tour_plugin_public_controller.rb b/plugins/site_tour/controllers/public/site_tour_plugin_public_controller.rb new file mode 100644 index 0000000..1265b8f --- /dev/null +++ b/plugins/site_tour/controllers/public/site_tour_plugin_public_controller.rb @@ -0,0 +1,11 @@ +class SiteTourPluginPublicController < PublicController + + before_filter :login_required + + def mark_action + user.site_tour_plugin_actions += [params[:action_name]].flatten + user.site_tour_plugin_actions.uniq! + render :json => {:ok => user.save} + end + +end diff --git a/plugins/site_tour/lib/ext/person.rb b/plugins/site_tour/lib/ext/person.rb new file mode 100644 index 0000000..a6af042 --- /dev/null +++ b/plugins/site_tour/lib/ext/person.rb @@ -0,0 +1,5 @@ +class Person + + settings_items :site_tour_plugin_actions, :type => Array, :default => [] + +end diff --git a/plugins/site_tour/lib/site_tour_plugin.rb b/plugins/site_tour/lib/site_tour_plugin.rb new file mode 100644 index 0000000..2710951 --- /dev/null +++ b/plugins/site_tour/lib/site_tour_plugin.rb @@ -0,0 +1,36 @@ +class SiteTourPlugin < Noosfero::Plugin + + def self.plugin_name + 'SiteTourPlugin' + end + + def self.plugin_description + _("A site tour to show users how to use the application.") + end + + def stylesheet? + true + end + + def js_files + ['intro.min.js', 'main.js'] + end + + def user_data_extras + proc do + logged_in? ? {:site_tour_plugin_actions => user.site_tour_plugin_actions}:{} + end + end + + def body_ending + proc do + tour_file = "/plugins/site_tour/tour/#{language}/tour.js" + if File.exists?(Rails.root.join("public#{tour_file}").to_s) + javascript_include_tag(tour_file) + else + "" + end + end + end + +end diff --git a/plugins/site_tour/public/intro.min.js b/plugins/site_tour/public/intro.min.js new file mode 100644 index 0000000..d95cd0f --- /dev/null +++ b/plugins/site_tour/public/intro.min.js @@ -0,0 +1,33 @@ +(function(w,p){"object"===typeof exports?p(exports):"function"===typeof define&&define.amd?define(["exports"],p):p(w)})(this,function(w){function p(a){this._targetElement=a;this._options={nextLabel:"Next →",prevLabel:"← Back",skipLabel:"Skip",doneLabel:"Done",tooltipPosition:"bottom",tooltipClass:"",highlightClass:"",exitOnEsc:!0,exitOnOverlayClick:!0,showStepNumbers:!0,keyboardNavigation:!0,showButtons:!0,showBullets:!0,showProgress:!1,scrollToElement:!0,overlayOpacity:0.8,positionPrecedence:["bottom", +"top","right","left"],disableInteraction:!1}}function J(a){var b=[],c=this;if(this._options.steps)for(var d=[],e=0,d=this._options.steps.length;ed.length)return!1;e=0;for(f=d.length;eh.width||0>l.left+l.width/2-s?(q(f,"bottom"),q(f,"top")):(l.height+l.top+p>h.height&& +q(f,"bottom"),0>l.top-p&&q(f,"top"));l.width+l.left+s>h.width&&q(f,"right");0>l.left-s&&q(f,"left");0h.height&&(c.className="introjs-arrow left-bottom",b.style.top="-"+(f-e.height-20)+"px");c.className="introjs-arrow left"; +break;case "left":!0==this._options.showStepNumbers&&(b.style.top="15px");e.top+f>h.height?(b.style.top="-"+(f-e.height-20)+"px",c.className="introjs-arrow right-bottom"):c.className="introjs-arrow right";b.style.right=e.width+20+"px";break;case "floating":c.style.display="none";a=k(b);b.style.left="50%";b.style.top="50%";b.style.marginLeft="-"+a.width/2+"px";b.style.marginTop="-"+a.height/2+"px";"undefined"!=typeof d&&null!=d&&(d.style.left="-"+(a.width/2+18)+"px",d.style.top="-"+(a.height/2+18)+ +"px");break;case "bottom-right-aligned":c.className="introjs-arrow top-right";b.style.right="0px";b.style.bottom="-"+(k(b).height+10)+"px";break;case "bottom-middle-aligned":d=k(a);a=k(b);c.className="introjs-arrow top-middle";b.style.left=d.width/2-a.width/2+"px";b.style.bottom="-"+(a.height+10)+"px";break;default:b.style.bottom="-"+(k(b).height+10)+"px",b.style.left=k(a).width/2-k(b).width/2+"px",c.className="introjs-arrow top"}}}function q(a,b){-1 a.active").className="";d.querySelector('.introjs-bullets li > a[data-stepnumber="'+ +a.step+'"]').className="active";d.querySelector(".introjs-progress .introjs-progressbar").setAttribute("style","width:"+I.call(b)+"%;");s.style.opacity=1;f&&(f.style.opacity=1);-1===r.tabIndex?l.focus():r.focus()},350)}else{var q=document.createElement("div"),m=document.createElement("div"),c=document.createElement("div"),n=document.createElement("div"),w=document.createElement("div"),D=document.createElement("div"),E=document.createElement("div"),u=document.createElement("div");q.className=e;m.className= +"introjs-tooltipReferenceLayer";t.call(b,q);t.call(b,m);this._targetElement.appendChild(q);this._targetElement.appendChild(m);c.className="introjs-arrow";w.className="introjs-tooltiptext";w.innerHTML=a.intro;D.className="introjs-bullets";!1===this._options.showBullets&&(D.style.display="none");for(var q=document.createElement("ul"),e=0,B=this._introItems.length;en||"none"!==u)g.className+=" introjs-fixParent";g=g.parentNode}M(a.element)||!0!==this._options.scrollToElement||(n=a.element.getBoundingClientRect(),g=F().height,c=n.bottom-(n.bottom-n.top),n=n.bottom-g,0>c||a.element.clientHeight>g?window.scrollBy(0,c-30):window.scrollBy(0,n+100));"undefined"!== +typeof this._introAfterChangeCallback&&this._introAfterChangeCallback.call(this,a.element)}function v(a,b){var c="";a.currentStyle?c=a.currentStyle[b]:document.defaultView&&document.defaultView.getComputedStyle&&(c=document.defaultView.getComputedStyle(a,null).getPropertyValue(b));return c&&c.toLowerCase?c.toLowerCase():c}function F(){if(void 0!=window.innerWidth)return{width:window.innerWidth,height:window.innerHeight};var a=document.documentElement;return{width:a.clientWidth,height:a.clientHeight}} +function M(a){a=a.getBoundingClientRect();return 0<=a.top&&0<=a.left&&a.bottom+80<=window.innerHeight&&a.right<=window.innerWidth}function K(a){var b=document.createElement("div"),c="",d=this;b.className="introjs-overlay";if("body"===a.tagName.toLowerCase())c+="top: 0;bottom: 0; left: 0;right: 0;position: fixed;",b.setAttribute("style",c);else{var e=k(a);e&&(c+="width: "+e.width+"px; height:"+e.height+"px; top:"+e.top+"px;left: "+e.left+"px;",b.setAttribute("style",c))}a.appendChild(b);b.onclick= +function(){!0==d._options.exitOnOverlayClick&&(y.call(d,a),void 0!=d._introExitCallback&&d._introExitCallback.call(d))};setTimeout(function(){c+="opacity: "+d._options.overlayOpacity.toString()+";";b.setAttribute("style",c)},10);return!0}function k(a){var b={};b.width=a.offsetWidth;b.height=a.offsetHeight;for(var c=0,d=0;a&&!isNaN(a.offsetLeft)&&!isNaN(a.offsetTop);)c+=a.offsetLeft,d+=a.offsetTop,a=a.offsetParent;b.top=d;b.left=c;return b}function I(){return 100*(parseInt(this._currentStep+1,10)/ +this._introItems.length)}var B=function(a){if("object"===typeof a)return new p(a);if("string"===typeof a){if(a=document.querySelector(a))return new p(a);throw Error("There is no element with given selector.");}return new p(document.body)};B.version="1.0.0";B.fn=p.prototype={clone:function(){return new p(this)},setOption:function(a,b){this._options[a]=b;return this},setOptions:function(a){var b=this._options,c={},d;for(d in b)c[d]=b[d];for(d in a)c[d]=a[d];this._options=c;return this},start:function(){J.call(this, +this._targetElement);return this},goToStep:function(a){this._currentStep=a-2;"undefined"!==typeof this._introItems&&x.call(this);return this},nextStep:function(){x.call(this);return this},previousStep:function(){C.call(this);return this},exit:function(){y.call(this,this._targetElement);return this},refresh:function(){t.call(this,document.querySelector(".introjs-helperLayer"));t.call(this,document.querySelector(".introjs-tooltipReferenceLayer"));return this},onbeforechange:function(a){if("function"=== +typeof a)this._introBeforeChangeCallback=a;else throw Error("Provided callback for onbeforechange was not a function");return this},onchange:function(a){if("function"===typeof a)this._introChangeCallback=a;else throw Error("Provided callback for onchange was not a function.");return this},onafterchange:function(a){if("function"===typeof a)this._introAfterChangeCallback=a;else throw Error("Provided callback for onafterchange was not a function");return this},oncomplete:function(a){if("function"=== +typeof a)this._introCompleteCallback=a;else throw Error("Provided callback for oncomplete was not a function.");return this},onexit:function(a){if("function"===typeof a)this._introExitCallback=a;else throw Error("Provided callback for onexit was not a function.");return this}};return w.introJs=B}); diff --git a/plugins/site_tour/public/introjs.min.css b/plugins/site_tour/public/introjs.min.css new file mode 100644 index 0000000..94afec6 --- /dev/null +++ b/plugins/site_tour/public/introjs.min.css @@ -0,0 +1 @@ +.introjs-overlay{position:absolute;z-index:999999;background-color:#000;opacity:0;background:-moz-radial-gradient(center,ellipse cover,rgba(0,0,0,0.4) 0,rgba(0,0,0,0.9) 100%);background:-webkit-gradient(radial,center center,0px,center center,100%,color-stop(0%,rgba(0,0,0,0.4)),color-stop(100%,rgba(0,0,0,0.9)));background:-webkit-radial-gradient(center,ellipse cover,rgba(0,0,0,0.4) 0,rgba(0,0,0,0.9) 100%);background:-o-radial-gradient(center,ellipse cover,rgba(0,0,0,0.4) 0,rgba(0,0,0,0.9) 100%);background:-ms-radial-gradient(center,ellipse cover,rgba(0,0,0,0.4) 0,rgba(0,0,0,0.9) 100%);background:radial-gradient(center,ellipse cover,rgba(0,0,0,0.4) 0,rgba(0,0,0,0.9) 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#66000000',endColorstr='#e6000000',GradientType=1);-ms-filter:"alpha(opacity=50)";filter:alpha(opacity=50);-webkit-transition:all .3s ease-out;-moz-transition:all .3s ease-out;-ms-transition:all .3s ease-out;-o-transition:all .3s ease-out;transition:all .3s ease-out}.introjs-fixParent{z-index:auto!important;opacity:1.0!important;position:absolute!important;-webkit-transform:none!important;-moz-transform:none!important;-ms-transform:none!important;-o-transform:none!important;transform:none!important}.introjs-showElement,tr.introjs-showElement>td,tr.introjs-showElement>th{z-index:9999999!important}.introjs-disableInteraction{z-index:99999999!important;position:absolute}.introjs-relativePosition,tr.introjs-showElement>td,tr.introjs-showElement>th{position:relative}.introjs-helperLayer{position:absolute;z-index:9999998;background-color:#FFF;background-color:rgba(255,255,255,.9);border:1px solid #777;border:1px solid rgba(0,0,0,.5);border-radius:4px;box-shadow:0 2px 15px rgba(0,0,0,.4);-webkit-transition:all .3s ease-out;-moz-transition:all .3s ease-out;-ms-transition:all .3s ease-out;-o-transition:all .3s ease-out;transition:all .3s ease-out}.introjs-tooltipReferenceLayer{position:absolute;z-index:10000000;background-color:transparent;-webkit-transition:all .3s ease-out;-moz-transition:all .3s ease-out;-ms-transition:all .3s ease-out;-o-transition:all .3s ease-out;transition:all .3s ease-out}.introjs-helperLayer *,.introjs-helperLayer *:before,.introjs-helperLayer *:after{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;-ms-box-sizing:content-box;-o-box-sizing:content-box;box-sizing:content-box}.introjs-helperNumberLayer{position:absolute;top:-16px;left:-16px;z-index:9999999999!important;padding:2px;font-family:Arial,verdana,tahoma;font-size:13px;font-weight:bold;color:white;text-align:center;text-shadow:1px 1px 1px rgba(0,0,0,.3);background:#ff3019;background:-webkit-linear-gradient(top,#ff3019 0,#cf0404 100%);background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#ff3019),color-stop(100%,#cf0404));background:-moz-linear-gradient(top,#ff3019 0,#cf0404 100%);background:-ms-linear-gradient(top,#ff3019 0,#cf0404 100%);background:-o-linear-gradient(top,#ff3019 0,#cf0404 100%);background:linear-gradient(to bottom,#ff3019 0,#cf0404 100%);width:20px;height:20px;line-height:20px;border:3px solid white;border-radius:50%;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff3019',endColorstr='#cf0404',GradientType=0);filter:progid:DXImageTransform.Microsoft.Shadow(direction=135,strength=2,color=ff0000);box-shadow:0 2px 5px rgba(0,0,0,.4)}.introjs-arrow{border:5px solid white;content:'';position:absolute}.introjs-arrow.top{top:-10px;border-top-color:transparent;border-right-color:transparent;border-bottom-color:white;border-left-color:transparent}.introjs-arrow.top-right{top:-10px;right:10px;border-top-color:transparent;border-right-color:transparent;border-bottom-color:white;border-left-color:transparent}.introjs-arrow.top-middle{top:-10px;left:50%;margin-left:-5px;border-top-color:transparent;border-right-color:transparent;border-bottom-color:white;border-left-color:transparent}.introjs-arrow.right{right:-10px;top:10px;border-top-color:transparent;border-right-color:transparent;border-bottom-color:transparent;border-left-color:white}.introjs-arrow.right-bottom{bottom:10px;right:-10px;border-top-color:transparent;border-right-color:transparent;border-bottom-color:transparent;border-left-color:white}.introjs-arrow.bottom{bottom:-10px;border-top-color:white;border-right-color:transparent;border-bottom-color:transparent;border-left-color:transparent}.introjs-arrow.left{left:-10px;top:10px;border-top-color:transparent;border-right-color:white;border-bottom-color:transparent;border-left-color:transparent}.introjs-arrow.left-bottom{left:-10px;bottom:10px;border-top-color:transparent;border-right-color:white;border-bottom-color:transparent;border-left-color:transparent}.introjs-tooltip{position:absolute;padding:10px;background-color:white;min-width:200px;max-width:300px;border-radius:3px;box-shadow:0 1px 10px rgba(0,0,0,.4);-webkit-transition:opacity .1s ease-out;-moz-transition:opacity .1s ease-out;-ms-transition:opacity .1s ease-out;-o-transition:opacity .1s ease-out;transition:opacity .1s ease-out}.introjs-tooltipbuttons{text-align:right;white-space:nowrap}.introjs-button{position:relative;overflow:visible;display:inline-block;padding:.3em .8em;border:1px solid #d4d4d4;margin:0;text-decoration:none;text-shadow:1px 1px 0 #fff;font:11px/normal sans-serif;color:#333;white-space:nowrap;cursor:pointer;outline:0;background-color:#ececec;background-image:-webkit-gradient(linear,0 0,0 100%,from(#f4f4f4),to(#ececec));background-image:-moz-linear-gradient(#f4f4f4,#ececec);background-image:-o-linear-gradient(#f4f4f4,#ececec);background-image:linear-gradient(#f4f4f4,#ececec);-webkit-background-clip:padding;-moz-background-clip:padding;-o-background-clip:padding-box;-webkit-border-radius:.2em;-moz-border-radius:.2em;border-radius:.2em;zoom:1;*display:inline;margin-top:10px}.introjs-button:hover{border-color:#bcbcbc;text-decoration:none;box-shadow:0 1px 1px #e3e3e3}.introjs-button:focus,.introjs-button:active{background-image:-webkit-gradient(linear,0 0,0 100%,from(#ececec),to(#f4f4f4));background-image:-moz-linear-gradient(#ececec,#f4f4f4);background-image:-o-linear-gradient(#ececec,#f4f4f4);background-image:linear-gradient(#ececec,#f4f4f4)}.introjs-button::-moz-focus-inner{padding:0;border:0}.introjs-skipbutton{margin-right:5px;color:#7a7a7a}.introjs-prevbutton{-webkit-border-radius:.2em 0 0 .2em;-moz-border-radius:.2em 0 0 .2em;border-radius:.2em 0 0 .2em;border-right:0}.introjs-nextbutton{-webkit-border-radius:0 .2em .2em 0;-moz-border-radius:0 .2em .2em 0;border-radius:0 .2em .2em 0}.introjs-disabled,.introjs-disabled:hover,.introjs-disabled:focus{color:#9a9a9a;border-color:#d4d4d4;box-shadow:none;cursor:default;background-color:#f4f4f4;background-image:none;text-decoration:none}.introjs-bullets{text-align:center}.introjs-bullets ul{clear:both;margin:15px auto 0;padding:0;display:inline-block}.introjs-bullets ul li{list-style:none;float:left;margin:0 2px}.introjs-bullets ul li a{display:block;width:6px;height:6px;background:#ccc;border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px;text-decoration:none}.introjs-bullets ul li a:hover{background:#999}.introjs-bullets ul li a.active{background:#999}.introjs-progress{overflow:hidden;height:10px;margin:10px 0 5px 0;border-radius:4px;background-color:#ecf0f1}.introjs-progressbar{float:left;width:0;height:100%;font-size:10px;line-height:10px;text-align:center;background-color:#08c}.introjsFloatingElement{position:absolute;height:0;width:0;left:50%;top:50%} \ No newline at end of file diff --git a/plugins/site_tour/public/main.js b/plugins/site_tour/public/main.js new file mode 100644 index 0000000..18b96ed --- /dev/null +++ b/plugins/site_tour/public/main.js @@ -0,0 +1,67 @@ +var siteTourPlugin = (function() { + + var actions = []; + var userData = {}; + + function hasMark(name) { + return jQuery.cookie("_noosfero_.sitetour." + name) || + jQuery.inArray(name, userData.site_tour_plugin_actions)>=0; + } + + function mark(name) { + jQuery.cookie("_noosfero_.sitetour." + name, 1); + if(userData.login) { + jQuery.post('/plugin/site_tour/public/mark_action', {action_name: name}, function(data) { }); + } + } + + function clearAll() { + jQuery('.site-tour-plugin').removeAttr('data-intro data-intro-name data-step'); + } + + function configureIntro(force) { + clearAll(); + for(var i=0; i 'test' + assert_response 401 + end + + should 'be able to mark one action' do + login_as(person.identifier) + xhr :post, :mark_action, :action_name => 'test' + assert_equal({'ok' => true}, ActiveSupport::JSON.decode(response.body)) + assert_equal ['test'], person.reload.site_tour_plugin_actions + end + + should 'be able to mark multiple actions' do + login_as(person.identifier) + xhr :post, :mark_action, :action_name => ['test1', 'test2'] + assert_equal({'ok' => true}, ActiveSupport::JSON.decode(response.body)) + assert_equal ['test1', 'test2'], person.reload.site_tour_plugin_actions + end + +end diff --git a/plugins/site_tour/test/test_helper.rb b/plugins/site_tour/test/test_helper.rb new file mode 100644 index 0000000..cca1fd3 --- /dev/null +++ b/plugins/site_tour/test/test_helper.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + '/../../../test/test_helper' diff --git a/plugins/site_tour/test/unit/site_tour_plugin_test.rb b/plugins/site_tour/test/unit/site_tour_plugin_test.rb new file mode 100644 index 0000000..2f0222c --- /dev/null +++ b/plugins/site_tour/test/unit/site_tour_plugin_test.rb @@ -0,0 +1,40 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class SiteTourPluginTest < ActiveSupport::TestCase + + def setup + @plugin = SiteTourPlugin.new + end + + attr_accessor :plugin + + should 'include site tour plugin actions in user data for logged in users' do + expects(:logged_in?).returns(true) + person = create_user('testuser').person + person.site_tour_plugin_actions = ['login', 'navigation'] + expects(:user).returns(person) + + assert_equal({:site_tour_plugin_actions => ['login', 'navigation']}, instance_eval(&plugin.user_data_extras)) + end + + should 'return empty hash when user is not logged in' do + expects(:logged_in?).returns(false) + assert_equal({}, instance_eval(&plugin.user_data_extras)) + end + + should 'include javascript related to tour instructions if file exists' do + file = '/plugins/site_tour/tour/pt/tour.js' + expects(:language).returns('pt') + File.expects(:exists?).with(Rails.root.join("public#{file}").to_s).returns(true) + expects(:javascript_include_tag).with(file) + instance_exec(&plugin.body_ending) + end + + should 'not include javascript file do not exists' do + file = '/plugins/site_tour/tour/pt/tour.js' + expects(:language).returns('pt') + File.expects(:exists?).with(Rails.root.join("public#{file}").to_s).returns(false) + assert_equal "", instance_exec(&plugin.body_ending) + end + +end -- libgit2 0.21.2