UNPKG

gd-sprest-bs

Version:

SharePoint JavaScript, TypeScript and Web Components designed using the Bootstrap framework.

192 lines (191 loc) • 7.51 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SPFxListWebPart = void 0; var gd_bs_1 = require("gd-bs"); var gd_sprest_1 = require("gd-sprest"); var base_1 = require("../base"); /** * SPFx List WebPart */ var SPFxListWebPart = function (wpProps) { var _ddl = null; var _loadFl = false; // Load the lists var loadLists = function (listId) { // Disable the dropdown _ddl.disable(); // Get the web url var webUrl = wp.Form.getValues()["WebUrl"]; // Load the lists (0, gd_sprest_1.Web)(webUrl).Lists().query({ OrderBy: ["Title"] }).execute(function (items) { var lists = []; // Set the flag _loadFl = true; // Parse the lists for (var i = 0; i < items.results.length; i++) { var list = items.results[i]; // Add the item lists.push({ data: list, text: list.Title, value: list.Id, isSelected: listId == list.Id }); } // Set the dropdown _ddl.setItems(lists); // Enable the dropdown _ddl.enable(); }); }; // Define the properties var baseProps = { // Set the form properties onConfigFormRendering: function (props) { // Set the controls props.controls = [ { name: "WebUrl", label: "Web Url", type: gd_bs_1.Components.FormControlTypes.TextField, description: "The relative url to the site containing the target list.", value: wp.Configuration ? wp.Configuration.WebUrl : null }, { name: "List", label: "List", type: gd_bs_1.Components.FormControlTypes.Dropdown, required: true, value: wp.Configuration ? wp.Configuration.ListId : null, onControlRendered: function (ctrl) { // Set the dropdown _ddl = ctrl.dropdown; } } ]; // Call the base event props = wpProps.onConfigFormRendering ? wpProps.onConfigFormRendering(props) : props; // Return the properties return props; }, // Footer rendering event onConfigFormFooterRendering: function (props) { // Add a load lists button props.tooltips.push({ content: "Loads the lists.", btnProps: { text: "Load Lists", type: gd_bs_1.Components.ButtonTypes.OutlinePrimary, onClick: function () { loadLists(); } } }); // Call the base event props = wpProps.onConfigFormFooterRendering ? wpProps.onConfigFormFooterRendering(props) : props; // Return the properties return props; }, // Modal displaying event onConfigFormDisplaying: function () { // See if the lists haven't been loaded if (_loadFl == false) { // Load the lists loadLists(wp.Configuration ? wp.Configuration.ListId : null); } // Call the base event wpProps.onConfigFormDisplaying ? wpProps.onConfigFormDisplaying() : null; }, // The configuration saving event onConfigSaving: function (cfg) { // Get the form values var values = wp.Form.getValues(); // Set the configuration var listItem = values["List"]; cfg = { ListId: listItem ? listItem.value : null, ListName: listItem ? listItem.text : null, WebUrl: values["WebUrl"] }; // Call the base event cfg = wpProps.onConfigSaving ? wpProps.onConfigSaving(cfg) : cfg; // Return the configuration return cfg; }, // The render items event renderItems: function (el, cfg) { // See if a render event exists if (wpProps.renderItems == null) { return; } // Set the list var list = (0, gd_sprest_1.Web)(cfg.WebUrl).Lists().getById(cfg.ListId); // Ensure the list exists if (cfg.ListId) { // See if we are doing a caml query if (wpProps.onListItemCAMLQuery) { // Set the CAML query var camlQuery = wpProps.onListItemCAMLQuery(cfg, ""); // Get the list items list.getItemsByQuery(camlQuery).execute( // Success function (items) { // Call the event wpProps.renderItems(el, cfg, items.results); }, function (err) { // Call the event wpProps.renderItems(el, cfg, []); }); } else { // Set the ODATA query var odata = wpProps.onListItemODataQuery ? wpProps.onListItemODataQuery(cfg, {}) : null; // See if we are making an ODATA query if (odata) { // Get the list items list.Items().query(odata).execute( // Success function (items) { // Call the event wpProps.renderItems(el, cfg, items.results); }, // Error function (err) { // Call the event wpProps.renderItems(el, cfg, []); }); } else { // Get the list items list.Items().execute( // Success function (items) { // Call the event wpProps.renderItems(el, cfg, items.results); }, // Error function (err) { // Call the event wpProps.renderItems(el, cfg, []); }); } } } } }; // Return the webpart var wp = (0, base_1.SPFxWebPart)(__assign(__assign({}, wpProps), baseProps)); return wp; }; exports.SPFxListWebPart = SPFxListWebPart;