Browse Source

HUE-950 [core] Updated tags UI

Enrico Berti 12 years ago
parent
commit
0df06d5a5e

+ 69 - 12
desktop/core/src/desktop/templates/home.mako

@@ -77,21 +77,21 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
               <a href="#" data-toggle="dropdown"><i class="icon-plus-sign"></i> ${_('New document')}</a>
               <a href="#" data-toggle="dropdown"><i class="icon-plus-sign"></i> ${_('New document')}</a>
               <ul class="dropdown-menu" role="menu">
               <ul class="dropdown-menu" role="menu">
                 % if 'beeswax' in apps:
                 % if 'beeswax' in apps:
-                <li><a href="#"><img src="/beeswax/static/art/icon_beeswax_24.png"/> ${_('Hive Query')}</a></li>
+                <li><a href="${ url('beeswax:index') }"><img src="/beeswax/static/art/icon_beeswax_24.png"/> ${_('Hive Query')}</a></li>
                 % endif
                 % endif
                 % if 'impala' in apps:
                 % if 'impala' in apps:
-                <li><a href="#"><img src="/impala/static/art/icon_impala_24.png"/> ${_('Impala Query')}</a></li>
+                <li><a href="${ url('impala:index') }"><img src="/impala/static/art/icon_impala_24.png"/> ${_('Impala Query')}</a></li>
                 % endif
                 % endif
                 % if 'pig' in apps:
                 % if 'pig' in apps:
-                <li><a href="#"><img src="/pig/static/art/icon_pig_24.png"/> ${_('Pig Script')}</a></li>
+                <li><a href="${ url('pig:index') }"><img src="/pig/static/art/icon_pig_24.png"/> ${_('Pig Script')}</a></li>
                 % endif
                 % endif
                 % if 'oozie' in apps:
                 % if 'oozie' in apps:
                 <li class="dropdown-submenu">
                 <li class="dropdown-submenu">
                   <a href="#"><img src="/oozie/static/art/icon_oozie_24.png"/> ${_('Oozie Scheduler')}</a>
                   <a href="#"><img src="/oozie/static/art/icon_oozie_24.png"/> ${_('Oozie Scheduler')}</a>
                   <ul class="dropdown-menu">
                   <ul class="dropdown-menu">
-                    <li><a href="#"><img src="/oozie/static/art/icon_oozie_24_workflow.png"/> ${_('Workflow')}</a></li>
-                    <li><a href="#"><img src="/oozie/static/art/icon_oozie_24_coordinator.png"/> ${_('Coordinator')}</a></li>
-                    <li><a href="#"><img src="/oozie/static/art/icon_oozie_24_bundle.png"/> ${_('Bundle')}</a></li>
+                    <li><a href="${ url('oozie:create_workflow') }"><img src="/oozie/static/art/icon_oozie_24_workflow.png"/> ${_('Workflow')}</a></li>
+                    <li><a href="${ url('oozie:create_coordinator') }"><img src="/oozie/static/art/icon_oozie_24_coordinator.png"/> ${_('Coordinator')}</a></li>
+                    <li><a href="${ url('oozie:create_bundle') }"><img src="/oozie/static/art/icon_oozie_24_bundle.png"/> ${_('Bundle')}</a></li>
                   </ul>
                   </ul>
                 </li>
                 </li>
                 % endif
                 % endif
@@ -156,7 +156,7 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
   </div>
   </div>
   <div class="modal-footer">
   <div class="modal-footer">
     <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
     <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
-    <a id="saveDocumentTags" href="#" class="btn btn-primary">${_('Save tags')}</a>
+    <a id="saveDocumentTags" href="#" class="btn btn-primary disable-feedback">${_('Save tags')}</a>
   </div>
   </div>
 </div>
 </div>
 
 
@@ -179,7 +179,7 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
   </div>
   </div>
   <div class="modal-footer">
   <div class="modal-footer">
     <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
     <a href="#" data-dismiss="modal" class="btn">${_('Cancel')}</a>
-    <a id="removeTags" href="#" class="btn btn-danger">${_('Remove selected')}</a>
+    <a id="removeTags" href="#" class="btn btn-danger disable-feedback">${_('Remove selected')}</a>
   </div>
   </div>
 </div>
 </div>
 
 
