[ckan-changes] commit/ckan: 2 new changesets
Bitbucket
commits-noreply at bitbucket.org
Sat Aug 27 00:13:14 UTC 2011
2 new changesets in ckan:
http://bitbucket.org/okfn/ckan/changeset/246e44e55a72/
changeset: 246e44e55a72
branch: refactor-1282-js-wui-cruft
user: rgrp
date: 2011-08-26 11:17:15
summary: [refactor,js][s]: switch formatautocomplete to use standard jquery ui autocomplete setup.
affected #: 4 files (846 bytes)
--- a/ckan/public/scripts/application.js Thu Aug 25 23:13:21 2011 +0100
+++ b/ckan/public/scripts/application.js Fri Aug 26 10:17:15 2011 +0100
@@ -4,6 +4,7 @@
CKAN.Utils.setupAuthzGroupAutocomplete($('input.autocomplete-authzgroup'));
CKAN.Utils.setupPackageAutocomplete($('input.autocomplete-package'));
CKAN.Utils.setupTagAutocomplete($('input.autocomplete-tag'));
+ CKAN.Utils.setupFormatAutocomplete($('input.autocomplete-format'));
});
}(jQuery));
@@ -103,6 +104,25 @@
});
};
+ // Attach tag autocompletion to provided elements
+ //
+ // Requires: jquery-ui autocomplete
+ my.setupFormatAutocomplete = function(elements) {
+ elements.autocomplete({
+ minLength: 1,
+ source: function(request, callback) {
+ var url = '/api/2/util/resource/format_autocomplete?incomplete=' + request.term;
+ $.getJSON(url, function(data) {
+ // data = { ResultSet: { Result: [ {Name: tag} ] } } (Why oh why?)
+ var formats = $.map(data.ResultSet.Result, function(value, idx) {
+ return value.Format;
+ });
+ callback(formats);
+ });
+ }
+ });
+ };
+
// Attach user autocompletion to provided elements
//
// Requires: jquery-ui autocomplete
--- a/ckan/public/scripts/formatautocomplete.js Thu Aug 25 23:13:21 2011 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-(function($){
- var url = "";
-
- function extractDataAttributes(){
- var el = $(this);
- $.each(this.attributes, function(){
- // get the autocomplete API URL
- if(this.name === 'data-format-autocomplete-url'){
- url = this.value;
- }
- });
- }
-
- function autoCompleteList(request, response){
- var requestData = {'incomplete': request.term};
-
- $.ajax({
- url: url,
- data: requestData,
- dataType: 'jsonp',
- type: 'get',
- jsonpCallback: 'callback',
- success: function(json){
- var formats = [];
- $.each(json["ResultSet"]["Result"], function(){
- formats.push(this["Format"]);
- });
- response(formats);
- },
- });
- }
-
- $(document).ready(function(){
- $('.format-autocomplete').focus(extractDataAttributes)
- .autocomplete({source: autoCompleteList});
- });
-})(jQuery);
--- a/ckan/templates/package/edit.html Thu Aug 25 23:13:21 2011 +0100
+++ b/ckan/templates/package/edit.html Fri Aug 26 10:17:15 2011 +0100
@@ -11,9 +11,6 @@
<!-- Flexitable --><script type="text/javascript" src="${g.site_url}/scripts/flexitable.js"></script><link rel="stylesheet" href="${g.site_url}/css/flexitable.css" />
-
- <!-- Format field autocomplete -->
- <script type="text/javascript" src="${g.site_url}/scripts/formatautocomplete.js"></script></py:def><div py:match="content" class="package">
--- a/ckan/templates/package/new_package_form.html Thu Aug 25 23:13:21 2011 +0100
+++ b/ckan/templates/package/new_package_form.html Fri Aug 26 10:17:15 2011 +0100
@@ -78,10 +78,16 @@
<tr><py:for each="col in c.resource_columns"><td py:choose="" class="resource-${col}">
- <input py:when="col == 'format'" name="resources__${num}__${col}"
- type="text" value="${res.get(col, '')}" class="format-autocomplete short"
- data-format-autocomplete-url="/api/2/util/resource/format_autocomplete" />
- <input py:otherwise="" name="resources__${num}__${col}" type="text" value="${res.get(col, '')}" class="${'medium-width' if col=='description' else 'short'}" />
+ <input py:when="col == 'format'"
+ name="resources__${num}__${col}"
+ type="text" value="${res.get(col, '')}"
+ class="autocomplete-format short"
+ />
+ <input py:otherwise=""
+ name="resources__${num}__${col}"
+ type="text"
+ value="${res.get(col, '')}"
+ class="${'medium-width' if col=='description' else 'short'}" /></td></py:for><td class="resource-id"><input name="resources__${num}__id" type="hidden" value="${res.get('id', '')}" /></td>
http://bitbucket.org/okfn/ckan/changeset/6fd144409e9f/
changeset: 6fd144409e9f
branch: refactor-1282-js-wui-cruft
user: rgrp
date: 2011-08-27 02:13:04
summary: [js,refactor][s]: move package name slug generation into application.js and refactor to be js object.
affected #: 4 files (8.3 KB)
--- a/ckan/public/scripts/application.js Fri Aug 26 10:17:15 2011 +0100
+++ b/ckan/public/scripts/application.js Sat Aug 27 01:13:04 2011 +0100
@@ -166,6 +166,117 @@
});
};
+ // Name slug generator for $name element using $title element
+ //
+ // Also does nice things like show errors if name not available etc
+ //
+ // Usage: CKAN.Utils.PackageSlugCreator.create($('#my-title'), $('#my-name'))
+ my.PackageSlugCreator = (function() {
+ // initialize function
+ //
+ // args: $title and $name input elements
+ function SlugCreator($title, $name) {
+ this.name_field = $name;
+ this.title_field = $title;
+ // Keep a variable where we can store whether the name field has been
+ // directly modified by the user or not. If it has, we should no longer
+ // fetch updates.
+ this.name_changed = false;
+ // url for slug api (we need api rather than do it ourself because we check if available)
+ this.url = '/api/2/util/package/create_slug';
+ // Add a new element where the validity of the package name can be displayed
+ this.name_field.parent().append('<div id="package_name_valid_msg"></div>');
+ this.title_field.blur(this.title_change_handler())
+ this.title_field.keyup(this.title_change_handler())
+ this.name_field.keyup(this.name_change_handler());
+ this.name_field.blur(this.name_blur_handler());
+ }
+
+ SlugCreator.create = function($title, $name) {
+ return new SlugCreator($title, $name);
+ }
+
+ SlugCreator.prototype.title_change_handler = function() {
+ var self = this;
+ return function() {
+ if (!self.name_changed && self.title_field.val().replace(/^\s+|\s+$/g, '')) {
+ self.update(self.title_field.val(), function(data) {self.name_field.val(data.name)});
+ }
+ }
+ }
+
+ SlugCreator.prototype.name_blur_handler = function() {
+ var self = this;
+ return function() {
+ // Reset if the name is emptied
+ if (!self.name_field.val().replace(/^\s+|\s+$/g, '')){
+ self.name_changed = false;
+ $('#package_name_valid_msg').html('');
+ } else {
+ self.update(self.name_field.val(), function(data) {
+ self.name_field.val(data.name)
+ });
+ }
+ };
+ }
+
+ SlugCreator.prototype.name_change_handler = function() {
+ var self = this;
+ return function() {
+ // Reset if the name is emptied
+ if (!self.name_field.val().replace(/^\s+|\s+$/g, '')){
+ self.name_changed = false;
+ $('#package_name_valid_msg').html('');
+ } else {
+ self.name_changed = true;
+ self.update(self.name_field.val(), function(data) {
+ if (self.name_field.val().length >= data.name) {
+ self.name_field.val(data.name);
+ }
+ });
+ }
+ };
+ }
+
+ // Create a function for fetching the value and updating the result
+ SlugCreator.prototype.perform_update = function(value, on_success){
+ var self = this;
+ $.ajax({
+ url: self.url,
+ data: 'title=' + value,
+ dataType: 'jsonp',
+ type: 'get',
+ jsonpCallback: 'callback',
+ success: function (data) {
+ if (on_success) {
+ on_success(data);
+ }
+ var valid_msg = $('#package_name_valid_msg');
+ if (data.valid) {
+ valid_msg.html('<span style="font-weight: bold; color: #0c0">This package name is available!</span>');
+ } else {
+ valid_msg.html('<span style="font-weight: bold; color: #c00">This package name is already used, please use a different name</span>');
+ }
+ }
+ });
+ }
+
+ // We only want to perform the update if there hasn't been a change for say 200ms
+ var timer = null;
+ SlugCreator.prototype.update = function(value, on_success) {
+ var self = this;
+ if (this.timer) {
+ clearTimeout(this.timer)
+ };
+ this.timer = setTimeout(function () {
+ self.perform_update(value, on_success)
+ }, 200);
+ }
+
+ return SlugCreator;
+ })();
+
+
return my;
}(jQuery, CKAN.Utils || {});
--- a/ckan/templates/package/form_fields.html Fri Aug 26 10:17:15 2011 +0100
+++ b/ckan/templates/package/form_fields.html Sat Aug 27 01:13:04 2011 +0100
@@ -2,115 +2,11 @@
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="">
-<py:if test="hasattr(c, 'package_create_slug_api_url')"><script type="text/javascript">
-//<![CDATA[
-(function($){
- $.fn.ajaxCreateSlug = function(name, url) {
- var title = this;
- var updater = {
- init: function(title, name) {
- // Add a new element where the validity of the package name can be displayed
- this.name_field = name;
- this.title_field = title;
- this.name_field.parent().append('<div id="package_name_valid_msg"></div>');
- this.title_field.blur(this.title_change_handler())
- this.title_field.keyup(this.title_change_handler())
- this.name_field.keyup(this.name_change_handler());
- this.name_field.blur(this.name_blur_handler());
- this.url = url;
- },
- title_change_handler: function() {
- var self = this;
- return function() {
- if (!self.name_changed && self.title_field.val().replace(/^\s+|\s+$/g, '')) {
- self.update(self.title_field.val(), function(data) {self.name_field.val(data.name)});
- }
- }
- },
- name_blur_handler: function() {
- var self = this;
- return function() {
- // Reset if the name is emptied
- if (!self.name_field.val().replace(/^\s+|\s+$/g, '')){
- self.name_changed = false;
- $('#package_name_valid_msg').html('');
- } else {
- self.update(self.name_field.val(), function(data) {
- self.name_field.val(data.name)
- });
- }
- };
- },
- name_change_handler: function() {
- var self = this;
- return function() {
- // Reset if the name is emptied
- if (!self.name_field.val().replace(/^\s+|\s+$/g, '')){
- self.name_changed = false;
- $('#package_name_valid_msg').html('');
- } else {
- self.name_changed = true;
- self.update(self.name_field.val(), function(data) {
- if (self.name_field.val().length >= data.name) {
- self.name_field.val(data.name);
- }
- });
- }
- };
- },
- // Keep a variable where we can store whether the name field has been
- // directly modified by the user or not. If it has, we should no longer
- // fetch updates.
- name_changed: false,
-
- // Create a function for fetching the value and updating the result
- perform_update: function(value, on_success){
- var self = this;
- $.ajax({
- url: self.url,
- data: 'title=' + value,
- dataType: 'jsonp',
- type: 'get',
- jsonpCallback: 'callback',
- success: function (data) {
- if (on_success) {
- on_success(data);
- }
- var valid_msg = $('#package_name_valid_msg');
- if (data.valid) {
- valid_msg.html('<span style="font-weight: bold; color: #0c0">This package name is available!</span>');
- } else {
- valid_msg.html('<span style="font-weight: bold; color: #c00">This package name is already used, please use a different name</span>');
- }
- }
- });
- },
- // We only want to perform the update if there hasn't been a change for say 200ms
- timer: null,
- update: function(value, on_success) {
- var self = this;
- if (this.timer) {
- clearTimeout(this.timer)
- };
- this.timer = setTimeout(function () {
- self.perform_update(value, on_success)
- }, 200);
- }
- }
- updater.init(title, $(name), url);
- return title;
- };
-})( jQuery );
-
-$(document).ready(function() {
- $('#Package--title').ajaxCreateSlug('#Package--name', '${c.package_create_slug_api_url}');
-});
-
-//]]>
+ jQuery(document).ready(function($) {
+ CKAN.Utils.PackageSlugCreator.create($('#Package--title'), $('#Package--name'));
+ });
</script>
-</py:if>
-
<py:def function="form_fields(field_group)"><?python
--- a/ckan/templates/package/new.html Fri Aug 26 10:17:15 2011 +0100
+++ b/ckan/templates/package/new.html Sat Aug 27 01:13:04 2011 +0100
@@ -16,7 +16,14 @@
<script type="text/javascript" src="${g.site_url}/scripts/flexitable.js"></script><link rel="stylesheet" href="${g.site_url}/css/flexitable.css" />
- <xi:include href="new_package_form.js"/>
+ <script type="text/javascript">
+ jQuery(document).ready(function($) {
+ CKAN.Utils.PackageSlugCreator.create($('#title'), $('#name'));
+ if (!$('#preview').length) {
+ $("#title").focus();
+ }
+ });
+ </script></py:def><div py:match="content">
--- a/ckan/templates/package/new_package_form.js Fri Aug 26 10:17:15 2011 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,110 +0,0 @@
-
-<script type="text/javascript">
-//<![CDATA[
-(function($){
- $.fn.ajaxCreateSlug = function(name, url) {
- var title = this;
- var updater = {
- init: function(title, name) {
- // Add a new element where the validity of the package name can be displayed
- this.name_field = name;
- this.title_field = title;
- this.name_field.parent().append('<div id="package_name_valid_msg"></div>');
- this.title_field.blur(this.title_change_handler())
- this.title_field.keyup(this.title_change_handler())
- this.name_field.keyup(this.name_change_handler());
- this.name_field.blur(this.name_blur_handler());
- this.url = url;
- },
- title_change_handler: function() {
- var self = this;
- return function() {
- if (!self.name_changed && self.title_field.val().replace(/^\s+|\s+$/g, '')) {
- self.update(self.title_field.val(), function(data) {self.name_field.val(data.name)});
- }
- }
- },
- name_blur_handler: function() {
- var self = this;
- return function() {
- // Reset if the name is emptied
- if (!self.name_field.val().replace(/^\s+|\s+$/g, '')){
- self.name_changed = false;
- $('#package_name_valid_msg').html('');
- } else {
- self.update(self.name_field.val(), function(data) {
- self.name_field.val(data.name)
- });
- }
- };
- },
- name_change_handler: function() {
- var self = this;
- return function() {
- // Reset if the name is emptied
- if (!self.name_field.val().replace(/^\s+|\s+$/g, '')){
- self.name_changed = false;
- $('#package_name_valid_msg').html('');
- } else {
- self.name_changed = true;
- self.update(self.name_field.val(), function(data) {
- if (self.name_field.val().length >= data.name) {
- self.name_field.val(data.name);
- }
- });
- }
- };
- },
- // Keep a variable where we can store whether the name field has been
- // directly modified by the user or not. If it has, we should no longer
- // fetch updates.
- name_changed: false,
- // Create a function for fetching the value and updating the result
- perform_update: function(value, on_success){
- var self = this;
- $.ajax({
- url: self.url,
- data: 'title=' + value,
- dataType: 'jsonp',
- type: 'get',
- jsonpCallback: 'callback',
- success: function (data) {
- if (on_success) {
- on_success(data);
- }
- var valid_msg = $('#package_name_valid_msg');
- if (data.valid) {
- valid_msg.html('<span style="font-weight: bold; color: #0c0">This package name is available!</span>');
- } else {
- valid_msg.html('<span style="font-weight: bold; color: #c00">This package name is already used, please use a different name</span>');
- }
- }
- });
- },
- // We only want to perform the update if there hasn't been a change for say 200ms
- timer: null,
- update: function(value, on_success) {
- var self = this;
- if (this.timer) {
- clearTimeout(this.timer)
- };
- this.timer = setTimeout(function () {
- self.perform_update(value, on_success)
- }, 200);
- }
- }
- updater.init(title, $(name), url);
- return title;
- };
-})( jQuery );
-$(document).ready(function() {
- $('#title').ajaxCreateSlug('#name', '/api/2/util/package/create_slug');
-});
-
-$(document).ready(function () {
- if (!$('#preview').length) {
- $("#title").focus();
- }
-});
-//]]>
-</script>
Repository URL: https://bitbucket.org/okfn/ckan/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the ckan-changes
mailing list