noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 57/219: New : inplace_edit permit the develop


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 57/219: New : inplace_edit permit the developer to create easily a "inplace" edit field
Date: Mon, 18 Dec 2017 13:22:38 -0500 (EST)

sparkyx pushed a commit to branch master
in repository noalyss.

commit 176403f1b49d54216c33067e516927e5192261d4
Author: Dany De Bontridder <address@hidden>
Date:   Mon Sep 25 23:29:40 2017 +0200

    New : inplace_edit permit the developer to create
    easily a "inplace" edit field
---
 include/lib/inplace_edit.class.php | 166 +++++++++++++++++++++++++++++++++++++
 scenario/inplace_edit.test.php     |  75 +++++++++++++++++
 2 files changed, 241 insertions(+)

diff --git a/include/lib/inplace_edit.class.php 
b/include/lib/inplace_edit.class.php
new file mode 100644
index 0000000..855773e
--- /dev/null
+++ b/include/lib/inplace_edit.class.php
@@ -0,0 +1,166 @@
+<?php
+
+/*
+ *   This file is part of NOALYSS.
+ *
+ *   NOALYSS is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   NOALYSS is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with NOALYSS; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+// Copyright Author Dany De Bontridder address@hidden
+
+/**
+ * @file
+ * @brief Inplace_edit class for ajax update of HtmlInput object
+ */
+/**
+ * @class
+ * @brief Inplace_edit class for ajax update of HtmlInput object.
+ * You need an ajax to response and modify the data. Some parameters will be 
sent
+ * by default when you click on the element
+ *  - input : htmlInput object serialized
+ *  - action : ok or cancel , nothing if you just want to display the input
+ * 
+ * @example inplace_edit.test.php
+ */
+class Inplace_Edit
+{
+  
+
+    ///< HtmlInput object 
+    private $input;
+    ///< Json object to pass to JavaScript
+    private $json;
+    ///< Php file which answered the ajax
+    private $callback;
+    /**
+     * Create a Inplace_Edit, initialise JSON and fullfill the default json 
value:
+     * input which is the HtmlInput object serialized
+     * @param HtmlInput $p_input
+     */
+    function __construct(HtmlInput $p_input) {
+        $this->input=$p_input;
+        $x["input"]=serialize($p_input);
+        $this->json=json_encode($x, 
JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
+    }
+    ///@brief build a Inplace_Edit object from
+    /// a serialized string (ajax json parameter = input)
+    static function build($p_serialize)
+    {
+        
+        $input=  unserialize($p_serialize);
+        $obj=new Inplace_Edit($input);
+        return $obj;
+    }
+    function display()
+    {
+        echo $this->input->value;
+    }
+    /**
+     * @brief response in ajax to be edited
+     */
+     function ajax_input() {
+        echo $this->input->input();
+        echo '<a class="smallbutton"  
id="inplace_edit_ok'.$this->input->id.'">'._('ok').'</a>';
+        echo '<a class="smallbutton" 
id="inplace_edit_cancel'.$this->input->id.'">'._('cancel').'</a>';
+        echo <<<EOF
+        <script>
+            {$this->input->id}edit.onclick=null;
+            inplace_edit_ok{$this->input->id}.onclick= function () {
+                var json={$this->json};
+                json['ieaction']='ok';
+                json['value']=$({$this->input->id}).value;
+                new Ajax.Updater('{$this->input->id}edit'
+                ,'{$this->callback}',
+                 {parameters:  json ,evalScripts:true});}
+            inplace_edit_cancel{$this->input->id}.onclick= function () {
+                var json={$this->json};
+                json['ieaction']='cancel';
+                new Ajax.Updater('{$this->input->id}edit'
+                ,'{$this->callback}',
+                 {parameters:  json ,evalScripts:true});}
+            
+        </script>
+EOF;
+    }
+    
+    /***
+     * @brief display the value with the click event
+     */
+    function input() {
+        echo <<<EOF
+            <span class="inplace_edit" id="{$this->input->id}edit" >
+EOF;
+        echo $this->input->value;
+        echo "</span>";
+        echo "
+            <script>
+        {$this->input->id}edit.onclick=function() {
+                 new Ajax.Updater('{$this->input->id}edit'
+                ,'{$this->callback}',
+                 {parameters:  {$this->json} ,evalScripts:true});}
+            </script>
+              ";      
+    }
+    /**
+     * @brief the php callback file to call for ajax
+     */
+    function set_callback($callback) {
+        $this->callback=$callback;
+    }
+    /***
+     * @brief the JSON parameter to give to the script, 
+     * this function shouldn't be used since it override the default JSON value
+     * @see add_json_parameter
+     */
+    function set_json($json) {
+        $this->json=$json;
+    }
+     /**
+     * Add json parameter to the current one, if there attribute already exists
+     * it will be overwritten
+     */
+    function add_json_param($p_attribute,$p_value) {
+        $x=json_decode($this->json,TRUE);
+        $x[$p_attribute]=$p_value;
+        $this->json=json_encode($x, 
JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
+    }
+    /**
+     * \brief return the HtmlObject , var input
+     * 
+     */
+    function get_input() {
+        return $this->input;
+    }
+    /**
+     * @brief set the var input (HtmlObject) and update the
+     * json attribute input
+     * @param HtmlInput $p_input
+     */
+    function set_input(HtmlInput $p_input) {
+        $this->input = $p_input;
+        $x=json_decode($this->json,TRUE);
+        $x["input"]=serialize($p_input);
+        $this->json=json_encode($x, 
JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
+    }
+    /**
+     * Set the value of the HtmlInput object $input
+     * @param type $p_value
+     */
+    function set_value($p_value) {
+        $input=$this->get_input();
+        $this->input->value=  strip_tags($p_value);
+        $this->set_input($input);
+    }
+}
\ No newline at end of file
diff --git a/scenario/inplace_edit.test.php b/scenario/inplace_edit.test.php
new file mode 100644
index 0000000..6800165
--- /dev/null
+++ b/scenario/inplace_edit.test.php
@@ -0,0 +1,75 @@
+<?php
+
+/*
+ *   This file is part of NOALYSS.
+ *
+ *   NOALYSS is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   NOALYSS is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with NOALYSS; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+// Copyright Author Dany De Bontridder address@hidden 2017
+
+if (!defined('ALLOWED'))
+    die('Appel direct ne sont pas permis');
+//@description:Test the class Inplace_Edit , ajax and javascript
+
+
+require_once NOALYSS_INCLUDE . '/lib/itext.class.php';
+require_once NOALYSS_INCLUDE . '/lib/inum.class.php';
+require_once NOALYSS_INCLUDE . '/lib/inplace_edit.class.php';
+if (!isset($_REQUEST["TestAjaxFile"])) {
+    echo h1(_("Test Inplace_Edit"));
+    /***********************************************
+     * If TestAjaxFile is not set it is not a ajax call
+     *********************************************** */
+    $hello = new IText("hello");
+    $hello->value = "2012";
+    $hello->id = "hello_ajax";
+   
+    $ajax_hello = new Inplace_Edit($hello);
+    $ajax_hello->set_callback("ajax_test.php");
+    $ajax_hello->add_json_param("TestAjaxFile", __FILE__) ;
+    $ajax_hello->add_json_param("gDossier", Dossier::id());
+    echo $ajax_hello->input();
+    
+    
+} else {
+    /*************************************************
+     * Ajax answer
+     **************************************************/
+    $input=$http->request("input");
+    $action=$http->request("ieaction","string","display");
+    if ($action=="display") {
+        $ajax_hello = Inplace_Edit::build($input);
+        $ajax_hello->set_callback("ajax_test.php");
+        $ajax_hello->add_json_param("TestAjaxFile", __FILE__);
+        $ajax_hello->add_json_param("gDossier", Dossier::id());
+        echo $ajax_hello->ajax_input();
+    }
+    if ( $action == "ok") {
+        $ajax_hello = Inplace_Edit::build($input);
+        $ajax_hello->add_json_param("TestAjaxFile", __FILE__);
+        $ajax_hello->add_json_param("gDossier", Dossier::id());
+        $ajax_hello->set_value($http->request("value"));
+        $ajax_hello->set_callback("ajax_test.php");
+        echo $ajax_hello->input();
+    }
+    if ( $action == "cancel") {
+        $ajax_hello = Inplace_Edit::build($input);
+        $ajax_hello->add_json_param("TestAjaxFile", __FILE__);
+        $ajax_hello->add_json_param("gDossier", Dossier::id());
+        $ajax_hello->set_callback("ajax_test.php");
+        echo $ajax_hello->input();
+    }
+}
\ No newline at end of file



reply via email to

[Prev in Thread] Current Thread [Next in Thread]