]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Refs #1793 added ability to add widgets to layout through ajax
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 18 Nov 2010 16:13:34 +0000 (16:13 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 18 Nov 2010 16:13:34 +0000 (16:13 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@7338 36083f99-b078-4883-b0ff-0f9b5a30f544

actions/widgets/add.php
engine/lib/widgets.php
js/lib/ui.widgets.js
languages/en.php
views/default/css.php
views/default/layouts/widgets.php
views/default/widgets/add.php
views/default/widgets/add_button.php

index 57c331e21d6c35d2da8c608a007d6a68f4ac112d..c57e3596a0fe142a1299fbb6a43c25886d206177 100644 (file)
@@ -6,25 +6,28 @@
  * @subpackage Widgets.Management
  */
 
-$guid = get_input('user');
+$user_guid = get_input('user');
 $handler = get_input('handler');
 $context = get_input('context');
-$column = get_input('column');
+$column = get_input('column', 1);
 
-$result = false;
-
-if (!empty($guid)) {
-       if ($user = get_entity($guid)) {
+$guid = false;
+if (!empty($user_guid)) {
+       if ($user = get_entity($user_guid)) {
                if ($user->canEdit()) {
-                       $result = add_widget($user->getGUID(), $handler, $context, 0, $column);
+                       $guid = add_widget($user->getGUID(), $handler, $context, 0, $column);
                }
        }
 }
 
-if ($result) {
-       system_message(elgg_echo('widgets:save:success'));
+if ($guid) {
+       system_message(elgg_echo('widgets:add:success'));
+
+       // send widget html for insertion
+       $widget = get_entity($guid);
+       echo elgg_view_entity($widget);
 } else {
-       register_error(elgg_echo('widgets:save:failure'));
+       register_error(elgg_echo('widgets:add:failure'));
 }
 
 forward(REFERER);
\ No newline at end of file
index 1de114833e257c16ab47b0624fc18c77c112d8ac..79a3b37bf549b3a7abc38f0f208116d3272969e8 100644 (file)
@@ -176,7 +176,7 @@ function display_widget(ElggObject $widget) {
  * @param int    $column      The column to display this widget in (1, 2 or 3)
  * @param int    $access_id   If not specified, it is set to the default access level
  *
- * @return bool Depending on success
+ * @return int|false Widget GUID or false on failure
  */
 function add_widget($entity_guid, $handler, $context, $order = 0, $column = 1, $access_id = null) {
        if (empty($entity_guid) || empty($context) || empty($handler) || !widget_type_exists($handler)) {
@@ -193,17 +193,12 @@ function add_widget($entity_guid, $handler, $context, $order = 0, $column = 1, $
                        $widget->access_id = get_default_access();
                }
 
-               if (!$widget->save()) {
-                       return false;
-               }
-
                $widget->handler = $handler;
                $widget->context = $context;
                $widget->column = $column;
                $widget->order = $order;
 
-               // save_widget_location($widget, $order, $column);
-               return true;
+               return $widget->save();
        }
 
        return false;
index f4fa4b25611ae5fbafd48f97fbbff93fee3aec44..b1e7b2a9938dead79a67c74e5840a065056cb9d5 100644 (file)
@@ -14,9 +14,26 @@ elgg.ui.widgets.init = function() {
                event.preventDefault();\r
        });\r
 \r
-\r
+       $('.widgets_add a').bind('click', function(event) {\r
+               elgg.action('widgets/add', {\r
+                       data: {\r
+                               handler: $(this).attr('id'),\r
+                               user: elgg.get_loggedin_userid(),\r
+                               context: $("input[name='widget_context']").val()\r
+                       },\r
+                       success: function(json) {\r
+                               elgg.ui.widgets.insert(json.output);\r
+                       }\r
+               });\r
+               event.preventDefault();\r
+       });\r
 };\r
 \r
+// insert a widget into the layout\r
+elgg.ui.widgets.insert = function(html) {\r
+       $('.widget_first_col').prepend(html);\r
+}\r
+\r
 //List active widgets for each page column\r
 elgg.ui.widgets.outputList = function(forElement) {\r
        return $("input[name='handler'], input[name='guid']", forElement).makeDelimitedList("value");\r
index e78d01c816176bc09dfc1ad201a286f47edb6a25..682c43333ed12b67ecf20a4d908623d81ae676a0 100644 (file)
@@ -245,6 +245,8 @@ $english = array(
        'widgets:panel:save:failure' => "There was a problem saving your widgets. Please try again.",
        'widgets:save:success' => "The widget was successfully saved.",
        'widgets:save:failure' => "We could not save your widget. Please try again.",
+       'widgets:add:success' => "The widget was successfully added.",
+       'widgets:add:failure' => "We could not add your widget.",
        'widgets:handlernotfound' => 'This widget is either broken or has been disabled by the site administrator.',
 
 /**
index edb0ebe07eccc64b0a7fdd4885bcacef48d744b6..d5b96bf592900d898eda2dc1f2e34cf277d70069 100644 (file)
@@ -863,28 +863,28 @@ li.navigation_more ul li {
        float: right;
        min-height: 15px;
 }
-.widget_col_1 {
+.widget_1_columns {
        width: 100%;
 }
-.widget_col_2 {
+.widget_2_columns {
        width: 48%;
        margin-right: 4%;
 }
-.widget_col_3 {
+.widget_3_columns {
        width: 32%;
        margin-right: 2%;
 }
-.widget_col_4 {
+.widget_4_columns {
        width: 23.5%;
        margin-right: 2%;
 }
-.widget_first_col {
+.widget_col_1 {
        margin-right: 0;
 }
 #widget_add_button {
        padding: 0px;
-       background-color: transparent;
        text-align: right;
+       margin-bottom: 15px;
 }
 .widgets_add {
        width: 100%;
index 5d6e42a431c02822c3ebe4f678eef83f6a3922e9..38451d1c1f99e2af62f27f9c91b1d89c99731410 100644 (file)
@@ -18,12 +18,19 @@ elgg_push_context('widgets');
 elgg_get_widgets($owner->guid, $context);
 
 if (elgg_can_edit_widgets()) {
-       echo elgg_view('widgets/add', array('widgets' => $widgets));
+       if ($show_add_widgets) {
+               echo elgg_view('widgets/add_button');
+       }
+       $params = array(
+               'widgets' => $widgets,
+               'context' => $context,
+       );
+       echo elgg_view('widgets/add', $params);
 }
 
 echo $vars['box'];
 
-$widget_class = "widget_col_$num_columns";
+$widget_class = "widget_{$num_columns}_columns";
 for ($column_index = 1; $column_index <= $num_columns; $column_index++) {
        $widgets = get_widgets($owner->guid, $context, $column_index);
 
@@ -34,15 +41,7 @@ for ($column_index = 1; $column_index <= $num_columns; $column_index++) {
        $widget2->handler = 'test';
        $widgets = array($widget1, $widget2);
 
-       $first = ($column_index == 1) ? 'widget_first_col' : '';
-
-       echo "<div class=\"widget_column $widget_class $first\">";
-       // button for adding new widgets
-       if ($column_index == 1) {
-               if ($show_add_widgets && elgg_can_edit_widgets()) {
-                       echo elgg_view('widgets/add_button');
-               }
-       }
+       echo "<div class=\"widget_column $widget_class widget_col_$column_index\">";
        if (is_array($widgets) && sizeof($widgets) > 0) {
                foreach ($widgets as $widget) {
                        echo elgg_view_entity($widget);
index 2cc06dab7a6220324a6a8d2c795f26a3c69af34e..9687c5bea91f20321f196b0170f84454aa4748cc 100644 (file)
@@ -3,18 +3,32 @@
 $widgets = $vars['widgets'];
 $widget_types = get_widget_types();
 
+$context = $vars['context'];
+
 ?>
 <div class="widgets_add hidden">
        <p>
                <?php echo elgg_echo('widgets:add:description'); ?>
        </p>
        <ul>
-               <?php
-                       foreach ($widget_types as $widget_type) {
-                               $link = elgg_view('output/url', array('text' => $widget_type->name, 'href' => '#'));
-                               echo "<li>$link</li>";
-                       }
-               ?>
+<?php
+               foreach ($widget_types as $handler => $widget_type) {
+                       $options = array(
+                               'text' => $widget_type->name,
+                               'href' => '#',
+                               'internalid' => $handler,
+                       );
+                       $link = elgg_view('output/url', $options);
+                       echo "<li>$link</li>";
+               }
+?>
        </ul>
+<?php
+       $params = array(
+               'internalname' => 'widget_context',
+               'value' => $context
+       );
+       echo elgg_view('input/hidden', $params);
+?>
        <div class="clearfloat"></div>
 </div>
index 8ec36b086100a5af0ff396aac7aac57d1004e80a..2957513b67bb4d3b3e2165624a4f3c051be3d02b 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 ?>
-<div class="widget" id="widget_add_button">
+<div id="widget_add_button">
 <?php
 $options = array(
        'href' => '#',