// // API Item Picker // var baseAPIUrl = "http://localhost:3000" var ChooseProductCustomField = function () { /// The type definition of this Agility Custom Field Type. var self = this; /// The display name of the Custom Field Type self.Label = "Choose Product"; /// The internal reference name of the Custom Field Type. Must not contain any special characters. self.ReferenceName = "ChooseProduct"; /// This function runs every time the field is rendered self.Render = function (options) { /// /// The Render handler for this field. Create any elements and bindings that you might need, pull down resources. /// This method will be called everytime to the field value changes. /// /// The options used to render this field. //get our base element var $pnl = $(".product-picker-field", options.$elem); if ($pnl.size() == 0) { var htmlContent = ` `; //pull down the html template and load it into the element options.$elem.append(htmlContent) $pnl = $(".product-picker-field", options.$elem); //bind our viewmodel to this var viewModel = function () { /// The KO ViewModel that will be binded to your HTML template. /// /// The .field-row jQuery Dom Element. /// The entire Content Item object including Values and their KO Observable properties of all other fields on the form. /// The value binding of thie Custom Field Type. Get and set this field's value by using this property. /// Object representing the field's settings such as 'Hidden', 'Label', and 'Description' /// Represents if this field should be readonly or not. /// var self = this; self.ajaxRequest = null; self.selectedValue = options.fieldBinding.extend({ throttle: 500 }); self.formatResult = function (item) { return $(`
${item.node.title}
`); //return item.node.title; }; self.formatSelection = function (item) { return $(`
${item.node.title}
`); }; self.ajaxRequest = null; self.select2 = { label: 'Product', readOnly: false, value: options.fieldBinding, multiple: false, maximumSelectionSize: 1, minimumInputLength: 0, placeholder: 'Find product...', formatResult: self.formatResult, formatSelection: self.formatSelection, templateResult: self.templateResult, matcher: function (term, text) { return true; }, id: function (obj) { //set content of the Agility CMS Content Item //options.contentItem.Values.ExternalID(obj.ID) //options.contentItem.Values.MyField1(obj.Value1) //options.contentItem.Values.MyField2(obj.Value2) //etc... //save the whole thing as JSON return JSON.stringify(obj.node) }, ajax: { // instead of writing the function to execute the request we use Select2's convenient helper url: `${baseAPIUrl}/api/search-products`, dataType: 'json', type: "get", quietMillis: 250, originalValue: ko.unwrap(options.fieldBinding), term: "", data: function (term, page, params) { return { filter: term, // search term }; }, results: function (data, page) { return { results: data }; }, current: function (data) { }, cache: true }, initSelection: function (element, callback) { //use the hidden "product name" field var json = ko.unwrap(options.fieldBinding); console.log({ json }) if (json && json.length > 0) { var node = JSON.parse(json) console.log({ node }) callback({ node }) } // console.log(val) // var label = ko.unwrap(options.contentItem.Values.ProductName); // if (val && label) { // var data = { // node: { // id: val, // title: label // } // }; // callback(data); // } }, allowClear: false, dropdownCssClass: "bigdrop" }; } ko.applyBindings(viewModel, $pnl.get(0)); } } } ContentManager.Global.CustomInputFormFields.push(new ChooseProductCustomField());