fl-builder-search.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. (function($) {
  2. var Search = {
  3. /**
  4. * Kick-off the query
  5. *
  6. * @since 2.0
  7. * @param {Object} query The query
  8. * @return {Object} The response
  9. */
  10. query: function( query ) {
  11. var data = {},
  12. kind = null,
  13. results = {
  14. library: {}
  15. },
  16. foundItems = null;
  17. query = this.normalizeQuery(query);
  18. // Reduce data by kind
  19. if ( !_.isNull( query.kind ) ) {
  20. for( var i in query.kind ) {
  21. kind = query.kind[ i ];
  22. data[ kind ] = FLBuilderConfig.contentItems[ kind ];
  23. }
  24. } else {
  25. data = FLBuilderConfig.contentItems;
  26. }
  27. foundItems = this.findMatches( query, data );
  28. results.library = this.formatResults( foundItems, query );
  29. return results;
  30. },
  31. /**
  32. * Make sure the query object is well-formed.
  33. *
  34. * @since 2.0
  35. * @param {Object} query The query
  36. * @return {Object} The query
  37. */
  38. normalizeQuery: function( query ) {
  39. var defaultQuery = {
  40. kind: null,
  41. type: null,
  42. category: null,
  43. group: null,
  44. enabled: true, /* pertains only to modules */
  45. global: null, /* pertains only to user row and module templates */
  46. searchTerm: null,
  47. categorized: false,
  48. };
  49. query = _.extend(defaultQuery, query);
  50. if ( _.isString( query.kind ) ) {
  51. query.kind = [ query.kind ];
  52. }
  53. return query;
  54. },
  55. /**
  56. * Find the objects in the dataset that match query criteria.
  57. *
  58. * @since 2.0
  59. * @param {Object} query
  60. * @param {Object} data
  61. * @return {Object}
  62. */
  63. findMatches: function( query, data ) {
  64. var foundItems = {},
  65. typeName = null,
  66. objects = null,
  67. object = null,
  68. inArray = null,
  69. matches = null,
  70. i = null;
  71. for ( typeName in data ) {
  72. objects = data[ typeName ];
  73. foundItems[ typeName ] = {
  74. items: []
  75. };
  76. for ( i in objects ) {
  77. object = objects[i];
  78. // Test for category matches.
  79. if ( ! _.isUndefined( query.category ) && ! _.isNull( query.category ) ) {
  80. if ( ! this.matchesCategory( object.category, query.category ) ) {
  81. continue;
  82. }
  83. }
  84. switch ( typeName ) {
  85. case 'template':
  86. // Content type - module | row | layout
  87. if ( ! _.isUndefined( query.content ) && ! _.isNull( query.content ) ) {
  88. inArray = _.includes( query.content, object.content );
  89. matches = query.content === object.content;
  90. if ( ! inArray && ! matches) {
  91. continue;
  92. }
  93. }
  94. // Text type matches - core | user
  95. if ( ! _.isUndefined( query.type ) && ! _.isNull( query.type ) ) {
  96. if ( query.type !== object.type ) {
  97. continue;
  98. }
  99. }
  100. if ( !_.isNull( query.group ) ) {
  101. var queryGroup = query.group,
  102. objectGroup = object.group;
  103. // Normalize group into arrays
  104. if ( _.isString( queryGroup) ) {
  105. queryGroup = [ queryGroup ];
  106. }
  107. if ( _.isString( objectGroup) ) {
  108. objectGroup = [ objectGroup ];
  109. }
  110. if ( _.isEmpty( queryGroup ) || _.isEmpty( objectGroup ) ) {
  111. continue;
  112. }
  113. var hasGroup = false;
  114. for( i in queryGroup ) {
  115. var group = queryGroup[i];
  116. if ( _.includes( objectGroup, group ) ) {
  117. hasGroup = true;
  118. }
  119. }
  120. if ( !hasGroup ) {
  121. continue;
  122. }
  123. }
  124. break;
  125. case 'module':
  126. if ( ! _.isNull( query.group ) ) {
  127. if ( query.group === false && object.group.length > 0 ) {
  128. continue;
  129. }
  130. if ( query.group !== false && ! _.includes( object.group, query.group ) ) {
  131. continue;
  132. }
  133. }
  134. break;
  135. }
  136. if ( ! _.isUndefined( query.searchTerm ) && ! _.isNull( query.searchTerm ) ) {
  137. if ( ! this.matchesSearchTerm( object, query.searchTerm ) ) {
  138. continue;
  139. }
  140. }
  141. foundItems[ typeName ].items.push( object );
  142. }
  143. }
  144. return foundItems;
  145. },
  146. /**
  147. * @since 2.0
  148. * @param {Object} objectCat
  149. * @param {Object} queryCats
  150. * @return {Boolean}
  151. */
  152. matchesCategory: function( objectCat, queryCats ) {
  153. var queryCat, i, j, cat, key, value;
  154. if ( objectCat === queryCats ) {
  155. return true;
  156. }
  157. if ( _.isString( queryCats ) ) {
  158. queryCats = [queryCats];
  159. }
  160. // Loop over multipe query categories
  161. for ( i in queryCats ) {
  162. queryCat = queryCats[ i ];
  163. if ( _.isString( objectCat ) && objectCat === queryCat ) {
  164. return true;
  165. }
  166. if ( _.isArray( objectCat ) ) {
  167. for ( j in objectCat ) {
  168. cat = objectCat[ j ];
  169. if ( cat === queryCat ) {
  170. return true;
  171. }
  172. }
  173. }
  174. if ( _.isObject( objectCat ) ) {
  175. for ( key in objectCat ) {
  176. value = objectCat[ key ];
  177. if ( value === queryCat || key === queryCat ) {
  178. return true;
  179. }
  180. }
  181. }
  182. }
  183. return false;
  184. },
  185. /**
  186. * @since 2.0
  187. * @param {Object} obj
  188. * @param {String} term
  189. * @return {Boolean}
  190. */
  191. matchesSearchTerm: function( obj, term ) {
  192. var widgetLowercase,
  193. moduleWordLowercase,
  194. termLowercase = term.toLowerCase();
  195. // Match Slug
  196. if ( !_.isUndefined( obj.slug ) && obj.slug.toLowerCase().includes( termLowercase ) ) {
  197. return true;
  198. }
  199. // Match Name
  200. if ( !_.isUndefined( obj.name ) && obj.name.toLowerCase().includes( termLowercase ) ) {
  201. return true;
  202. }
  203. // Match Category
  204. if ( _.isString(obj.category) && obj.category.toLowerCase().includes( termLowercase ) ) {
  205. return true;
  206. }
  207. // Match Description
  208. if ( !_.isUndefined( obj.description ) && obj.description.toLowerCase().includes( termLowercase ) ) {
  209. return true;
  210. }
  211. // Match Widget Base ID (slug equivalent)
  212. if ( !_.isUndefined( obj.id_base ) && obj.id_base.includes(term)) {
  213. return true;
  214. }
  215. // If term matches "Widget" or "widget"
  216. if ( obj.isWidget ) {
  217. widgetLowercase = "widget";
  218. if ( widgetLowercase.includes( termLowercase ) ) {
  219. return true;
  220. }
  221. }
  222. // if term matches "Module" or "module"
  223. if ( !_.isUndefined( obj.editor_export ) ) {
  224. moduleWordLowercase = "module";
  225. if ( moduleWordLowercase.includes( termLowercase ) ) {
  226. return true;
  227. }
  228. }
  229. return false;
  230. },
  231. /**
  232. * @since 2.0
  233. * @param {Object} foundItems
  234. * @param {Object} query
  235. * @return {Object}
  236. */
  237. formatResults: function(foundItems, query) {
  238. if (query.categorized) {
  239. for( type in foundItems) {
  240. var items = foundItems[type].items;
  241. foundItems[type].categorized = this.groupBy(items, 'category');
  242. }
  243. }
  244. return foundItems;
  245. },
  246. /**
  247. * @since 2.0
  248. * @param {Object} items
  249. * @param {String} propertyName
  250. * @return {Object}
  251. */
  252. groupBy: function(items, propertyName) {
  253. var groups = {}, propertyValue, propertyValues;
  254. _.forEach(items, function(item, i, list) {
  255. propertyValue = item[propertyName];
  256. if ( _.isNull(propertyValue) || _.isUndefined(propertyValue) ) { return; }
  257. if ( _.isString(propertyValue) && item[propertyValue] === "") { return; }
  258. // Group item by single category
  259. if (_.isString(propertyValue)) {
  260. groups[propertyValue] = groups[propertyValue] || [];
  261. groups[propertyValue].push(item);
  262. }
  263. // Group item into each category in an array
  264. if (_.isArray(propertyValue)) {
  265. propertyValues = propertyValue;
  266. _.forEach(propertyValues, function(prop, j, list) {
  267. groups[prop] = groups[prop] || [];
  268. groups[prop].push(item);
  269. });
  270. }
  271. // Group item into each value of a hash.
  272. if (_.isObject(propertyValue)) {
  273. propertyValues = propertyValue;
  274. _.forEach(propertyValues, function(value, key, list) {
  275. groups[value] = groups[value] || [];
  276. groups[value].push(item);
  277. });
  278. }
  279. });
  280. // if any group only contains blank, lose it.
  281. return groups;
  282. },
  283. /**
  284. * @since 2.0
  285. * @param String The search term
  286. * @return {Object} The response
  287. */
  288. search: function(term) {
  289. var query = {
  290. searchTerm: term
  291. }
  292. var raw = this.query(query),
  293. response = {
  294. total: 0,
  295. term: term,
  296. sections: {}
  297. };
  298. if ( !_.isUndefined(raw.library.module.items) ) {
  299. var categorized = {};
  300. for(var i in raw.library.module.items) {
  301. var item = raw.library.module.items[i],
  302. group = item.group[0],
  303. cat = item.category,
  304. name = item.name;
  305. if ( _.isUndefined(categorized[group]) ) {
  306. categorized[group] = {};
  307. }
  308. if ( _.isUndefined(categorized[group][cat]) ) {
  309. categorized[group][cat] = [];
  310. }
  311. categorized[group][cat].push(item);
  312. }
  313. response.grouped = categorized;
  314. }
  315. for( var i in raw.library) {
  316. var type = raw.library[i];
  317. if ( !_.isUndefined(type.items) && type.items.length > 0) {
  318. response.sections[i] = {
  319. name: FLBuilderStrings.typeLabels[i],
  320. handle: i,
  321. type: "",
  322. items: type.items
  323. };
  324. response.total += type.items.length;
  325. }
  326. }
  327. return response;
  328. }
  329. }
  330. /**
  331. * Public Interface
  332. */
  333. FLBuilder.Search = {
  334. /**
  335. * Find items that match query object
  336. *
  337. * @since 2.0
  338. * @param {Object} query The query
  339. * @return {Object} The response
  340. */
  341. byQuery: function(query) {
  342. return Search.query(query);
  343. },
  344. /**
  345. * Find items by search term
  346. *
  347. * @since 2.0
  348. * @param String term
  349. * @return {Object} The response
  350. */
  351. byTerm: function(term) {
  352. var response = Search.search(term);
  353. return response;
  354. }
  355. }
  356. })(jQuery);