angular-calendar
Version:
A calendar component for angular 20.2+ that can display events on a month, week or day view
234 lines (233 loc) • 13.5 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
var schematics_1 = require("@angular-devkit/schematics");
var tasks_1 = require("@angular-devkit/schematics/tasks");
var ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
var ast_utils_1 = require("@schematics/angular/utility/ast-utils");
var change_1 = require("@schematics/angular/utility/change");
var workspace_1 = require("@schematics/angular/utility/workspace");
var dependencies_1 = require("@schematics/angular/utility/dependencies");
var core_1 = require("@angular-devkit/core");
var utils_1 = require("../utils");
var version_names_1 = require("./version-names");
function default_1(options) {
// Determine which setup strategy to use
var setupStrategy = getSetupStrategy(options);
return (0, schematics_1.chain)([
addPackageJsonDependencies(options),
installPackageJsonDependencies(),
setupStrategy,
addAngularCalendarStyle(options),
]);
}
function getSetupStrategy(options) {
return options.standalone
? addToStandaloneComponent(options)
: addToNgModule(options);
}
function installPackageJsonDependencies() {
return function (host, context) {
context.addTask(new tasks_1.NodePackageInstallTask());
context.logger.log('info', "Installing angular calendar dependencies...");
return host;
};
}
function addPackageJsonDependencies(options) {
return function (host, context) {
var dateAdapters = {
moment: version_names_1.momentVersion,
'date-fns': version_names_1.dateFnsVersion,
};
var angularCalendarDependency = nodeDependencyFactory('angular-calendar', version_names_1.angularCalendarVersion);
var dateAdapterLibrary = options.dateAdapter;
var dateAdapterLibraryDependency = nodeDependencyFactory(dateAdapterLibrary, dateAdapters[dateAdapterLibrary]);
var angularDraggableDroppableDependency = nodeDependencyFactory('angular-draggable-droppable', version_names_1.angularDraggableDroppableVersion);
var angularResizableElementDependency = nodeDependencyFactory('angular-resizable-element', version_names_1.angularResizableElementVersion);
(0, dependencies_1.addPackageJsonDependency)(host, angularCalendarDependency);
context.logger.log('info', "Added \"".concat(angularCalendarDependency.name, "\" into ").concat(angularCalendarDependency.type));
(0, dependencies_1.addPackageJsonDependency)(host, dateAdapterLibraryDependency);
context.logger.log('info', "Added \"".concat(dateAdapterLibraryDependency.name, "\" into ").concat(dateAdapterLibraryDependency.type));
(0, dependencies_1.addPackageJsonDependency)(host, angularDraggableDroppableDependency);
context.logger.log('info', "Added \"".concat(angularDraggableDroppableDependency.name, "\" into ").concat(angularDraggableDroppableDependency.type));
(0, dependencies_1.addPackageJsonDependency)(host, angularResizableElementDependency);
context.logger.log('info', "Added \"".concat(angularResizableElementDependency.name, "\" into ").concat(angularResizableElementDependency.type));
return host;
};
}
function nodeDependencyFactory(packageName, version) {
return {
type: dependencies_1.NodeDependencyType.Default,
name: packageName,
version: version,
overwrite: true,
};
}
function getCalendarImports() {
return [
'CalendarPreviousViewDirective',
'CalendarTodayDirective',
'CalendarNextViewDirective',
'CalendarMonthViewComponent',
'CalendarWeekViewComponent',
'CalendarDayViewComponent',
'CalendarDatePipe',
];
}
function getProviderFunction(options) {
return "provideCalendar({\n provide: DateAdapter,\n useFactory: ".concat(options.dateAdapter === 'moment' ? 'momentAdapterFactory' : 'adapterFactory', ",\n })");
}
function addToStandaloneComponent(options) {
var _this = this;
return function (host, context) { return __awaiter(_this, void 0, void 0, function () {
var workspace, project, mainPath, componentPath, componentSource, standalone, updates, importsChanges, providersChanges, recorder;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
context.logger.log('info', "Adding calendar imports and providers to standalone component...");
return [4 /*yield*/, (0, workspace_1.getWorkspace)(host)];
case 1:
workspace = _a.sent();
project = (0, utils_1.getProjectFromWorkspace)(workspace, options.projectName);
mainPath = (0, utils_1.getProjectMainFile)(project);
if (options.installToPath) {
// User specified a component path
componentPath = (0, core_1.normalize)(project.root + '/' + options.installToPath);
}
else {
// Use default app component
componentPath = getAppComponentPath(host, mainPath, project);
}
componentSource = (0, utils_1.getSourceFile)(host, componentPath);
standalone = (0, utils_1.isStandaloneComponent)(componentSource);
if (standalone === false) {
throw new schematics_1.SchematicsException("Component at ".concat(componentPath, " has standalone: false. Please use a standalone component or select the NgModule option."));
}
updates = getImportInserts(componentSource, componentPath, options.dateAdapter);
importsChanges = (0, utils_1.addImportsToComponent)(componentSource, componentPath, getCalendarImports());
providersChanges = (0, utils_1.addProvidersToComponent)(componentSource, componentPath, [getProviderFunction(options)]);
recorder = host.beginUpdate(componentPath);
__spreadArray(__spreadArray(__spreadArray([], updates, true), importsChanges, true), providersChanges, true).forEach(function (update) {
if (update instanceof change_1.InsertChange) {
recorder.insertLeft(update.pos, update.toAdd);
}
});
host.commitUpdate(recorder);
return [2 /*return*/, (0, schematics_1.mergeWith)((0, schematics_1.source)(host))];
}
});
}); };
}
/**
* Get the path to the default app component
*/
function getAppComponentPath(host, mainPath, project) {
// Try common app component paths (both old and new Angular naming conventions)
var possiblePaths = [
"".concat(project.root, "/src/app/app.component.ts"), // Traditional naming
"".concat(project.root, "/src/app/app.ts"), // Angular 17+ standalone naming
"".concat(project.root, "/src/app/app-component.ts"),
];
for (var _i = 0, possiblePaths_1 = possiblePaths; _i < possiblePaths_1.length; _i++) {
var path = possiblePaths_1[_i];
if (host.exists(path)) {
return path;
}
}
// Return first possible path as fallback (will be checked for existence elsewhere)
return possiblePaths[0];
}
function addToNgModule(options) {
var _this = this;
return function (host, context) { return __awaiter(_this, void 0, void 0, function () {
var workspace, project, mainPath, appModulePath, moduleSource, updates, importsChanges, providersChanges, recorder;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
context.logger.log('info', "Adding calendar components and providers to NgModule...");
return [4 /*yield*/, (0, workspace_1.getWorkspace)(host)];
case 1:
workspace = _a.sent();
project = (0, utils_1.getProjectFromWorkspace)(workspace, options.projectName);
mainPath = (0, utils_1.getProjectMainFile)(project);
appModulePath = options.installToPath
? (0, core_1.normalize)(project.root + '/' + options.installToPath)
: (0, ng_ast_utils_1.getAppModulePath)(host, mainPath);
moduleSource = (0, utils_1.getSourceFile)(host, appModulePath);
updates = getImportInserts(moduleSource, appModulePath, options.dateAdapter);
importsChanges = (0, utils_1.addImportsToNgModule)(moduleSource, appModulePath, getCalendarImports());
providersChanges = (0, utils_1.addProvidersToNgModule)(moduleSource, appModulePath, [getProviderFunction(options)]);
recorder = host.beginUpdate(appModulePath);
__spreadArray(__spreadArray(__spreadArray([], updates, true), importsChanges, true), providersChanges, true).forEach(function (update) {
if (update instanceof change_1.InsertChange) {
recorder.insertLeft(update.pos, update.toAdd);
}
});
host.commitUpdate(recorder);
return [2 /*return*/, (0, schematics_1.mergeWith)((0, schematics_1.source)(host))];
}
});
}); };
}
function addAngularCalendarStyle(options) {
var libStylePath = 'node_modules/angular-calendar/css/angular-calendar.css';
return (0, utils_1.addStyle)(libStylePath, options.projectName);
}
function getImportInserts(sourceFile, filePath, dateAdapter) {
// Add imports to the NgModule / component
var updates = [
// Add import statements
(0, ast_utils_1.insertImport)(sourceFile, filePath, "".concat(getCalendarImports().join(', '), ", DateAdapter, provideCalendar"), 'angular-calendar'),
(0, ast_utils_1.insertImport)(sourceFile, filePath, 'adapterFactory', "angular-calendar/date-adapters/".concat(dateAdapter)),
];
if (dateAdapter === 'moment') {
updates.push((0, utils_1.insertWildcardImport)(sourceFile, filePath, 'moment', 'moment'));
updates.push((0, utils_1.insertAfterImports)(sourceFile, filePath, ";\n\nexport function momentAdapterFactory() {\n return adapterFactory(moment);\n}"));
}
return updates;
}