Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Initial version of the ui-input directive - RFC #191

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions modules/directives/input/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
angular.module('ui.directives')
.directive('uiInput', ['$compile', '$http', '$templateCache', 'ui.config', function ($compile, $http, $templateCache, uiConfig) {

uiConfig.input = uiConfig.input || {};

var idUid = ['0', '0', '0'];
var nameUid = ['0', '0', '0'];

/*
Copyright (c) 2010-2012 Google, Inc. http://angularjs.org
This function is slightly modified version of the nextUid() function found in AngularJS code base
*/
function nextUid(uid) {
var index = uid.length;
var digit;

while (index) {
index--;
digit = uid[index].charCodeAt(0);
if (digit == 57 /*'9'*/) {
uid[index] = 'A';
return uid.join('');
}
if (digit == 90 /*'Z'*/) {
uid[index] = '0';
} else {
uid[index] = String.fromCharCode(digit + 1);
return uid.join('');
}
}
uid.unshift('0');
return uid.join('');
}

/*
Copyright (c) 2010-2012 Google, Inc. http://angularjs.org
This function is slightly modified version of the snake_case() function found in AngularJS code base
*/
function snake_case(name, separator) {
return name.replace(/[A-Z]/g, function (letter, pos) {
return (pos ? separator : '') + letter.toLowerCase();
});
}

return {
restrict:'EA',
priority:10000,
terminal:true,
scope:true,
require:'?uiForm',
compile:function compile(tElement, tAttrs, transclude, uiForm) {

var model = tAttrs.ngModel, input = {
id:tAttrs.id || 'input' + nextUid(idUid)
};

return function (scope, element, attrs) {

//infer a field type from template's tag name (can be one of ui-input, ui-select, ui-textarea)
$http.get(scope.$eval(attrs.src), {cache:$templateCache}).success(function (response) {

element.html(response);

var inputEl = element.find('[ng-transclude]');
angular.forEach(tAttrs, function (value, key) {
if (key.charAt(0) !== '$' && ['src','uiInput'].indexOf(key) === -1) {
inputEl.attr(snake_case(key, '-'), value);
}
});

//prepare validation messages
input.validation = angular.extend({}, uiConfig.input.validation, scope.$eval(tAttrs.validation));

//expose model to a field's template
scope.$input = input;
$compile(element.contents())(scope);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think $compile errors because of the ng-transcludes. It doesn't know what to do. You'll have to manually transclude the elements with ng-transclude attribute, then remove them before $compiling.

OR alternatively, you could try putting in all the html during compile phase, and it might know how to use the transcludes. But you don't scope during compile phase, so that would hard to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Touché, I guess I just figured that ng-transclude was not actually a
directive but a subset.
On Oct 31, 2012 7:46 PM, "Andy Joslin" notifications@github.com wrote:

In modules/directives/input/input.js:

  •      element.html(response);
    
  •      var inputEl = element.find('[ng-transclude]');
    
  •      angular.forEach(tAttrs, function (value, key) {
    
  •        if (key.charAt(0) !== '$' && ['src','uiInput'].indexOf(key) === -1) {
    
  •          inputEl.attr(snake_case(key, '-'), value);
    
  •        }
    
  •      });
    
  •      //prepare validation messages
    
  •      input.validation = angular.extend({}, uiConfig.input.validation, scope.$eval(tAttrs.validation));
    
  •      //expose model to a field's template
    
  •      scope.$input = input;
    
  •      $compile(element.contents())(scope);
    

I think $compile errors because of the ng-transcludes. It doesn't know
what to do. You'll have to manually transclude the elements with
ng-transclude attribute, then remove it before $compiling.


Reply to this email directly or view it on GitHubhttps://github.com//pull/191/files#r2001035.

scope.$field = inputEl.controller('ngModel');
});
};
}
};
}]);