diff --git a/public/custom-fields.js b/public/custom-fields.js index fef5b3c43..bbe617971 100644 --- a/public/custom-fields.js +++ b/public/custom-fields.js @@ -21,7 +21,7 @@ var FriendlyURLFormField = function () { } /// The partial HTML template that represents your custom field. Your ViewModel will be automatically bound to this template. - self.Template = 'https://agility.github.io/CustomFields/friendly-url/html/friendly-url-template.html'; + self.Template = 'https://agility.github.io/CustomFields/friendly-url/html/friendly-url-template.html'; /// type="Array">The Javscript dependencies that must be loaded before your ViewModel is bound. They will be loaded in the order you specify. self.DependenciesJS = []; @@ -251,143 +251,169 @@ var ChooseProductCustomField = function () { $pnl = $(".product-picker-field", options.$elem); + var websiteName = ContentManager.ViewModels.Navigation.currentWebsite() + var searchAPIUrl = "" + ContentManager.DashboardDataAccess.GetAllDigitalChannelsWithDomains(websiteName, + function (channels) { - //bind our viewmodel to this - var viewModel = function () { + for (var channel in channels) { - /// 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; - - ContentManager.DataAccess.GetPreviewUrl() - - self.ajaxRequest = null; - - self.selectedValue = options.fieldBinding.extend({ throttle: 500 }); - - self.formatResult = function (item) { - - return $(`
${item.name}
`); - //return item.node.title; - }; - - - self.formatSelection = function (item) { - - return $(`
${item.name}
`); - - }; - 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) { - - return JSON.stringify(obj) - }, - - ajax: { // instead of writing the function to execute the request we use Select2's convenient helper - url: "https://nextjs-commerce-agility-cms.vercel.app/api/search-products", - //url: "http://localhost:3000/api/search-products", - dataType: 'json', - type: "get", - quietMillis: 250, - - originalValue: ko.unwrap(options.fieldBinding), - term: "", - data: function (term, page, params) { - return { - search: term, // search term - }; - }, - results: function (data, page) { - - return { - results: data - }; - }, - current: function (data) { - console.log("CURRENT", data) - }, - cache: true - }, - initSelection: function (element, callback) { - //use the hidden "product name" field - var json = ko.unwrap(options.fieldBinding); - - if (json && json.length > 0) { - var node = JSON.parse(json) - 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", - change: function(e) { - - if (e.added) { - var obj = e.added - //set the title and the description if we have them - if (options.contentItem.Values.Title) { - options.contentItem.Values.Title(obj.name) - } - if (options.contentItem.Values.Description) { - - if (obj.description.indexOf("/>") != -1) { - obj.description = $(obj.description).text() + var channelObj = channels[channel] + for (var i = 0; i < channelObj.length; i++) { + var domain = channelObj[i] + if (domain.IsPreviewDomain) { + //calculate the url to search for products by using the preview url... + var searchUrl = domain.DomainUrl + if (searchUrl.lastIndexOf("/") == searchUrl.length - 1) { + searchUrl = searchUrl.substring(0, searchUrl.length - 1) } - options.contentItem.Values.Description(obj.description) + searchAPIUrl = searchUrl + "/api/search-products" + break; } - - if (options.contentItem.Values.CTA) { - var productUrl = "~/product" + obj.slug - var cta = "Buy Now" - options.contentItem.Values.CTA(cta) - } - } } - }; - } - ko.applyBindings(viewModel, $pnl.get(0)); + + + + //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.name}
`); + //return item.node.title; + }; + + + self.formatSelection = function (item) { + + return $(`
${item.name}
`); + + }; + 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) { + + return JSON.stringify(obj) + }, + + ajax: { // instead of writing the function to execute the request we use Select2's convenient helper + url: searchAPIUrl, + dataType: 'json', + type: "get", + quietMillis: 250, + + originalValue: ko.unwrap(options.fieldBinding), + term: "", + data: function (term, page, params) { + return { + search: term, // search term + }; + }, + results: function (data, page) { + + return { + results: data + }; + }, + current: function (data) { + console.log("CURRENT", data) + }, + cache: true + }, + initSelection: function (element, callback) { + //use the hidden "product name" field + var json = ko.unwrap(options.fieldBinding); + + if (json && json.length > 0) { + var node = JSON.parse(json) + 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", + change: function (e) { + + if (e.added) { + var obj = e.added + //set the title and the description if we have them + if (options.contentItem.Values.Title) { + options.contentItem.Values.Title(obj.name) + } + if (options.contentItem.Values.Description) { + + if (obj.description.indexOf("/>") != -1) { + obj.description = $(obj.description).text() + } + + options.contentItem.Values.Description(obj.description) + } + + if (options.contentItem.Values.CTA) { + var productUrl = "~/product" + obj.slug + var cta = "Buy Now" + options.contentItem.Values.CTA(cta) + } + + } + } + }; + } + + ko.applyBindings(viewModel, $pnl.get(0)); + }) } }