mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add initial tour-guide feature
This commit is contained in:
360
packages/node_modules/@node-red/editor-client/src/js/ui/tour/tourGuide.js
vendored
Normal file
360
packages/node_modules/@node-red/editor-client/src/js/ui/tour/tourGuide.js
vendored
Normal file
@@ -0,0 +1,360 @@
|
||||
RED.tourGuide = (function() {
|
||||
var activeListeners = [];
|
||||
var shade;
|
||||
var focus;
|
||||
var popover;
|
||||
var stepContent;
|
||||
var targetElement;
|
||||
var fullscreen;
|
||||
|
||||
var tourCache = {};
|
||||
|
||||
function run(tourPath, done) {
|
||||
done = done || function(err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
};
|
||||
if (tourCache[tourPath]) {
|
||||
runTour(tourCache[tourPath],done);
|
||||
} else {
|
||||
import(tourPath).then(function(module) {
|
||||
tourCache[tourPath] = module.default;
|
||||
runTour(tourCache[tourPath],done);
|
||||
}).catch(function(err) {
|
||||
console.warn("Error loading tour:",err);
|
||||
done(err);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function repositionFocus() {
|
||||
if (targetElement) {
|
||||
var pos = targetElement[0].getBoundingClientRect();
|
||||
var dimension = Math.max(50, Math.max(pos.width,pos.height)*1.5);
|
||||
if (!fullscreen) {
|
||||
focus.css({
|
||||
left: (pos.left+pos.width/2)+"px",
|
||||
top: (pos.top+pos.height/2)+"px",
|
||||
width: (2*dimension)+"px",
|
||||
height: (2*dimension)+"px"
|
||||
})
|
||||
focus[0].offsetHeight; // Flush CSS changes
|
||||
focus.addClass("transition");
|
||||
focus.css({
|
||||
width: dimension+"px",
|
||||
height: dimension+"px"
|
||||
})
|
||||
} else {
|
||||
focus.css({
|
||||
left: ($(window).width()/2)+"px",
|
||||
top: ($(window).height()/2)+"px",
|
||||
width: "0px",
|
||||
height: "0px"
|
||||
})
|
||||
}
|
||||
if (popover) {
|
||||
popover.move({
|
||||
target: targetElement,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
function runTour(tour, done) {
|
||||
|
||||
shade = $('<div class="red-ui-tourGuide-shade"></div>').appendTo(document.body);
|
||||
focus = $('<div class="red-ui-tourGuide-shade-focus"></div>').appendTo(shade);
|
||||
|
||||
// var resizeTimer;
|
||||
//
|
||||
$(window).on("resize.red-ui-tourGuide", function() {
|
||||
repositionFocus();
|
||||
})
|
||||
|
||||
|
||||
|
||||
var i = 0;
|
||||
var state = {
|
||||
index: 0,
|
||||
count: tour.steps.length
|
||||
};
|
||||
|
||||
function endTour(err) {
|
||||
$(window).off("resize.red-ui-tourGuide");
|
||||
if (popover) {
|
||||
popover.close();
|
||||
}
|
||||
stepContent = null;
|
||||
popover = null;
|
||||
shade.remove();
|
||||
shade = null;
|
||||
done(err);
|
||||
}
|
||||
function runStep(carryOn) {
|
||||
if (carryOn === false) {
|
||||
endTour(false);
|
||||
return;
|
||||
}
|
||||
if (i === tour.steps.length) {
|
||||
endTour();
|
||||
return
|
||||
}
|
||||
state.index = i;
|
||||
// console.log("TOUR STEP",i+1,"OF",tour.steps.length)
|
||||
try {
|
||||
runTourStep(tour.steps[i++], state, runStep)
|
||||
} catch(err) {
|
||||
endTour(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
runStep();
|
||||
}
|
||||
|
||||
function clearListeners() {
|
||||
activeListeners.forEach(function(listener) {
|
||||
if (listener.type === "dom-event") {
|
||||
listener.target[0].removeEventListener(listener.event,listener.listener,listener.opts);
|
||||
} else if (listener.type === "nr-event") {
|
||||
RED.events.off(listener.event, listener.listener)
|
||||
}
|
||||
})
|
||||
activeListeners = [];
|
||||
}
|
||||
|
||||
function prepareStep(step, state, done) {
|
||||
if (step.prepare) {
|
||||
if (step.prepare.length === 0) {
|
||||
step.prepare.call(state);
|
||||
} else {
|
||||
step.prepare.call(state, done)
|
||||
return;
|
||||
}
|
||||
}
|
||||
done();
|
||||
}
|
||||
function completeStep(step, state, done) {
|
||||
function finish() {
|
||||
clearListeners();
|
||||
setTimeout(function() {
|
||||
done();
|
||||
},0)
|
||||
}
|
||||
if (step.complete) {
|
||||
if (step.complete.length === 0) {
|
||||
step.complete.call(state);
|
||||
} else {
|
||||
step.complete.call(state, finish)
|
||||
return;
|
||||
}
|
||||
}
|
||||
finish();
|
||||
|
||||
}
|
||||
function runTourStep(step, state, done) {
|
||||
shade.fadeIn();
|
||||
prepareStep(step, state, function() {
|
||||
var zIndex;
|
||||
var direction = step.direction || "bottom";
|
||||
fullscreen = false;
|
||||
|
||||
if (typeof step.element === "string") {
|
||||
targetElement = $(step.element)
|
||||
} else if (typeof step.element === "function") {
|
||||
targetElement = step.element.call(state);
|
||||
} else if (!step.element) {
|
||||
targetElement = $(".red-ui-editor")
|
||||
fullscreen = true;
|
||||
direction = "inset";
|
||||
} else {
|
||||
targetElement = step.element;
|
||||
}
|
||||
|
||||
if (targetElement.length === 0) {
|
||||
targetElement = null;
|
||||
shade.hide();
|
||||
throw new Error("Element not found")
|
||||
}
|
||||
|
||||
zIndex = targetElement.css("z-index");
|
||||
if (!fullscreen) {
|
||||
targetElement.css("z-index",2002);
|
||||
}
|
||||
repositionFocus();
|
||||
focus.toggleClass("disableInteraction", step.interactive === false)
|
||||
|
||||
if (!stepContent) {
|
||||
stepContent = $('<div style="position:relative"></div>');
|
||||
} else {
|
||||
stepContent.empty();
|
||||
}
|
||||
$('<button type="button" class="red-ui-button red-ui-button-small" style="float: right; margin-top: -4px; margin-right: -4px;"><i class="fa fa-times"></i></button>').appendTo(stepContent).click(function(evt) {
|
||||
evt.preventDefault();
|
||||
completeStep(step, state, function() {
|
||||
done(false);
|
||||
});
|
||||
})
|
||||
|
||||
var stepDescription = $('<div class="red-ui-tourGuide-popover-description"></div>').appendTo(stepContent);
|
||||
if (step.titleIcon) {
|
||||
$('<h2><i class="'+step.titleIcon+'"></i></h2>').appendTo(stepDescription);
|
||||
}
|
||||
if (step.title) {
|
||||
$('<h2>').text(step.title).appendTo(stepDescription);
|
||||
}
|
||||
$('<div>').css("text-align","left").html(step.description).appendTo(stepDescription);
|
||||
|
||||
var stepToolbar = $('<div>',{class:"red-ui-tourGuide-toolbar"}).appendTo(stepContent);
|
||||
|
||||
// var breadcrumbs = $('<div>',{class:"red-ui-tourGuide-breadcrumbs"}).appendTo(stepToolbar);
|
||||
// var bcStart = Math.max(0,state.index - 3);
|
||||
// var bcEnd = Math.min(state.count, bcStart + 7);
|
||||
// if (bcEnd === state.count) {
|
||||
// bcStart = Math.max(0,bcEnd - 7);
|
||||
// }
|
||||
// for (var i = bcStart; i < bcEnd; i++) {
|
||||
// var bullet = $('<i class="fa"></i>').addClass(i===state.index ? "fa-circle":"fa-circle-o").appendTo(breadcrumbs);
|
||||
// if (i === bcStart) {
|
||||
// if (i > 1) {
|
||||
// bullet.css("font-size", "3px");
|
||||
// } else if (i === 1) {
|
||||
// bullet.css("font-size", "4px");
|
||||
// }
|
||||
// } else if (i === bcStart + 1) {
|
||||
// if (i > 2) {
|
||||
// bullet.css("font-size", "4px");
|
||||
// }
|
||||
// }
|
||||
// if (i === bcEnd - 1) {
|
||||
// if (i < state.count - 2) {
|
||||
// bullet.css("font-size", "3px");
|
||||
// } else if (i === state.count - 2) {
|
||||
// bullet.css("font-size", "4px");
|
||||
// }
|
||||
// } else if (i === bcEnd - 2) {
|
||||
// if (i < state.count - 3) {
|
||||
// bullet.css("font-size", "4px");
|
||||
// }
|
||||
// }
|
||||
// // if (i === bcEnd - 1) {
|
||||
// // if (i < state.count - 2) {
|
||||
// // bullet.css("font-size", "3px");
|
||||
// // } else if (i === state.count - 2) {
|
||||
// // bullet.css("font-size", "4px");
|
||||
// // }
|
||||
// // }
|
||||
// }
|
||||
|
||||
$('<small>').text((state.index+1)+"/"+state.count).appendTo(stepToolbar)
|
||||
if (fullscreen || !step.wait) {
|
||||
var nextButton = $('<button type="button" class="red-ui-button" style="position: absolute; right:0;bottom:0;"></button>').appendTo(stepToolbar).click(function(evt) {
|
||||
evt.preventDefault();
|
||||
stepEventListener();
|
||||
});
|
||||
if (state.index === state.count - 1) {
|
||||
$('<span>close</span>').appendTo(nextButton);
|
||||
} else if (state.index === 0) {
|
||||
$('<span>start</span>').appendTo(nextButton);
|
||||
} else if (state.index < state.count-1) {
|
||||
$('<span>next <i class="fa fa-chevron-right"></i></span>').appendTo(nextButton);
|
||||
}
|
||||
}
|
||||
|
||||
var width = step.width;
|
||||
if (fullscreen) {
|
||||
width = 500;
|
||||
}
|
||||
var maxWidth = Math.max(width || 0, 300);
|
||||
if (!popover) {
|
||||
popover = RED.popover.create({
|
||||
target: targetElement,
|
||||
width: width || "auto",
|
||||
maxWidth: maxWidth+"px",
|
||||
direction: direction,
|
||||
class: "red-ui-tourGuide-popover"+(fullscreen?" ":""),
|
||||
trigger: "manual",
|
||||
content: stepContent
|
||||
}).open();
|
||||
}
|
||||
popover.element.toggleClass("red-ui-tourGuide-popover-full",!!fullscreen);
|
||||
popover.move({
|
||||
target: targetElement,
|
||||
width: width || "auto",
|
||||
maxWidth: maxWidth+"px",
|
||||
direction: direction,
|
||||
})
|
||||
|
||||
|
||||
var isSVG = targetElement[0] instanceof SVGElement;
|
||||
if (step.fallback) {
|
||||
focus.one("mouseenter", function(evt) {
|
||||
setTimeout(function() {
|
||||
focus.css({
|
||||
width: (4*dimension)+"px",
|
||||
height: (4*dimension)+"px"
|
||||
})
|
||||
shade.fadeOut();
|
||||
popover.move({
|
||||
target: $(".red-ui-editor"),
|
||||
direction: step.fallback,
|
||||
offset: 10,
|
||||
transition: true
|
||||
})
|
||||
// popover.element.addClass('red-ui-tourGuide-popover-bounce');
|
||||
},isSVG?0:500);
|
||||
})
|
||||
}
|
||||
|
||||
var stepEventListener = function() {
|
||||
focus.removeClass("transition");
|
||||
targetElement.css("z-index",zIndex);
|
||||
completeStep(step, state, done);
|
||||
}
|
||||
|
||||
if (step.wait) {
|
||||
if (step.wait.type === "dom-event") {
|
||||
var eventTarget = targetElement;
|
||||
if (step.wait.element) {
|
||||
if (typeof step.wait.element === "string") {
|
||||
eventTarget = $(step.wait.element);
|
||||
} else if (typeof step.wait.element === "function") {
|
||||
eventTarget = step.wait.element.call(state);
|
||||
}
|
||||
}
|
||||
var listener = {
|
||||
type: step.wait.type,
|
||||
target: eventTarget,
|
||||
event: step.wait.event,
|
||||
listener: function() {
|
||||
stepEventListener();
|
||||
},
|
||||
opts: { once: true }
|
||||
}
|
||||
activeListeners.push(listener)
|
||||
eventTarget[0].addEventListener(listener.event,listener.listener,listener.opts)
|
||||
} else if (step.wait.type === "nr-event") {
|
||||
var listener = {
|
||||
type: step.wait.type,
|
||||
event: step.wait.event,
|
||||
listener: function() {
|
||||
if (step.wait.filter) {
|
||||
if (!step.wait.filter.apply(state,arguments)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
stepEventListener();
|
||||
}
|
||||
}
|
||||
activeListeners.push(listener);
|
||||
RED.events.on(listener.event,listener.listener);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
run: run
|
||||
}
|
||||
|
||||
|
||||
})();
|
Reference in New Issue
Block a user