PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/static/public/js/libs/editors/multi-editor.js

http://github.com/jspears/bobamo
JavaScript | 63 lines | 52 code | 6 blank | 5 comment | 22 complexity | 34b99c674eb7937e531be1191ff7a50d MD5 | raw file
Possible License(s): Apache-2.0
  1. define(['Backbone.Form', 'jquery', 'underscore'], function (Form, $, _) {
  2. var editors = Form.editors;
  3. var MultiEditor = editors.MultiEditor = editors.Select.extend({
  4. initialize:function (options) {
  5. editors.Select.prototype.initialize.apply(this, _.toArray(arguments));
  6. if (this.schema && ( this.schema.multiple !== false || this.schema.schemaType == 'Array' || this.schema.type == 'Array' )){
  7. this.$el.attr('multiple', 'multiple');
  8. this._multiple = true;
  9. }
  10. return this;
  11. },
  12. setValue:function (value) {
  13. if (value)
  14. $('option', this.el).each(function (k, v) {
  15. var $v = $(v);
  16. if (value && value.indexOf($v.val()) > -1)
  17. $v.attr('selected', 'selected');
  18. });
  19. this.value = value;
  20. },
  21. selectNone:'<option data-none="null" value="">None</option>',
  22. renderOptions:function (options) {
  23. var $select = this.$el,
  24. html = '';
  25. var soptions = this.schema.options;
  26. if (!(soptions && soptions.required )) {
  27. html += soptions && soptions.selectNone || this.selectNone;
  28. }
  29. //Accept string of HTML
  30. if (_.isString(options)) {
  31. html += options;
  32. }
  33. //Or array
  34. else if (_.isArray(options)) {
  35. html += this._arrayToHtml(options);
  36. }
  37. //Or Backbone collection
  38. else if (options instanceof Backbone.Collection) {
  39. html += this._collectionToHtml(options)
  40. }
  41. //Insert options
  42. $select.html(html);
  43. //Select correct option
  44. this.setValue(this.value);
  45. },
  46. getValue:function(){
  47. var $children =this.$el.children();
  48. for(var i=0,l=$children.length; i<l; i++){
  49. if($children[i].selected && $($children[i]).attr('data-none') == 'null'){
  50. return null;
  51. }
  52. }
  53. return this.$el.val();
  54. }
  55. });
  56. return MultiEditor;
  57. });