]> saetta.ns0.it Git - zakform/cgi/commitdiff
Added functions FormElement::*_class.
authorAndrea Zagli <azagli@libero.it>
Sun, 29 Nov 2020 09:27:52 +0000 (10:27 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 29 Nov 2020 09:27:52 +0000 (10:27 +0100)
src/formelement.c
src/formelement.h

index bed6b934485ae319b79a3957eb3d309f7f7b2267..647a942ff01638fb168f230a297b57a4cae8338b 100644 (file)
@@ -220,31 +220,14 @@ gchar
 
        if (ZAK_FORM_CGI_IS_FORM_ELEMENT (element) && ZAK_FORM_CGI_FORM_ELEMENT_GET_CLASS (element)->render != NULL)
                {
-                       if (priv->ht_attrs == NULL)
-                               {
-                                       priv->ht_attrs = g_hash_table_new (g_str_hash, g_str_equal);
-                               }
-
-                       attr_class = g_hash_table_lookup (priv->ht_attrs, "class");
-                       if (attr_class != NULL)
-                               {
-                                       if (g_strstr_len (attr_class, -1, "form-control") == NULL)
-                                               {
-                                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup_printf ("%s form-control", attr_class));
-                                                       g_free (attr_class);
-                                               }
-                               }
-                       else
-                               {
-                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup ("form-control"));
-                               }
+                       zak_form_cgi_form_element_add_class (element, "form-control");
 
                        if (messages != NULL
                            && messages->len > 0)
                                {
                                        if (g_strcmp0 (priv->bootstrap_version, "4") == 0)
                                                {
-                                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup_printf ("%s is-invalid", g_hash_table_lookup (priv->ht_attrs, "class")));
+                                                       zak_form_cgi_form_element_add_class (element, "is-invalid");
                                                }
                                }
 
@@ -322,6 +305,97 @@ gchar
        return ret;
 }
 
+gboolean
+zak_form_cgi_form_element_has_class (ZakFormCgiFormElement *element, const gchar *class)
+{
+       gchar *attr;
+       gchar **classes;
+
+       gboolean ret;
+
+       ZakFormCgiFormElementPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       if (priv->ht_attrs == NULL)
+               {
+                       priv->ht_attrs = g_hash_table_new (g_str_hash, g_str_equal);
+                       return FALSE;
+               }
+
+       attr = g_hash_table_lookup (priv->ht_attrs, "class");
+       if (attr != NULL)
+               {
+                       classes = g_strsplit (attr, " ", -1);
+                       ret = g_strv_contains (classes, class);
+                       g_strfreev (classes);
+
+                       return ret;
+               }
+       else
+               {
+                       return FALSE;
+               }
+}
+
+void
+zak_form_cgi_form_element_add_class (ZakFormCgiFormElement *element, const gchar *class)
+{
+       gchar *attr;
+
+       ZakFormCgiFormElementPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       if (!zak_form_cgi_form_element_has_class (element, class))
+               {
+                       attr = g_hash_table_lookup (priv->ht_attrs, "class");
+                       if (attr != NULL)
+                               {
+                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup_printf ("%s %s", attr, class));
+                               }
+                       else
+                               {
+                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup (class));
+                               }
+               }
+}
+
+void
+zak_form_cgi_form_element_remove_class (ZakFormCgiFormElement *element, const gchar *class)
+{
+       gchar *attr;
+       gchar **classes;
+       GString *attr_new;
+
+       guint l;
+       guint i;
+
+       ZakFormCgiFormElementPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       if (zak_form_cgi_form_element_has_class (element, class))
+               {
+                       attr = g_hash_table_lookup (priv->ht_attrs, "class");
+                       if (attr != NULL)
+                               {
+                                       attr_new = g_string_new ("");
+                                       classes = g_strsplit (attr, " ", -1);
+                                       l = g_strv_length (classes);
+                                       for (i = 0; i < l; i++)
+                                               {
+                                                       if (g_strcmp0 (classes[i], class) != 0)
+                                                               {
+                                                                       g_string_append_printf (attr_new, " %s", classes[i]);
+                                                               }
+                                               }
+                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup (attr_new->str));
+                                       g_string_free (attr_new, TRUE);
+                               }
+               }
+}
+
 void
 zak_form_cgi_form_element_set_bootstrap_version (ZakFormCgiFormElement *element, const gchar *version)
 {
index c2a0eeb61a6eb3339b54973f62ee41aa54e63625..918011444762486a45c39b1217e497fe7ab938f2 100644 (file)
@@ -55,6 +55,10 @@ void zak_form_cgi_form_element_bind (ZakFormCgiFormElement *element, GValue *val
 gchar *zak_form_cgi_form_element_render (ZakFormCgiFormElement *element);
 gchar *zak_form_cgi_form_element_render_label (ZakFormCgiFormElement *element);
 
+gboolean zak_form_cgi_form_element_has_class (ZakFormCgiFormElement *element, const gchar *class);
+void zak_form_cgi_form_element_add_class (ZakFormCgiFormElement *element, const gchar *class);
+void zak_form_cgi_form_element_remove_class (ZakFormCgiFormElement *element, const gchar *class);
+
 void zak_form_cgi_form_element_set_bootstrap_version (ZakFormCgiFormElement *element, const gchar *version);
 gchar *zak_form_cgi_form_element_get_bootstrap_version (ZakFormCgiFormElement *element);