ko.bindingHandlers.placeholder = init: (elem, valueAccessor, allBindingsAccessor, viewModel) -> update: (elem, valueAccessor, allBindingsAccessor) -> text = ko.utils.unwrapObservable valueAccessor() $(elem).attr('placeholder', text) $(elem).inputHint() ko.bindingHandlers.validation = init: (elem, valueAccessor, allBindingsAccessor, viewModel) -> validation = ko.utils.unwrapObservable valueAccessor() validation(elem) ko.bindingHandlers.showWithSlide = init: (elem, valueAccessor, allBindingsAccessor, viewModel) -> shouldBeVisible = ko.utils.unwrapObservable valueAccessor() if shouldBeVisible then $(elem).show() else $(elem).hide() $(elem).removeClass('hide') update: (elem, valueAccessor, allBindingsAccessor) -> shouldBeVisible = ko.utils.unwrapObservable valueAccessor() duration = allBindingsAccessor().slideDuration || 600 if shouldBeVisible then $(elem).slideDown(duration) else $(elem).slideUp(duration) ko.bindingHandlers.fadeIn = init: (elem, valueAccessor, allBindingsAccessor, viewModel) -> shouldBeVisible = ko.utils.unwrapObservable valueAccessor() if shouldBeVisible then $(elem).show() else $(elem).hide() $(elem).removeClass('hide') update: (elem, valueAccessor, allBindingsAccessor) -> shouldBeVisible = ko.utils.unwrapObservable valueAccessor() duration = allBindingsAccessor().slideDuration || 600 if shouldBeVisible then $(elem).fadeIn(duration) else $(elem).fadeOut(duration) ko.bindingHandlers.show = update: (elem, valueAccessor, allBindingsAccessor, viewModel) -> shouldBeVisible = ko.utils.unwrapObservable valueAccessor() if shouldBeVisible then $(elem).show() else $(elem).hide() $(elem).removeClass('hide') ko.bindingHandlers.hide = update: (elem, valueAccessor, allBindingsAccessor, viewModel) -> shouldBeHidden = ko.utils.unwrapObservable valueAccessor() if shouldBeHidden then $(elem).hide() else $(elem).show() $(elem).removeClass('hide') ko.bindingHandlers.chosen = { init: (element, valueAccessor, allBindingsAccessor, viewModel)-> chosen = allBindingsAccessor().chosen settings = {'default': 'Select one...'} $.extend(settings, chosen.settings) if chosen.settings? $(element).attr('data-placeholder', settings.default) update: (element, valueAccessor, allBindingsAccessor, viewModel)-> chosen = allBindingsAccessor().chosen settings = {'default': 'Select one...', disable_search_threshold: 10} $.extend(settings, chosen.settings) if chosen.settings? selectedOptions = if chosen.selectedOptions? then chosen.selectedOptions() else [] selectedOptions.push(chosen.selectedOption()) if chosen.selectedOption? $(element).html('') $(element).append("") if option.sub_options? and option.sub_options.length > 0 then for suboption in option.sub_options otitle = (if typeof suboption == 'string' then suboption else if $.isPlainObject(suboption) then suboption.title) ovalue = (if typeof suboption == 'string' then suboption else if $.isPlainObject(suboption) then suboption.value) selected_attr = if $.inArray(ovalue, selectedOptions) >= 0 then ' selected=selected' else '' $(element).append("") $(element).unbind('change.chosen').chosen(settings).bind 'change.chosen', (event)-> if $(element).context.type == 'select-multiple' chosen.writeSelectedOptions(if $(element).val()? then $(element).val() else []) if chosen.writeSelectedOptions? return ids_array = $(element).val().split('::') if ids_array.length > 1 module_title = $(element).find("option[value='#{ids_array[0]}']").text() fs_title = $(element).find('option:selected').html().replace('   - ', '') title = "#{module_title}: #{fs_title}" $('#' + $(element).attr('id') + '_chosen').find('a.chosen-single span').html(title) $(element).trigger("chosen:updated")# if $(element).context.type != 'select-multiple' } ko.bindingHandlers.select2 = { init: ((element, valueAccessor, allBindingsAccessor) -> obj = valueAccessor() allBindings = allBindingsAccessor() lookupKey = allBindings.lookupKey $(element).select2(obj) $(element).select2('enable', !!obj.enable) if obj.enable? if (lookupKey) value = ko.utils.unwrapObservable(allBindings.value) $(element).select2 'data', ko.utils.arrayFirst(obj.data.results, (item)-> return item[lookupKey] == value ) ko.utils.domNodeDisposal.addDisposeCallback(element, -> $(element).select2('destroy') ) ) update: ((element, valueAccessor) -> obj = valueAccessor() $(element).select2('enable', !!obj.enable) if obj.enable? $(element).trigger('change') ) } ko.bindingHandlers.class = update: (element, valueAccessor)-> if element['__ko__previousClassValue__'] $(element).removeClass(element['__ko__previousClassValue__']) value = ko.utils.unwrapObservable(valueAccessor()) $(element).addClass(value) element['__ko__previousClassValue__'] = value ko.bindingHandlers.pageSlider = init: (element, valueAccessor, allBindingsAccessor, viewModel) -> allBindings = allBindingsAccessor() maxPageObservable = if typeof allBindings.maxPage == 'undefined' then viewModel.maxPage else allBindings.maxPage maxPageUnwrapped = ko.utils.unwrapObservable(maxPageObservable) pageObservable = valueAccessor() if typeof maxPageUnwrapped == 'undefined' throw Error("A maxPage needs to exist in the model or specified as maxPage on the data-bind") return false $(element).addClass 'paging_slider' # Setup the paginateSlider to update the page observable $(element).paginateSlider(maxPageUnwrapped).bind 'slidechange', (e, ui)-> pageObservable ui.value # Any changes to the maxPage will change the pagination numPage maxPageObservable.subscribe (newValue) -> $(element).paginateSlider 'numPages', newValue update: (element, valueAccessor) -> $(element).paginateSlider 'currentPage', ko.utils.unwrapObservable(valueAccessor()) BINDINGS_TO_APPLY = {} ko.addBindings = (bindings) -> for k,v of bindings BINDINGS_TO_APPLY[k] = v $ -> ko.applyBindings BINDINGS_TO_APPLY