Browse Source

Shop, products attach search popup dialog

Vova Tkach 5 years ago
parent
commit
d91e569451

+ 90 - 4
assets/cp.scripts.js

@@ -3718,7 +3718,7 @@
 	/**
 	/**
 	 * Checks if a side of an element is scrolled past a side of its parents
 	 * Checks if a side of an element is scrolled past a side of its parents
 	 * @param  {HTMLElement}  el           The element who's side being scrolled out of view is in question
 	 * @param  {HTMLElement}  el           The element who's side being scrolled out of view is in question
-	 * @param  {[DOMRect]}    rect         Optional rect of `el` to use
+	 * @param  {[DOMRect]}    rect         Optional rect of el to use
 	 * @param  {String}       elSide       Side of the element in question ('top', 'left', 'right', 'bottom')
 	 * @param  {String}       elSide       Side of the element in question ('top', 'left', 'right', 'bottom')
 	 * @param  {String}       parentSide   Side of the parent in question ('top', 'left', 'right', 'bottom')
 	 * @param  {String}       parentSide   Side of the parent in question ('top', 'left', 'right', 'bottom')
 	 * @return {HTMLElement}               The parent scroll element that the el's side is scrolled past, or null if there is no such element
 	 * @return {HTMLElement}               The parent scroll element that the el's side is scrolled past, or null if there is no such element
@@ -4659,7 +4659,7 @@
 			}
 			}
 
 
 			if (lastDownEl === target) {
 			if (lastDownEl === target) {
-				// Ignoring duplicate `down`
+				// Ignoring duplicate down
 				return;
 				return;
 			} // Get the index of the dragged element within its parent
 			} // Get the index of the dragged element within its parent
 
 
@@ -4713,7 +4713,7 @@
 
 
 			if (options.handle && !closest(originalTarget, options.handle, el, false)) {
 			if (options.handle && !closest(originalTarget, options.handle, el, false)) {
 				return;
 				return;
-			} // Prepare `dragstart`
+			} // Prepare
 
 
 
 
 			this._prepareDragStart(evt, touch, target);
 			this._prepareDragStart(evt, touch, target);
@@ -5649,7 +5649,7 @@
 		/**
 		/**
 		 * For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
 		 * For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
 		 * @param   {HTMLElement}  el
 		 * @param   {HTMLElement}  el
-		 * @param   {String}       [selector]  default: `options.draggable`
+		 * @param   {String}       [selector]  default: options.draggable
 		 * @returns {HTMLElement|null}
 		 * @returns {HTMLElement|null}
 		 */
 		 */
 		closest: function closest$1(el, selector) {
 		closest: function closest$1(el, selector) {
@@ -7653,6 +7653,92 @@
 					AjaxFail(xhr.responseText, status, error);
 					AjaxFail(xhr.responseText, status, error);
 				});
 				});
 			},
 			},
+
+			ShopAttachProduct: function(product_id) {
+				var html = '<div class="modal fade" id="sys-modal-shop-product-attach" tabindex="-1" role="dialog" aria-labelledby="sysModalShopProductLabel" aria-hidden="true"> \
+					<div class="modal-dialog modal-dialog-centered" role="document"> \
+						<div class="modal-content"> \
+							<input type="hidden" name="action" value="index-user-update-profile"> \
+							<div class="modal-header"> \
+								<h5 class="modal-title" id="sysModalShopProductLabel">Attach product</h5> \
+								<button type="button" class="close" data-dismiss="modal" aria-label="Close"> \
+									<span aria-hidden="true">&times;</span> \
+								</button> \
+							</div> \
+							<div class="modal-body text-left"> \
+								<div class="form-group"> \
+									<input type="text" class="form-control" name="product-name" value="" placeholder="Type product name here..." readonly autocomplete="off"> \
+								</div> \
+								<div class="form-group" style="margin-bottom:0px;"> \
+									<div class="products-list"></div> \
+								</div> \
+							</div> \
+							<div class="modal-footer"> \
+								<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> \
+							</div> \
+						</div> \
+					</div> \
+				</div>';
+				$('#sys-modal-shop-product-attach-placeholder').html(html);
+				$("#sys-modal-shop-product-attach").modal({
+					backdrop: 'static',
+					keyboard: true,
+					show: false,
+				});
+				$('#sys-modal-shop-product-attach').on('hidden.bs.modal', function(e) {
+					$('#sys-modal-shop-product-attach-placeholder').html('');
+				});
+				$("#sys-modal-shop-product-attach").modal('show');
+				setTimeout(function() {
+					var SearchInput = $('#sys-modal-shop-product-attach input[name="product-name"]');
+					SearchInput.keyup(function() {
+						if(true || this.value != '') {
+							$.ajax({
+								type: "POST",
+								url: '/cp/',
+								data: {
+									action: 'shop-attach-product-search',
+									words: this.value,
+									id: product_id,
+								}
+							}).done(function(data) {
+								if($('#sys-modal-shop-product-attach').length > 0) {
+									if(IsDebugMode()) console.log('done', data);
+									AjaxDone(data);
+								}
+							}).fail(function(xhr, status, error) {
+								if(false) {
+									if($('#sys-modal-shop-product-attach').length > 0) {
+										if(IsDebugMode()) console.log('fail', xhr, status, error);
+										AjaxFail(xhr.responseText, status, error);
+									}
+								}
+							});
+						}
+					});
+					SearchInput.attr("readonly", false);
+					SearchInput.keyup();
+					SearchInput.focus();
+				}, 500);
+			},
+
+			ShopAttachProductTo: function(parent_id, product_id) {
+				$.ajax({
+					type: "POST",
+					url: '/cp/',
+					data: {
+						action: 'shop-attach-product-to',
+						parent_id: parent_id,
+						product_id: product_id,
+					}
+				}).done(function(data) {
+					if(IsDebugMode()) console.log('done', data);
+					AjaxDone(data);
+				}).fail(function(xhr, status, error) {
+					if(IsDebugMode()) console.log('fail', xhr, status, error);
+					AjaxFail(xhr.responseText, status, error);
+				});
+			},
 		};
 		};
 	}(window, $);
 	}(window, $);
 
 