@@ -389,27 +389,75 @@ $(document).ready(function () {
   });
   });
 
 
   $("#saveDocumentTags").on("click", function () {
   $("#saveDocumentTags").on("click", function () {
+    $(this).attr("data-loading-text", $(this).text() + " ...");
+    $(this).button("loading");
     var _tags = [];
     var _tags = [];
     $(".documentTagsModalCheckbox.selected").each(function () {
     $(".documentTagsModalCheckbox.selected").each(function () {
       var _this = $(this);
       var _this = $(this);
       _tags.push(_this.data("value"));
       _tags.push(_this.data("value"));
     });
     });
-    // TODO: $.post to remote to save the tags of a document
-    console.log("Post add tags to document " + _tags);
+    $.post("/doc/update_tags", {
+      data: JSON.stringify({
+        doc_id: $("#documentTagsModal").data("document-id"),
+        tag_ids: _tags
+      })
+    }, function (response) {
+      if (response.doc != null) {
+        var _doc = response.doc;
+        var _dtNodes = documentsTable.fnGetNodes();
+        $(_dtNodes).each(function (iNode, node) {
+          if ($(node).children("td").eq(3).find(".documentTags").data("document-id") == _doc.id) {
+            var _tags = "";
+            for (var i = 0; i < _doc.tags.length; i++) {
+              _tags += '<span class="badge">' + _doc.tags[i].name + '</span> ';
+            }
+            documentsTable.fnUpdate('<div class="documentTags" data-document-id="' + _doc.id + '">' + _tags + '</div>', node, 3, false);
+            updateDoc(_doc);
+            $("#saveDocumentTags").button("loading");
+          }
+        });
+      }
+      $("#documentTagsModal").modal("hide");
+    })
   });
   });
 
 
   $("#removeTags").on("click", function () {
   $("#removeTags").on("click", function () {
+    $(this).attr("data-loading-text", $(this).text() + " ...");
+    $(this).button("loading");
     var _tags = [];
     var _tags = [];
     $(".tagsModalCheckbox.selected").each(function () {
     $(".tagsModalCheckbox.selected").each(function () {
       var _this = $(this);
       var _this = $(this);
       _tags.push(_this.data("value"));
       _tags.push(_this.data("value"));
     });
     });
-    // TODO: $.post to remote to remove tags globally
-    console.log("Post remove tags " + _tags);
+    $.post("/tag/remove_tags", {
+      data: JSON.stringify({
+        tag_ids: _tags
+      })
+    }, function (response) {
+      $.getJSON("/tag/list_tags", function (data) {
+        JSON_TAGS = data;
+        renderTags();
+        renderTagsModal();
+        renderDocs(function () {
+          $("#removeTags").button("reset");
+          $("#tagsModal").modal("hide");
+        });
+      });
+    });
   });
   });
 
 
 });
 });
 
 
