From 9786fe2c2ba515d9734fa3cad6fa6a430c99893f Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Thu, 24 Jul 2014 16:57:18 +0200
Subject: [PATCH] dev #15356 wrap url into methods

---
 .../CustomFieldsReportController.php          |  2 +-
 .../scripts/custom-fields-report/edit.phtml   |  2 +-
 .../admin/js/custom-fields/custom_fields.js   | 28 +++++++++++--
 .../custom-fields/tests/custom_fields_test.js | 41 +++++++++++++++++--
 4 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/application/modules/admin/controllers/CustomFieldsReportController.php b/application/modules/admin/controllers/CustomFieldsReportController.php
index b0391726f2d..5bad4aca9e9 100644
--- a/application/modules/admin/controllers/CustomFieldsReportController.php
+++ b/application/modules/admin/controllers/CustomFieldsReportController.php
@@ -50,7 +50,7 @@ class Admin_CustomFieldsReportController extends ZendAfi_Controller_Action {
 
 		
 		Class_ScriptLoader::getInstance()
-			->addAdminScript('/custom-fields/custom_fields')
+			->addAdminScript('custom-fields/custom_fields')
 			->addJQueryReady($report->getScript());
 		
 		$this->_forward('index');
diff --git a/application/modules/admin/views/scripts/custom-fields-report/edit.phtml b/application/modules/admin/views/scripts/custom-fields-report/edit.phtml
index cff699e46b0..8c46484f4db 100644
--- a/application/modules/admin/views/scripts/custom-fields-report/edit.phtml
+++ b/application/modules/admin/views/scripts/custom-fields-report/edit.phtml
@@ -9,7 +9,7 @@ echo $this->renderForm($this->form, [
 ]);
 
 Class_ScriptLoader::getInstance()
-	->addAdminScript('/custom-fields/custom_fields')
+	->addAdminScript('custom-fields/custom_fields')
 	->loadCodeMirror()
 	->addJQueryReady("
 var cm;
diff --git a/public/admin/js/custom-fields/custom_fields.js b/public/admin/js/custom-fields/custom_fields.js
index c9809c9218e..9a3d21a884e 100644
--- a/public/admin/js/custom-fields/custom_fields.js
+++ b/public/admin/js/custom-fields/custom_fields.js
@@ -19,24 +19,46 @@
  */
 (function ( $ ) {
   $.fn.report_helper = function() {
+    var base_url = window.location.hostname;
     var current = this;
     return {
       'current': current,
 
+
       downloadReport: function(datas) {
 	var a = $('<a target="_blank" download="report.csv" href="data:attachment/csv,' + encodeURIComponent(datas) + '"></a>');
-	a.appendTo(this.current);
-	a.click();
+	a.appendTo($('body'));
+	this.download(a);
+      },
+
+      download: function(anchor) {
+	anchor[0].click();
       },
 
-      jsonContent: function() {
+      jsonContent: function(url) {
 	var ajax= $.ajax({
 	  url: url,
 	  dataType: 'json',
 	  async: false
 	});
 	return $.parseJSON(ajax.responseText);
+      },
+
+
+      datasFields: function(model) {
+	return this.jsonContent(base_url+'/opac/datas/fields/model/'+model);
+      },
+
+      
+      fieldValues: function(model) {
+	return this.jsonContent(base_url+'/opac/datas/values/field/'+model);
+      },
+
+      
+      numberOf: function(model, id, child_name) {
+	return this.jsonContent(base_url+'/opac/datas/numberof/model/'+model+'/id/'+id+'/children/'+child_name);
       }
+
     };
   };
 } (jQuery));
diff --git a/public/admin/js/custom-fields/tests/custom_fields_test.js b/public/admin/js/custom-fields/tests/custom_fields_test.js
index 48fa26bb8be..9f10a3854f0 100644
--- a/public/admin/js/custom-fields/tests/custom_fields_test.js
+++ b/public/admin/js/custom-fields/tests/custom_fields_test.js
@@ -18,22 +18,55 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
 
-var fixture;
+var fixture = '';
+var helper = null;
+var options = null;
+
 
 QUnit.module('custom_fields', {
   setup: function() {
-    fixture = $('<div></div>');
+    jQuery.ajax = function (param) {
+      options = param;
+      var responseText = function() {
+	return false;
+      };
+      return this;
+    };
+
+    jQuery.parseJSON = function(response) {
+      return response;
+    }
+    
+    helper = $.fn.report_helper();
+    helper.download = function() {};
   }
 });
 
+
 test('plugin is defined', function() {
   ok($.fn.report_helper);
 });
 
 
 test('link to download csv should be present', function() {
-  var helper = fixture.report_helper();
   helper.downloadReport('info');
-  equal(fixture.find('a[href*="data:attachment/csv,info"]').length, 1, fixture.html());
+  equal($('body').find('a[href*="data:attachment/csv,info"]').length, 1, $('body').html());
+});
+
+
+test('url for data model should be as expected' ,function() {
+  var fields = helper.datasFields('Article');
+  equal(options.url.length, window.location.hostname+'/opac/datas/fields/model/Artcile'.length);  
 });
 
+
+test('url for field values should be as expected' ,function() {
+  var values = helper.fieldValues(456);
+  equal(options.url.length, window.location.hostname+'/opac/datas/values/field/456'.length);  
+});
+
+
+test('url for number of children by model and id should be  as expected' ,function() {
+  var values = helper.numberOf('Formation',98,'Session');
+  equal(options.url.length, window.location.hostname+'/opac/datas/numberof/model/Formation/id/98/children/Session'.length, options.url);
+});
-- 
GitLab