File diff suppressed because it is too large
+ 0 - 0
assets/cp.scripts.js.go


File diff suppressed because it is too large
+ 0 - 0
assets/tmpl.cp.base.go


+ 2 - 1
assets/tmpl.cp.base.html

@@ -35,8 +35,9 @@
 		</script>
 		</script>
 	</head>
 	</head>
 	<body class="{{$.Data.BodyClasses}} cp-mod-{{$.System.CpModule}} cp-sub-mod-{{$.System.CpSubModule}}">
 	<body class="{{$.Data.BodyClasses}} cp-mod-{{$.System.CpModule}} cp-sub-mod-{{$.System.CpSubModule}}">
-		<div id="sys-modal-system-message-placeholder"></div>
 		<div id="sys-modal-user-settings-placeholder"></div>
 		<div id="sys-modal-user-settings-placeholder"></div>
+		<div id="sys-modal-shop-product-attach-placeholder"></div>
+		<div id="sys-modal-system-message-placeholder"></div>
 		<nav class="navbar main navbar-expand-md navbar-dark fixed-top bg-dark">
 		<nav class="navbar main navbar-expand-md navbar-dark fixed-top bg-dark">
 			<a class="navbar-brand" href="/cp/">{{$.Data.Caption}}</a>
 			<a class="navbar-brand" href="/cp/">{{$.Data.Caption}}</a>
 			<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
 			<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">

+ 80 - 0
modules/module_shop_act_attach_product_search.go

@@ -0,0 +1,80 @@
+package modules
+
+import (
+	"html"
+	"strings"
+
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+func (this *Modules) shop_GetProductsListForAttaching(wrap *wrapper.Wrapper, name string, id int) string {
+	result := ``
+
+	words := strings.Split(name, " ")
+	filtered := []string{}
+	for _, value := range words {
+		word := strings.TrimSpace(value)
+		if word != "" {
+			filtered = append(filtered, "%"+word+"%")
+		}
+	}
+
+	search := ""
+	params := make([]interface{}, len(filtered)+1)
+	params[0] = id
+	for i, value := range filtered {
+		search += " AND name LIKE ?"
+		params[i+1] = value
+	}
+
+	rows, err := wrap.DB.Query(
+		`SELECT
+			id,
+			name
+		FROM
+			shop_products
+		WHERE
+			id <> ? AND
+			parent_id IS NULL
+			`+search+`
+		ORDER BY
+			id DESC
+		LIMIT 10;`,
+		params...,
+	)
+	if err == nil {
+		defer rows.Close()
+		values := make([]string, 2)
+		scan := make([]interface{}, len(values))
+		for i := range values {
+			scan[i] = &values[i]
+		}
+		for rows.Next() {
+			err = rows.Scan(scan...)
+			if *wrap.LogCpError(&err) == nil {
+				result += `<div><a href="javascript:fave.ShopAttachProductTo(` + utils.IntToStr(id) + `,` + html.EscapeString(string(values[0])) + `);">` + html.EscapeString(string(values[1])) + `</a></div>`
+			}
+		}
+	}
+	if result == "" {
+		result = `<div><b>No any of products found</b></div>`
+	}
+	return result
+}
+
+func (this *Modules) RegisterAction_ShopAttachProductSearch() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "shop-attach-product-search",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_words := wrap.R.FormValue("words")
+		pf_id := wrap.R.FormValue("id")
+		if !utils.IsNumeric(pf_id) {
+			wrap.Write(`$('#sys-modal-shop-product-attach .products-list').html('<b>Inner system error</b>');`)
+			return
+		}
+		wrap.Write(`$('#sys-modal-shop-product-attach .products-list').html('` + this.shop_GetProductsListForAttaching(wrap, pf_words, utils.StrToInt(pf_id)) + `');`)
+	})
+}

Some files were not shown because too many files changed in this diff