+function renderDocs(callback) {
+  $.getJSON("/doc/list_docs", function (data) {
+    JSON_DOCS = data;
+    populateTable();
+    if (callback != null) {
+      callback();
+    }
+  });
+}
+
 function isInTags(doc, tag) {
 function isInTags(doc, tag) {
   if (doc.tags == null) {
   if (doc.tags == null) {
     return false;
     return false;
@@ -480,6 +528,15 @@ function getDocById(id) {
   return _doc;
   return _doc;
 }
 }
 
 
+function updateDoc(updatedDoc) {
+  $(JSON_DOCS).each(function (cnt, doc) {
+    if (doc.id == updatedDoc.id) {
+      JSON_DOCS[cnt] = updatedDoc;
+    }
+  });
+}
+
+
 function getIcon(contentType) {
 function getIcon(contentType) {
   var _code = '<img src="';
   var _code = '<img src="';
   switch (contentType) {
   switch (contentType) {

+ 3 - 1
desktop/core/src/desktop/urls.py

@@ -70,9 +70,11 @@ dynamic_patterns += patterns('desktop.views',
 
 
   # Doc model
   # Doc model
   (r'^tag/add_tag', 'add_tag'),
   (r'^tag/add_tag', 'add_tag'),
+  (r'^tag/remove_tags', 'remove_tags'),
   (r'^tag/list_tags', 'list_tags'),
   (r'^tag/list_tags', 'list_tags'),
+  (r'^doc/list_docs$', 'list_docs'),
   (r'^doc/add_or_create_tag$', 'add_or_create_tag'),
   (r'^doc/add_or_create_tag$', 'add_or_create_tag'),
-  (r'^doc/remove_tag$', 'remove_tag'),
+  (r'^doc/update_tags$', 'update_tags'),
   (r'^doc/add_or_update_permission$', 'add_or_update_permission'),
   (r'^doc/add_or_update_permission$', 'add_or_update_permission'),
   (r'^doc/remove_permission$', 'remove_permission'),
   (r'^doc/remove_permission$', 'remove_permission'),
 
 

+ 42 - 15
desktop/core/src/desktop/views.py

@@ -62,10 +62,26 @@ def home(request):
     'json_tags': json.dumps(massaged_tags_for_json(tags, request.user))
     'json_tags': json.dumps(massaged_tags_for_json(tags, request.user))
   })
   })
 
 
+
+def list_docs(request):
+  docs = Document.objects.get_docs(request.user).order_by('-last_modified')[:100]
+  return HttpResponse(json.dumps(massaged_documents_for_json(docs)), mimetype="application/json")
+
+
+def list_tags(request):
+  tags = DocumentTag.objects.filter(owner=request.user)
+  return HttpResponse(json.dumps(massaged_tags_for_json(tags, request.user)), mimetype="application/json")
+
+
 def massaged_documents_for_json(documents):
 def massaged_documents_for_json(documents):
   docs = []
   docs = []
   for doc in documents:
   for doc in documents:
-    massaged_doc = {
+    docs.append(massage_doc_for_json(doc))
+
+  return docs
+
+def massage_doc_for_json(doc):
+  return {
       'id': doc.id,
       'id': doc.id,
       'contentType': doc.content_type.name,
       'contentType': doc.content_type.name,
       'name': doc.name,
       'name': doc.name,
@@ -76,10 +92,6 @@ def massaged_documents_for_json(documents):
       'lastModified': doc.last_modified.strftime("%x %X"),
       'lastModified': doc.last_modified.strftime("%x %X"),
       'lastModifiedInMillis': time.mktime(doc.last_modified.timetuple())
       'lastModifiedInMillis': time.mktime(doc.last_modified.timetuple())
     }
     }
-    docs.append(massaged_doc)
-
-  return docs
-
 
 
 def massaged_tags_for_json(tags, user):
 def massaged_tags_for_json(tags, user):
   ts = []
   ts = []
@@ -108,10 +120,6 @@ def add_tag(request):
 
 
   return HttpResponse(json.dumps(response), mimetype="application/json")
   return HttpResponse(json.dumps(response), mimetype="application/json")
 
 
-def list_tags(request):
-  tags = DocumentTag.objects.objects.filter(owner=request.user)
-  return HttpResponse(json.dumps(massaged_tags_for_json(tags, request.user)), mimetype="application/json")
-
 
 
 def add_or_create_tag(request):
 def add_or_create_tag(request):
   response = {'status': -1, 'message': ''}
   response = {'status': -1, 'message': ''}
@@ -129,21 +137,40 @@ def add_or_create_tag(request):
   return HttpResponse(json.dumps(response), mimetype="application/json")
   return HttpResponse(json.dumps(response), mimetype="application/json")
 
 
 
 
-def remove_tag(request):
+def update_tags(request):
+  response = {'status': -1, 'message': ''}
+
+  if request.method == 'POST':
+    request_json = json.loads(request.POST['data'])
+    try:
+      print request_json
+      #doc = DocumentTag.objects.update_tags(request.user, request_json['doc_id'], request_json['tag_ids'])
+      #response['doc'] = massage_doc_for_json(doc)
+    except Exception, e:
+      response['message'] = force_unicode(e)
+  else:
+    response['message'] = _('POST request only')
+
+  return HttpResponse(json.dumps(response), mimetype="application/json")
+
+
+def remove_tags(request):
   response = {'status': -1, 'message': _('Error')}
   response = {'status': -1, 'message': _('Error')}
-  
+
   if request.method == 'POST':
   if request.method == 'POST':
-    json = {'tag_id': 1, 'tag': 'hue project', 'doc_id': 1}  # instead ... json.load(request.POST)
+    request_json = json.loads(request.POST['data'])
+    print request_json
     try:
     try:
-      DocumentTag.objects.remove_tag(id=json['tag_id'], owner=request.user, doc_id=json['doc_id'])
+      for tag_id in request_json['tag_ids']:
+        print tag_id
+        #DocumentTag.delete_tag(tag_id, request.user)
       response['message'] = _('Tag removed !')
       response['message'] = _('Tag removed !')
     except Exception, e:
     except Exception, e:
       response['message'] = force_unicode(e)
       response['message'] = force_unicode(e)
   else:
   else:
     response['message'] = _('POST request only')
     response['message'] = _('POST request only')
-  
-  return HttpResponse(json.dumps(response), mimetype="application/json")
 
 
+  return HttpResponse(json.dumps(response), mimetype="application/json")
 
 
 def add_or_update_permission(request):
 def add_or_update_permission(request):
   pass
   pass