jquery.tour.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue tour plugin
  18. * Optionally depends on $.totalstorage for progress checking and $.jHueNotify for error notification
  19. * Can be instantiated with
  20. $.jHueTour({
  21. tours: [ <-- array, the tours available for this page
  22. {
  23. name: "xxxx", <-- unique tour name (location.pathname scope)
  24. desc: "Desc yyyy", <-- the label shown on the question mark
  25. path: "beeswax/*", <-- string for the path to show this tour on
  26. steps: [ <-- array, steps of the tour
  27. {
  28. arrowOn: "a[href='/beeswax']", <-- the element relative to the popover is positioned
  29. expose: ".navbar-fixed-top", <-- optional, the exposed object. if not present, arrowOn will be exposed
  30. title: "Welcome to Beeswax!", <-- popover title
  31. content: "This is a tour of the Beeswax app. <br/><b>HTML</b> is supported <em>too!</em>", <-- popover content, html enable
  32. placement: "bottom", <-- popover placement
  33. left: "100px", <-- popover absolute position (css string)
  34. top: -20 <-- popover relative position (it adds that amount of pixels to the popover calculated position)
  35. visitUrl: "blabla?tour=hello" <-- overrides everything, redirects to specific url
  36. },
  37. {
  38. arrowOn: ".subnav-fixed",
  39. title: "Beeswax sections",
  40. content: "There are several sections in the Beeswax app",
  41. placement: "bottom",
  42. left: "100px"
  43. }, ...
  44. ],
  45. video: "http://player.vimeo.com/xxxxx", <-- instead of the steps you can specify a video and it will be displayed in a modal
  46. blog: "http://gethue.tumblr.com/yyyyy" <-- if specified, a link to this with a "Read more about it..." label will be placed under the video in the modal. if video is empty, the link will be automagically opened
  47. }, ...
  48. ]
  49. });
  50. Calling $.jHueTour({tours: [...]}) more than once will merge the tour data, so you can keep adding tours dynamically to the same page
  51. You can interact with:
  52. - $.jHueTour("start") / $.jHueTour("show") / $.jHueTour("play") : starts the first available tour
  53. - $.jHueTour("stop") / $.jHueTour("close") / $.jHueTour("hide") / $.jHueTour("stop") : starts the first available tour
  54. - $.jHueTour("reset") : removes stored tours and history
  55. - $.jHueTour("clear") : removes current tours
  56. - $.jHueTour("http://remote/hue/tour.hue") : loads a remote tour
  57. - $.jHueTour("tourName", 1) : loads tour name and start at step 1
  58. - $.jHueTour() : returns the available tours
  59. */
  60. (function ($, window, document, undefined) {
  61. var pluginName = "jHueTour",
  62. defaults = {
  63. labels: {
  64. AVAILABLE_TOURS: "Available tours",
  65. NO_AVAILABLE_TOURS: "None for this page",
  66. MORE_INFO: "Read more about it..."
  67. },
  68. tours: [],
  69. showRemote: false,
  70. hideIfNoneAvailable: true
  71. };
  72. function Plugin(element, options) {
  73. this.element = element;
  74. if (typeof jHueTourGlobals !== undefined) {
  75. var extendedDefaults = $.extend({}, defaults, jHueTourGlobals);
  76. extendedDefaults.labels = $.extend({}, defaults.labels, jHueTourGlobals.labels);
  77. this.options = $.extend({}, extendedDefaults, options);
  78. this.options = $.extend({}, defaults, this.options);
  79. }
  80. else {
  81. this.options = $.extend({}, defaults, options);
  82. }
  83. this._defaults = defaults;
  84. this._name = pluginName;
  85. this.currentTour = {
  86. name: "",
  87. path: "",
  88. desc: "",
  89. remote: false,
  90. steps: [],
  91. shownStep: 0,
  92. video: "",
  93. blog: ""
  94. };
  95. this.init();
  96. }
  97. Plugin.prototype.init = function () {
  98. var _this = this;
  99. _this.initQuestionMark();
  100. var _tourMask = $("<div>").attr("id", "jHueTourMask");
  101. _tourMask.width($(document).width()).height($(document).height())
  102. _tourMask.click(function () {
  103. _this.closeCurtains();
  104. });
  105. _tourMask.appendTo($("body"));
  106. $(document).on("keyup", function (e) {
  107. var _code = (e.keyCode ? e.keyCode : e.which);
  108. if ($("#jHueTourMask").is(":visible") && _code == 27) {
  109. _this.performOperation("close");
  110. }
  111. });
  112. };
  113. Plugin.prototype.initQuestionMark = function () {
  114. var _this = this;
  115. $("#jHueTourFlag").remove();
  116. var _questionMark = $("<a>").attr("id", "jHueTourFlag").html('<i title="Demo tutorials" class="icon-flag-checkered" style=""></i>');
  117. if ($.totalStorage("jHueTourExtras") != null) {
  118. var _newTours = [];
  119. $.each(_this.options.tours, function (cnt, tour) {
  120. if (tour.remote == undefined || !tour.remote) {
  121. _newTours.push(tour);
  122. }
  123. });
  124. _this.options.tours = _newTours.concat($.totalStorage("jHueTourExtras"));
  125. }
  126. var _toursHtml = '<ul class="nav nav-pills nav-stacked" style="margin-bottom: 0">'
  127. var _added = 0;
  128. $.each(_this.options.tours, function (ctn, tour) {
  129. if (tour.path === undefined || RegExp(tour.path).test(location.pathname)) {
  130. var _tourDone = '';
  131. var _removeTour = '';
  132. var _extraIcon = '';
  133. if ($.totalStorage !== undefined) {
  134. var _key = location.pathname;
  135. if (tour.path !== undefined && tour.path != "") {
  136. _key = tour.path;
  137. }
  138. _key += "_" + tour.name;
  139. if ($.totalStorage("jHueTourHistory") != null && $.totalStorage("jHueTourHistory")[_key] == true) {
  140. _tourDone = '<div style="color:green;float:right;margin:4px"><i class="icon-check-sign"></i></div>';
  141. }
  142. }
  143. if (tour.remote) {
  144. _removeTour = '<div style="color:red;float:right;margin:4px;cursor: pointer" onclick="javascript:$.jHueTour(\'remove_' + tour.name + '\')"><i class="icon-remove-sign"></i></div>';
  145. }
  146. var _link = '<a href="javascript:$.jHueTour(\'' + tour.name + '\', 1)" style="padding-left:0">';
  147. if (typeof tour.video != "undefined" && tour.video != null && tour.video != ""){
  148. _extraIcon = '<i class="icon-youtube-play"></i> ';
  149. }
  150. else if (typeof tour.blog != "undefined" && tour.blog != null && tour.blog != ""){
  151. _extraIcon = '<i class="icon-external-link"></i> ';
  152. _link = '<a href="' + tour.blog + '" target="_blank" style="padding:0">';
  153. }
  154. _toursHtml += '<li>' + _removeTour + _tourDone + _link + _extraIcon + tour.desc + '</a></li>';
  155. _added++;
  156. }
  157. });
  158. if (_added == 0) {
  159. if (_this.options.hideIfNoneAvailable){
  160. _questionMark.css("display", "none");
  161. }
  162. else {
  163. _toursHtml += '<li>' + _this.options.labels.NO_AVAILABLE_TOURS + '</li>';
  164. }
  165. }
  166. if (_added > 0 && typeof $.totalStorage !== "undefined" && ($.totalStorage("jHueTourHideModal") == null || $.totalStorage("jHueTourHideModal") == false)) {
  167. $(document).ready(function () {
  168. $("#jHueTourModal").modal();
  169. $.totalStorage("jHueTourHideModal", true);
  170. $("#jHueTourModalChk").attr("checked", "checked");
  171. $("#jHueTourModalChk").on("change", function () {
  172. $.totalStorage("jHueTourHideModal", $(this).is(":checked"));
  173. });
  174. });
  175. }
  176. if (_this.options.showRemote){
  177. _toursHtml += '<li>' +
  178. ' <div class="input-append" style="margin-top: 10px">' +
  179. ' <input id="jHueTourRemoteTutorial" style="width:70%" type="text" placeholder="URL">' +
  180. ' <button id="jHueTourRemoteTutorialBtn" class="btn" type="button" onclick="javascript:$.jHueTour($(\'#jHueTourRemoteTutorial\').val())">' +
  181. ' <i class="icon-cloud-download"></i></button>' +
  182. ' </div>' +
  183. '</li>';
  184. }
  185. _toursHtml += '</ul>';
  186. _questionMark.click(function () {
  187. var _closeBtn = $("<a>");
  188. _closeBtn.html('<i class="icon-remove"></i>').addClass("btn-mini").css("cursor", "pointer").css("margin-left", "7px").css("float", "right").css("margin-top", "-4px").css("margin-right", "-6px");
  189. _closeBtn.click(function () {
  190. $(".popover").remove();
  191. });
  192. _questionMark.popover("destroy").popover({
  193. title: _this.options.labels.AVAILABLE_TOURS,
  194. content: _toursHtml,
  195. html: true,
  196. trigger: "manual",
  197. placement: "left"
  198. }).popover("show");
  199. if ($(".popover").position().top <= 0) {
  200. $(".popover").css("top", "10px");
  201. }
  202. _closeBtn.prependTo($(".popover-title"));
  203. });
  204. _questionMark.appendTo($("#jHueTourFlagPlaceholder"));
  205. };
  206. Plugin.prototype.addTours = function (options) {
  207. var _this = this;
  208. var _addableTours = [];
  209. if (options.tours != null) {
  210. $.each(options.tours, function (cnt, tour) {
  211. var _add = true;
  212. if (_this.options.tours != null) {
  213. $.each(_this.options.tours, function (icnt, itour) {
  214. if (itour.name == tour.name) {
  215. _add = false;
  216. }
  217. });
  218. }
  219. if (_add) {
  220. _addableTours.push(tour);
  221. }
  222. });
  223. }
  224. _this.options.tours = _this.options.tours.concat(_addableTours);
  225. };
  226. Plugin.prototype.availableTours = function () {
  227. return this.options.tours;
  228. };
  229. Plugin.prototype.performOperation = function (operation) {
  230. var _this = this;
  231. var _op = operation.toLowerCase();
  232. if (_op.indexOf("http:") == 0) {
  233. $("#jHueTourRemoteTutorial").attr("disabled", "disabled");
  234. $("#jHueTourRemoteTutorialBtn").attr("disabled", "disabled");
  235. $.ajax({
  236. type: "GET",
  237. url: operation + "?callback=?",
  238. async: false,
  239. jsonpCallback: "jHueRemoteTour",
  240. contentType: "application/json",
  241. dataType: "jsonp",
  242. success: function (json) {
  243. if ($.totalStorage !== undefined) {
  244. if ($.totalStorage("jHueTourExtras") == null) {
  245. $.totalStorage("jHueTourExtras", []);
  246. }
  247. var _newStoredArray = [];
  248. if (json.tours != null) {
  249. _newStoredArray = json.tours;
  250. $.each($.totalStorage("jHueTourExtras"), function (cnt, tour) {
  251. var _found = false;
  252. $.each(json.tours, function (icnt, itour) {
  253. if (itour.name == tour.name) {
  254. _found = true;
  255. }
  256. });
  257. if (!_found) {
  258. _newStoredArray.push(tour);
  259. }
  260. });
  261. }
  262. $.totalStorage("jHueTourExtras", _newStoredArray);
  263. }
  264. $("#jHueTourFlag").popover("destroy");
  265. _this.initQuestionMark();
  266. $("#jHueTourFlag").click();
  267. },
  268. error: function (e) {
  269. $(document).trigger("error", e.message);
  270. $("#jHueTourRemoteTutorial").removeAttr("disabled");
  271. $("#jHueTourRemoteTutorialBtn").removeAttr("disabled");
  272. }
  273. });
  274. }
  275. if (_op.indexOf("remove_") == 0) {
  276. var _tourName = _op.substr(7);
  277. if ($.totalStorage !== undefined) {
  278. var _newStoredArray = [];
  279. $.each($.totalStorage("jHueTourExtras"), function (cnt, tour) {
  280. if (tour.name != _tourName) {
  281. _newStoredArray.push(tour);
  282. }
  283. });
  284. $.totalStorage("jHueTourExtras", _newStoredArray);
  285. $("#jHueTourFlag").popover("destroy");
  286. _this.initQuestionMark();
  287. $("#jHueTourFlag").click();
  288. }
  289. }
  290. if (_op == "start" || _op == "show" || _op == "play") {
  291. if (_this.options.tours.length > 0 && _this.currentTour.name == "") {
  292. _this.currentTour.name = _this.options.tours[0].name;
  293. _this.currentTour.path = _this.options.tours[0].path;
  294. _this.currentTour.steps = _this.options.tours[0].steps;
  295. _this.currentTour.desc = _this.options.tours[0].desc;
  296. _this.currentTour.video = _this.options.tours[0].video;
  297. _this.currentTour.blog = _this.options.tours[0].blog;
  298. }
  299. this.showStep(1);
  300. }
  301. if (_op == "reset") {
  302. if ($.totalStorage !== undefined) {
  303. $.totalStorage("jHueTourHistory", null);
  304. $.totalStorage("jHueTourExtras", null);
  305. }
  306. }
  307. if (_op == "clear") {
  308. _this.options.tours = [];
  309. }
  310. if (_op == "end" || _op == "hide" || _op == "close" || _op == "stop") {
  311. _this.closeCurtains();
  312. }
  313. };
  314. Plugin.prototype.closeCurtains = function () {
  315. $(".popover").remove();
  316. $(".jHueTourExposed").removeClass("jHueTourExposed");
  317. $("#jHueTourMask").hide();
  318. };
  319. Plugin.prototype.showTour = function (tourName, stepNo) {
  320. var _this = this;
  321. if (_this.options.tours != null) {
  322. $.each(_this.options.tours, function (cnt, tour) {
  323. if (tour.name == tourName && (tour.path === undefined || RegExp(tour.path).test(location.pathname))) {
  324. _this.currentTour.name = tour.name;
  325. _this.currentTour.path = tour.path;
  326. _this.currentTour.steps = tour.steps;
  327. _this.currentTour.desc = tour.desc;
  328. _this.currentTour.video = tour.video;
  329. _this.currentTour.blog = tour.blog;
  330. if (stepNo === undefined) {
  331. _this.showStep(1);
  332. }
  333. else {
  334. _this.showStep(stepNo);
  335. }
  336. return;
  337. }
  338. });
  339. }
  340. };
  341. Plugin.prototype.showStep = function (stepNo) {
  342. var _this = this;
  343. if (typeof _this.currentTour.video != "undefined" && _this.currentTour.video != null && _this.currentTour.video != "") {
  344. if ($("#jHueTourVideoPlayer").length == 0) {
  345. var _playerHTML = '<div class="modal-header">' +
  346. '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
  347. '<h3>' + _this.currentTour.desc + '</h3>' +
  348. '</div>' +
  349. '<div class="modal-body">' +
  350. '<iframe id="jHueTourVideoFrame" src="' + _this.currentTour.video + '?autoplay=1" width="700" height="350" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" style="height:360px;width:640px"></iframe><div class="moreInfo">' +
  351. (typeof _this.currentTour.blog != "undefined" && _this.currentTour.blog != "" ? '<br/><a href="' + _this.currentTour.blog + '" target="_blank"><i class="icon-external-link"></i> ' + _this.options.labels.MORE_INFO + '</a>' : '') +
  352. '</div></div>';
  353. var _player = $("<div>").attr("id", "jHueTourVideoPlayer").addClass("modal").addClass("hide").addClass("fade");
  354. _player.html(_playerHTML);
  355. _player.appendTo($("body"));
  356. }
  357. else {
  358. $("#jHueTourVideoPlayer").find("h3").html(_this.currentTour.desc);
  359. $("#jHueTourVideoPlayer").find(".moreInfo").html(typeof _this.currentTour.blog != "undefined" && _this.currentTour.blog != "" ? '<a href="' + _this.currentTour.blog + '" target="_blank"><i class="icon-external-link"></i> ' + _this.options.labels.MORE_INFO + '</a>' : '');
  360. $("#jHueTourVideoFrame").attr("src", _this.currentTour.video + "?autoplay=1");
  361. }
  362. $("#jHueTourVideoPlayer").modal().modal("show");
  363. $("#jHueTourVideoPlayer").on("hidden", function () {
  364. $("#jHueTourVideoFrame").attr("src", "about:blank");
  365. });
  366. }
  367. else {
  368. if (_this.currentTour.steps[stepNo - 1] != null) {
  369. var _step = _this.currentTour.steps[stepNo - 1];
  370. _this.currentTour.shownStep = stepNo;
  371. var _navigation = "";
  372. if (_step.visitUrl != undefined) {
  373. location.href = _step.visitUrl;
  374. }
  375. if (_step.onShown != undefined) {
  376. window.setTimeout(_step.onShown, 10);
  377. }
  378. $(".popover").remove();
  379. $(".jHueTourExposed").removeClass("jHueTourExposed");
  380. if ($(".jHueTourExposed").css("position") == "relative") {
  381. $(".jHueTourExposed").css("position", "relative");
  382. }
  383. $("#jHueTourMask").width($(document).width()).height($(document).height()).show();
  384. var _closeBtn = $("<a>");
  385. _closeBtn.addClass("btn").addClass("btn-mini").html('<i class="icon-remove"></i>').css("float", "right").css("margin-top", "-4px").css("margin-right", "-6px");
  386. _closeBtn.click(function () {
  387. _this.performOperation("close");
  388. });
  389. var _nextBtn = $("<a>");
  390. _nextBtn.addClass("btn").addClass("btn-mini").html('<i class="icon-chevron-sign-right"></i>').css("margin-top", "10px");
  391. _nextBtn.click(function () {
  392. _this.showStep(_this.currentTour.shownStep + 1);
  393. });
  394. var _prevBtn = $("<a>");
  395. _prevBtn.addClass("btn").addClass("btn-mini").html('<i class="icon-chevron-sign-left"></i>').css("margin-top", "10px").css("margin-right", "10px");
  396. _prevBtn.click(function () {
  397. _this.showStep(_this.currentTour.shownStep - 1);
  398. });
  399. var _arrowOn = _step.arrowOn;
  400. var _additionalContent = "";
  401. if ($(_arrowOn).length == 0 || !($(_arrowOn).is(":visible"))) {
  402. _arrowOn = "body";
  403. _additionalContent = "<b>MISSING POINTER OF STEP " + _this.currentTour.shownStep + "</b> ";
  404. }
  405. $(_arrowOn).popover('destroy').popover({
  406. title: _step.title,
  407. content: _additionalContent + _step.content + "<br/>",
  408. html: true,
  409. trigger: 'manual',
  410. placement: (_step.placement != "" && _step.placement != undefined) ? _step.placement : "left"
  411. }).popover('show');
  412. if (_step.top != undefined) {
  413. if ($.isNumeric(_step.top)) {
  414. $(".popover").css("top", ($(".popover").position().top + _step.top) + "px");
  415. }
  416. else {
  417. $(".popover").css("top", _step.top);
  418. }
  419. }
  420. if (_step.left != undefined) {
  421. if ($.isNumeric(_step.left)) {
  422. $(".popover").css("left", ($(".popover").position().left + _step.left) + "px");
  423. }
  424. else {
  425. $(".popover").css("left", _step.left);
  426. }
  427. }
  428. $(".popover-title").html(_step.title);
  429. _closeBtn.prependTo($(".popover-title"));
  430. if (_this.currentTour.shownStep > 1) {
  431. _prevBtn.appendTo($(".popover-content p"));
  432. }
  433. if (_this.currentTour.shownStep < _this.currentTour.steps.length && (_step.waitForAction == undefined || _step.waitForAction == false)) {
  434. _nextBtn.appendTo($(".popover-content p"));
  435. }
  436. // last step, mark tour/tutorial as done
  437. if ($.totalStorage !== undefined && _this.currentTour.shownStep == _this.currentTour.steps.length) {
  438. var _key = location.pathname;
  439. if (_this.currentTour.path !== undefined && _this.currentTour.path != "") {
  440. _key = _this.currentTour.path;
  441. }
  442. _key += "_" + _this.currentTour.name;
  443. var _history = $.totalStorage("jHueTourHistory");
  444. if (_history == null) {
  445. _history = {}
  446. }
  447. _history[_key] = true;
  448. $.totalStorage("jHueTourHistory", _history);
  449. }
  450. var _exposedElement = $((_step.expose != undefined && _step.expose != "" ? _step.expose : _arrowOn));
  451. if (_exposedElement.css("position") === undefined || _exposedElement.css("position") != "fixed") {
  452. _exposedElement.css("position", "relative");
  453. }
  454. _exposedElement.addClass("jHueTourExposed");
  455. }
  456. }
  457. };
  458. $[pluginName] = function (options, stepNo) {
  459. var _el = $("body");
  460. if (!$("body").data('plugin_' + pluginName)) {
  461. $("body").data('plugin_' + pluginName, new Plugin(_el, options));
  462. }
  463. if (options === undefined) {
  464. return $("body").data('plugin_' + pluginName).availableTours();
  465. }
  466. if (typeof options == "string") {
  467. if (stepNo === undefined) {
  468. $("body").data('plugin_' + pluginName).performOperation(options);
  469. }
  470. else if ($.isNumeric(stepNo)) {
  471. $("body").data('plugin_' + pluginName).showTour(options, stepNo);
  472. }
  473. }
  474. else if ($.isNumeric(options)) {
  475. $("body").data('plugin_' + pluginName).showStep(options);
  476. }
  477. else {
  478. $("body").data('plugin_' + pluginName).addTours(options);
  479. }
  480. };
  481. })(jQuery, window, document);